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 5 #ifndef V8_CRDTP_ERROR_SUPPORT_H_ 6 #define V8_CRDTP_ERROR_SUPPORT_H_ 7 8 #include <cstdint> 9 #include <string> 10 #include <vector> 11 #include "export.h" 12 #include "span.h" 13 14 namespace v8_crdtp { 15 // ============================================================================= 16 // ErrorSupport - For tracking errors in tree structures. 17 // ============================================================================= 18 19 // This abstraction is used when converting between Values and inspector 20 // objects, e.g. in lib/ValueConversions_{h,cc}.template. As the processing 21 // enters and exits a branch, we call Push / Pop. Within the branch, 22 // we either set the name or an index (in case we're processing the element of a 23 // list/vector). Only once an error is seen, the path which is now on the 24 // stack is materialized and prefixes the error message. E.g., 25 // "foo.bar.2: some error". After error collection, ::Errors() is used to 26 // access the message. 27 class ErrorSupport { 28 public: 29 // Push / Pop operations for the path segments; after Push, either SetName or 30 // SetIndex must be called exactly once. 31 void Push(); 32 void Pop(); 33 34 // Sets the name of the current segment on the stack; e.g. a field name. 35 // |name| must be a C++ string literal in 7 bit US-ASCII. 36 void SetName(const char* name); 37 // Sets the index of the current segment on the stack; e.g. an array index. 38 void SetIndex(size_t index); 39 40 // Materializes the error internally. |error| must be a C++ string literal 41 // in 7 bit US-ASCII. 42 void AddError(const char* error); 43 44 // Returns the semicolon-separated list of errors as in 7 bit ASCII. 45 span<uint8_t> Errors() const; 46 47 private: 48 enum SegmentType { EMPTY, NAME, INDEX }; 49 struct Segment { 50 SegmentType type = EMPTY; 51 union { 52 const char* name; 53 size_t index; 54 }; 55 }; 56 std::vector<Segment> stack_; 57 std::string errors_; 58 }; 59 60 } // namespace v8_crdtp 61 62 #endif // V8_CRDTP_ERROR_SUPPORT_H_ 63