Macro copy_selected_fields

Source
macro_rules! copy_selected_fields {
    ($selector:expr, $struct_name:ident { $($field:ident: $block:expr),* $(,)? }) => { ... };
}
Expand description

Copy selected fields from a source struct to a new struct, using provided blocks for enabled fields.

For each field, if it’s enabled in the field selector, the corresponding block is evaluated. Otherwise, the field is set to None (for Option fields) or a default value.

§Examples

let mut selector = user.serialize_fields();
selector.enable_dot_hierarchy("id");
selector.enable_dot_hierarchy("name");

#[derive(Debug)]
struct PartialUser {
    id: Option<u32>,
    name: Option<String>,
    email: Option<String>,
}

let partial = copy_selected_fields!(selector, PartialUser {
    id: Some(user.id),
    name: Some(user.name.clone()),
    email: user.email.clone()
});

assert_eq!(partial.id, Some(1));
assert_eq!(partial.name, Some("Alice".to_string()));
assert_eq!(partial.email, None); // Not enabled in selector