response.ipp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* Copyright (c) 2018-2023 Marcelo Zimbres Silva ([email protected])
  2. *
  3. * Distributed under the Boost Software License, Version 1.0. (See
  4. * accompanying file LICENSE.txt)
  5. */
  6. #include <boost/redis/response.hpp>
  7. #include <boost/redis/error.hpp>
  8. #include <boost/assert.hpp>
  9. namespace boost::redis
  10. {
  11. void consume_one(generic_response& r, system::error_code& ec)
  12. {
  13. if (r.has_error())
  14. return; // Nothing to consume.
  15. if (std::empty(r.value()))
  16. return; // Nothing to consume.
  17. auto const depth = r.value().front().depth;
  18. // To simplify we will refuse to consume any data-type that is not
  19. // a root node. I think there is no use for that and it is complex
  20. // since it requires updating parent nodes.
  21. if (depth != 0) {
  22. ec = error::incompatible_node_depth;
  23. return;
  24. }
  25. auto f = [depth](auto const& e)
  26. { return e.depth == depth; };
  27. auto match = std::find_if(std::next(std::cbegin(r.value())), std::cend(r.value()), f);
  28. r.value().erase(std::cbegin(r.value()), match);
  29. }
  30. void consume_one(generic_response& r)
  31. {
  32. system::error_code ec;
  33. consume_one(r, ec);
  34. if (ec)
  35. throw system::system_error(ec);
  36. }
  37. } // boost::redis::resp3