response.ipp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* Copyright (c) 2018-2024 Marcelo Zimbres Silva (mzimbres@gmail.com)
  2. *
  3. * Distributed under the Boost Software License, Version 1.0. (See
  4. * accompanying file LICENSE.txt)
  5. */
  6. #include <boost/redis/error.hpp>
  7. #include <boost/redis/response.hpp>
  8. #include <boost/assert.hpp>
  9. namespace boost::redis {
  10. void consume_one(generic_response& r, system::error_code& ec)
  11. {
  12. if (r.has_error())
  13. return; // Nothing to consume.
  14. if (std::empty(r.value()))
  15. return; // Nothing to consume.
  16. auto const depth = r.value().front().depth;
  17. // To simplify we will refuse to consume any data-type that is not
  18. // a root node. I think there is no use for that and it is complex
  19. // since it requires updating parent nodes.
  20. if (depth != 0) {
  21. ec = error::incompatible_node_depth;
  22. return;
  23. }
  24. auto f = [depth](auto const& e) {
  25. return e.depth == depth;
  26. };
  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. } // namespace boost::redis