1 // Copyright 2022 The Chromium Authors 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 THIRD_PARTY_RUST_SERDE_JSON_LENIENT_V0_2_WRAPPER_SERDE_JSON_LENIENT_H_ 6 #define THIRD_PARTY_RUST_SERDE_JSON_LENIENT_V0_2_WRAPPER_SERDE_JSON_LENIENT_H_ 7 8 #include <stdint.h> 9 10 #include "third_party/rust/cxx/v1/cxx.h" 11 12 namespace serde_json_lenient { 13 14 // An opaque pointer provided by the caller to decode_json() which is passed 15 // through to the visitor functions on `Functions`. 16 struct ContextPointer; 17 18 // C++ functions that provide the functionality for each stop by the JSON 19 // deserializer in order to construct the output in C++ from the JSON 20 // deserialization. 21 // 22 // TODO(danakj): CXX does not support function pointers, so we need to use a 23 // struct of methods (this struct) for it to call instead of a structure of 24 // function pointers which could be defined directly in the Rust code. 25 struct Functions { 26 void (*list_append_none_fn)(ContextPointer&); 27 void (*list_append_bool_fn)(ContextPointer&, bool); 28 void (*list_append_i32_fn)(ContextPointer&, int32_t); 29 void (*list_append_f64_fn)(ContextPointer&, double); 30 void (*list_append_str_fn)(ContextPointer&, rust::Str); 31 // The returned ContextPointer reference is given to the visitor functions as 32 // an argument for all nodes visited in this list. 33 ContextPointer& (*list_append_list_fn)(ContextPointer&, size_t); 34 // The returned ContextPointer reference is given to the visitor functions as 35 // an argument for all nodes visited in this dict. 36 ContextPointer& (*list_append_dict_fn)(ContextPointer&); 37 38 void (*dict_set_none_fn)(ContextPointer&, rust::Str); 39 void (*dict_set_bool_fn)(ContextPointer&, rust::Str, bool); 40 void (*dict_set_i32_fn)(ContextPointer&, rust::Str, int32_t); 41 void (*dict_set_f64_fn)(ContextPointer&, rust::Str, double); 42 void (*dict_set_str_fn)(ContextPointer&, rust::Str, rust::Str); 43 // The returned ContextPointer reference is given to the visitor functions as 44 // an argument for all nodes visited in this list. 45 ContextPointer& (*dict_set_list_fn)(ContextPointer&, rust::Str, size_t); 46 // The returned ContextPointer reference is given to the visitor functions as 47 // an argument for all nodes visited in this dict. 48 ContextPointer& (*dict_set_dict_fn)(ContextPointer&, rust::Str); 49 list_append_noneFunctions50 void list_append_none(ContextPointer& c) const { 51 list_append_none_fn(c); 52 } list_append_boolFunctions53 void list_append_bool(ContextPointer& c, bool val) const { 54 list_append_bool_fn(c, val); 55 } list_append_i32Functions56 void list_append_i32(ContextPointer& c, int32_t val) const { 57 list_append_i32_fn(c,val); 58 } list_append_f64Functions59 void list_append_f64(ContextPointer& c, double val) const { 60 list_append_f64_fn(c, val); 61 } list_append_strFunctions62 void list_append_str(ContextPointer& c, rust::Str val) const { 63 list_append_str_fn(c, val); 64 } list_append_listFunctions65 ContextPointer& list_append_list(ContextPointer& c, size_t reserve) const { 66 return list_append_list_fn(c, reserve); 67 } list_append_dictFunctions68 ContextPointer& list_append_dict(ContextPointer& c) const { 69 return list_append_dict_fn(c); 70 } 71 dict_set_noneFunctions72 void dict_set_none(ContextPointer& c, rust::Str key) const { 73 dict_set_none_fn(c, key); 74 } dict_set_boolFunctions75 void dict_set_bool(ContextPointer& c, rust::Str key, bool val) const { 76 dict_set_bool_fn(c, key, val); 77 } dict_set_i32Functions78 void dict_set_i32(ContextPointer& c, rust::Str key, int32_t val) const { 79 dict_set_i32_fn(c, key,val); 80 } dict_set_f64Functions81 void dict_set_f64(ContextPointer& c, rust::Str key, double val) const { 82 dict_set_f64_fn(c, key, val); 83 } dict_set_strFunctions84 void dict_set_str(ContextPointer& c, rust::Str key, rust::Str val) const { 85 dict_set_str_fn(c, key, val); 86 } dict_set_listFunctions87 ContextPointer& dict_set_list(ContextPointer& c, rust::Str key, 88 size_t reserve) const { 89 return dict_set_list_fn(c, key, reserve); 90 } dict_set_dictFunctions91 ContextPointer& dict_set_dict(ContextPointer& c, rust::Str key) const { 92 return dict_set_dict_fn(c, key); 93 } 94 }; 95 96 } // namespace serde_json_lenient 97 98 #endif // THIRD_PARTY_RUST_SERDE_JSON_LENIENT_V0_2_WRAPPER_SERDE_JSON_LENIENT_H_ 99