error_support.cc 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "error_support.h"
  5. #include <cassert>
  6. namespace crdtp {
  7. void ErrorSupport::Push() {
  8. stack_.emplace_back();
  9. }
  10. void ErrorSupport::Pop() {
  11. stack_.pop_back();
  12. }
  13. void ErrorSupport::SetName(const char* name) {
  14. assert(!stack_.empty());
  15. stack_.back().type = NAME;
  16. stack_.back().name = name;
  17. }
  18. void ErrorSupport::SetIndex(size_t index) {
  19. assert(!stack_.empty());
  20. stack_.back().type = INDEX;
  21. stack_.back().index = index;
  22. }
  23. void ErrorSupport::AddError(const char* error) {
  24. assert(!stack_.empty());
  25. if (!errors_.empty())
  26. errors_ += "; ";
  27. for (size_t ii = 0; ii < stack_.size(); ++ii) {
  28. if (ii)
  29. errors_ += ".";
  30. const Segment& s = stack_[ii];
  31. switch (s.type) {
  32. case NAME:
  33. errors_ += s.name;
  34. continue;
  35. case INDEX:
  36. errors_ += std::to_string(s.index);
  37. continue;
  38. default:
  39. assert(s.type != EMPTY);
  40. continue;
  41. }
  42. }
  43. errors_ += ": ";
  44. errors_ += error;
  45. }
  46. span<uint8_t> ErrorSupport::Errors() const {
  47. return SpanFrom(errors_);
  48. }
  49. } // namespace crdtp