Macro ion_rs::ion_struct
source · macro_rules! ion_struct { ($($field_name:tt : $element:expr),*) => { ... }; }
Expand description
Constructs an struct Element
with the specified fields.
For each field, the name must implement Into<Symbol>
and the value must implement
Into<Element>
.
use ion_rs::element::Element;
use ion_rs::{ion_struct, IonType};
let field_name_2 = "x";
let prefix = "abc";
let suffix = "def";
// Construct an s-expression Element from Rust values
let actual = ion_struct! {
"w": "foo",
// ^--- Quoted strings are field name literals
// v--- Unquoted field names are interpreted as variables
field_name_2: 7,
"y": false,
"z": ion_struct!{ "a": 1.5f64, "b": -8.25f64},
// Arbitrary expressions are acceptable, though some may require
// v--- an extra scope (braces: `{}`) to be understood properly.
{format!("{}_{}", prefix, suffix)}: IonType::Null
}
.into();
// Construct an Element from serialized Ion data
let expected = Element::read_one(
r#"{w: "foo", x: 7, y: false, z: {a: 1.5e0, b: -8.25e0}, abc_def: null}"#,
)
.unwrap();
// Compare the two Elements
assert_eq!(expected, actual);