pub struct ObjectBuilder<'a> { /* private fields */ }Expand description
Builds an inner object or map within a Doc or another element.
A builder can either be obtained via DocBuilder::root_object_builder or using either ObjectBuilder::put_object - to place an inner object in another or via ListBuilder::append_object to add an object to a list.
Implementations§
Source§impl<'a> ObjectBuilder<'a>
impl<'a> ObjectBuilder<'a>
Sourcepub fn put_int(&mut self, key: impl AsRef<str>, value: i64) -> Result<()>
pub fn put_int(&mut self, key: impl AsRef<str>, value: i64) -> Result<()>
Places an integer value within the object being built.
§Errors
If the internal symbol table overflows, an error is returned.
§Example
let mut builder = DocBuilder::new();
let mut obj_builder = builder.root_object_builder();
obj_builder.put_int("Test", 42).unwrap();
let doc = builder.build();
assert_eq!(doc.root().query("Test").as_int().unwrap(), 42);Sourcepub fn insert_int(&mut self, key: Symbol, value: i64)
pub fn insert_int(&mut self, key: Symbol, value: i64)
Places an integer value within the object being built using a Symbolwhich has been looked
up previously.
§Example
let mut builder = DocBuilder::new();
let key = builder.resolve("Test").unwrap();
let mut obj_builder = builder.root_object_builder();
obj_builder.insert_int(key, 42);
let doc = builder.build();
assert_eq!(doc.root().query("Test").as_int().unwrap(), 42);Sourcepub fn put_string(
&mut self,
key: impl AsRef<str>,
value: impl AsRef<str>,
) -> Result<()>
pub fn put_string( &mut self, key: impl AsRef<str>, value: impl AsRef<str>, ) -> Result<()>
Places a string value within the object being built.
§Errors
If the internal symbol table overflows, an error is returned.
§Example
let mut builder = DocBuilder::new();
let mut obj_builder = builder.root_object_builder();
obj_builder.put_string("Test", "Foo").unwrap();
let doc = builder.build();
assert_eq!(doc.root().query("Test").as_str().unwrap(), "Foo");Sourcepub fn insert_string(&mut self, key: Symbol, value: impl AsRef<str>)
pub fn insert_string(&mut self, key: Symbol, value: impl AsRef<str>)
Places a string value within the object being built using a Symbolwhich has been looked
up previously.
§Example
let mut builder = DocBuilder::new();
let key = builder.resolve("Test").unwrap();
let mut obj_builder = builder.root_object_builder();
obj_builder.insert_string(key, "Foo");
let doc = builder.build();
assert_eq!(doc.root().query("Test").as_str().unwrap(), "Foo");Sourcepub fn put_bool(&mut self, key: impl AsRef<str>, value: bool) -> Result<()>
pub fn put_bool(&mut self, key: impl AsRef<str>, value: bool) -> Result<()>
Places a bool value within the object being built.
§Errors
If the internal symbol table overflows, an error is returned.
§Example
let mut builder = DocBuilder::new();
let mut obj_builder = builder.root_object_builder();
obj_builder.put_bool("Test", true).unwrap();
let doc = builder.build();
assert_eq!(doc.root().query("Test").as_bool(), true);Sourcepub fn insert_bool(&mut self, key: Symbol, value: bool)
pub fn insert_bool(&mut self, key: Symbol, value: bool)
Places bool value within the object being built using a Symbolwhich has been looked
up previously.
§Example
let mut builder = DocBuilder::new();
let key = builder.resolve("Test").unwrap();
let mut obj_builder = builder.root_object_builder();
obj_builder.insert_bool(key, true);
let doc = builder.build();
assert_eq!(doc.root().query("Test").as_bool(), true);Sourcepub fn put_list(&mut self, key: impl AsRef<str>) -> Result<ListBuilder<'_>>
pub fn put_list(&mut self, key: impl AsRef<str>) -> Result<ListBuilder<'_>>
Places a list value within the object being built. Returns the builder used to populate the list.
§Errors
If the internal symbol table overflows, an error is returned.
§Example
let mut builder = DocBuilder::new();
let mut obj_builder = builder.root_object_builder();
let mut list_builder = obj_builder.put_list("Test").unwrap();
list_builder.append_int(1);
let doc = builder.build();
assert_eq!(doc.root().query("Test").at(0).as_int().unwrap(), 1);Sourcepub fn insert_list(&mut self, key: Symbol) -> ListBuilder<'_>
pub fn insert_list(&mut self, key: Symbol) -> ListBuilder<'_>
Places a list value within the object being built using a Symbolwhich has been looked
up previously. Returns the builder used to populate the list.
§Example
let mut builder = DocBuilder::new();
let key = builder.resolve("Test").unwrap();
let mut obj_builder = builder.root_object_builder();
let mut list_builder = obj_builder.insert_list(key);
list_builder.append_int(1);
let doc = builder.build();
assert_eq!(doc.root().query("Test").at(0).as_int().unwrap(), 1);Sourcepub fn put_object(&mut self, key: impl AsRef<str>) -> Result<ObjectBuilder<'_>>
pub fn put_object(&mut self, key: impl AsRef<str>) -> Result<ObjectBuilder<'_>>
Places an inner object within the object being built. Returns the builder used to populate the list.
§Errors
If the internal symbol table overflows, an error is returned.
§Example
let mut builder = DocBuilder::new();
let mut obj_builder = builder.root_object_builder();
let mut inner_obj_builder = obj_builder.put_object("Test").unwrap();
inner_obj_builder.put_string("Foo", "Bar").unwrap();
let doc = builder.build();
assert_eq!(doc.root().query("Test.Foo").as_str().unwrap(), "Bar");Sourcepub fn insert_object(&mut self, key: Symbol) -> ObjectBuilder<'_>
pub fn insert_object(&mut self, key: Symbol) -> ObjectBuilder<'_>
Places an inner object within the object being built using a Symbolwhich has been looked
up previously. Returns the builder used to populate the list.
§Example
let mut builder = DocBuilder::new();
let key = builder.resolve("Test").unwrap();
let mut obj_builder = builder.root_object_builder();
let mut inner_obj_builder = obj_builder.insert_object(key);
inner_obj_builder.put_string("Foo", "Bar").unwrap();
let doc = builder.build();
assert_eq!(doc.root().query("Test.Foo").as_str().unwrap(), "Bar");