macro_rules! selection {
( $s:expr, { $($n:ident),+ $(,)? } ) => { ... };
( $s:expr, [{ $($n:ident),+ $(,)? }] ) => { ... };
}Available on crate feature
serde only.Expand description
Specta compatible selection of struct fields.
use specta::{selection, ts::inline_ref};
#[derive(Clone)]
struct MyStruct {
name: String,
age: i32,
is_verified: bool,
password: String,
}
let person = MyStruct {
name: "Monty".into(),
age: 7,
is_verified: true,
password: "password".into(),
};
let people = vec![person.clone(), person.clone()];
// Selection creates an anonymous struct with the subset of fields you want.
assert_eq!(inline_ref(&selection!(person, {
name,
age
}), &Default::default()).unwrap(), "{ name: string; age: number }");
// You can apply the selection to an array.
assert_eq!(inline_ref(&selection!(people, [{
name,
age
}]), &Default::default()).unwrap(), "{ name: string; age: number }[]");