pub struct StructBuilder { /* private fields */ }
Expand description

Constructs Struct values incrementally.

use ion_rs::element::Element;
use ion_rs::ion_struct;
let actual: Element = ion_struct! {
    "a": 1,
    "b": true,
    "c": "foo"
}
.into();
let expected = Element::read_one(r#"{a: 1, b: true, c: "foo"}"#).unwrap();
assert_eq!(actual, expected);
use ion_rs::element::{Element, Struct};
use ion_rs::ion_struct;
let base_struct: Struct = ion_struct! {
    "foo": 1,
    "bar": 2,
    "baz": 3
};

let modified_struct: Element = base_struct
    .clone_builder()
    .remove_field("bar")
    .with_field("quux", 4)
    .build()
    .into(); // Convert from `Struct` to `Element`

let expected = Element::read_one(r#"{foo: 1, baz: 3, quux: 4}"#).unwrap();
assert_eq!(expected, modified_struct);

Implementations§

source§

impl StructBuilder

source

pub fn with_field<S: Into<Symbol>, E: Into<Element>>( self, field_name: S, field_value: E ) -> Self

Adds the provided (name, value) pair to the Struct being constructed.

source

pub fn with_fields<S, E, I>(self, fields: I) -> Selfwhere S: Into<Symbol>, E: Into<Element>, I: IntoIterator<Item = (S, E)>,

Adds all of the provided (name, value) pairs to the Struct being constructed.

use ion_rs::element::{Element, Struct};
use ion_rs::ion_struct;

let struct1 = ion_struct! {
    "foo": 1,
    "bar": 2,
    "baz": 3
};

let struct2 = ion_struct! {
    "a": 4,
    "b": 5,
    "c": 6
};

let merged = struct1
    .clone_builder()
    .with_fields(struct2.fields())
    .build();

let expected = Element::read_one("{foo: 1, bar: 2, baz: 3, a: 4, b: 5, c: 6}").unwrap();
source

pub fn remove_field<A: AsRef<str>>(self, field_to_remove: A) -> Self

Removes the first field with the specified name from the Struct being constructed.

source

pub fn build(self) -> Struct

Builds a Struct with the previously specified fields.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.