/// This test demonstrates flatten being used recursively. /// Fields are expected to be consumed by the outermost matching struct. #[test] fn recursive_flattening() { #[derive(FromMeta)] struct Nested2 {
above: isize,
below: isize,
port: Option<isize>,
}
assert_eq!(parsed.pos.cross_section.z_axis.above, 20);
assert_eq!(parsed.pos.cross_section.z_axis.below, -3); // This should be `None` because the `port` field in `Nested1` consumed // the field before the leftovers were passed to `Nested2::from_list`.
assert_eq!(parsed.pos.cross_section.z_axis.port, None);
}
/// This test confirms that a collection - in this case a HashMap - can /// be used with `flatten`. #[test] fn flattening_into_hashmap() { #[derive(FromDeriveInput)] #[darling(attributes(ca))] struct Catchall {
hello: String,
volume: usize, #[darling(flatten)]
others: std::collections::HashMap<String, String>,
}
/// This test makes sure that field names from parent structs are not inappropriately /// offered as alternates for unknown field errors in child structs. /// /// A naive implementation that tried to offer all the flattened fields for "did you mean" /// could inspect all errors returned by the flattened field's `from_list` call and add the /// parent's field names as alternates to all unknown field errors. /// /// THIS WOULD BE INCORRECT. Those unknown field errors may have already come from /// child fields within the flattened struct, where the parent's field names are not valid. #[test] fn do_not_suggest_invalid_alts() { let errors = Outer::from_derive_input(&parse_quote! { #[v(first = "Hello", last = "World", parent(first = "Hi", last = "Earth", blasts = "off"))] struct Demo;
})
.map(|_| "Should have failed")
.unwrap_err()
.to_string();
assert!(
!errors.contains("`blast`"), "Should not contain `blast`: {}",
errors
);
}
#[test] #[cfg(feature = "suggestions")] fn suggest_valid_parent_alts() { let errors = Outer::from_derive_input(&parse_quote! { #[v(first = "Hello", bladt = false, last = "World", parent(first = "Hi", last = "Earth"))] struct Demo;
})
.map(|_| "Should have failed")
.unwrap_err()
.to_string();
assert!(
errors.contains("`blast`"), "Should contain `blast` as did-you-mean suggestion: {}",
errors
);
}
/// Make sure that flatten works with smart pointer types, e.g. `Box`. /// /// The generated `flatten` impl directly calls `FromMeta::from_list` /// rather than calling `from_meta`, and the default impl of `from_list` /// will return an unsupported format error; this test ensures that the /// smart pointer type is properly forwarding the `from_list` call. #[test] fn flattening_to_box() { #[derive(FromDeriveInput)] #[darling(attributes(v))] struct Example { #[darling(flatten)]
items: Box<Vis>,
}
let when_omitted = Example::from_derive_input(&parse_quote! { struct Demo;
})
.unwrap();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.