/// A struct or enum which should have `FromMeta` or `FromDeriveInput` implementations /// generated. #[derive(Debug, Clone)] pubstruct Core { /// The type identifier. pub ident: syn::Ident,
/// The type's generics. If the type does not use any generics, this will /// be an empty instance. pub generics: syn::Generics,
/// Controls whether missing properties should cause errors or should be filled by /// the result of a function call. This can be overridden at the field level. pub default: Option<DefaultExpression>,
/// The rule that should be used to rename all fields/variants in the container. pub rename_rule: RenameRule,
/// A transform which will be called on `darling::Result<Self>`. It must either be /// an `FnOnce(T) -> T` when `map` is used, or `FnOnce(T) -> darling::Result<T>` when /// `and_then` is used. /// /// `map` and `and_then` are mutually-exclusive to avoid confusion about the order in /// which the two are applied. pub post_transform: Option<codegen::PostfixTransform>,
/// The body of the _deriving_ type. pub data: Data<InputVariant, InputField>,
/// The custom bound to apply to the generated impl pub bound: Option<Vec<syn::WherePredicate>>,
/// Whether or not unknown fields should produce an error at compilation time. pub allow_unknown_fields: Option<bool>,
}
impl Core { /// Partially initializes `Core` by reading the identity, generics, and body shape. pubfn start(di: &syn::DeriveInput) -> Result<Self> {
Ok(Core {
ident: di.ident.clone(),
generics: di.generics.clone(),
data: Data::try_empty_from(&di.data)?,
default: Default::default(), // See https://github.com/TedDriggs/darling/issues/10: We default to snake_case // for enums to help authors produce more idiomatic APIs.
rename_rule: iflet syn::Data::Enum(_) = di.data {
RenameRule::SnakeCase
} else {
Default::default()
},
post_transform: Default::default(),
bound: Default::default(),
allow_unknown_fields: Default::default(),
})
}
fn as_codegen_default(&self) -> Option<codegen::DefaultExpression<'_>> { self.default.as_ref().map(|expr| match *expr {
DefaultExpression::Explicit(ref path) => codegen::DefaultExpression::Explicit(path),
DefaultExpression::Inherit => { // It should be impossible for any input to get here, // so panic rather than returning an error or pretending // everything is fine.
panic!("DefaultExpression::Inherit is not valid at container level")
}
DefaultExpression::Trait { span } => codegen::DefaultExpression::Trait { span },
})
}
}
impl ParseAttribute for Core { fn parse_nested(&mutself, mi: &syn::Meta) -> Result<()> { let path = mi.path();
if path.is_ident("default") { ifself.default.is_some() { return Err(Error::duplicate_field("default").with_span(mi));
}
self.default = FromMeta::from_meta(mi)?;
} elseif path.is_ident("rename_all") { // WARNING: This may have been set based on body shape previously, // so an overwrite may be permissible. self.rename_rule = FromMeta::from_meta(mi)?;
} elseif path.is_ident("map") || path.is_ident("and_then") { // This unwrap is safe because we just called is_ident above let transformer = path.get_ident().unwrap().clone();
iflet Some(post_transform) = &self.post_transform { if transformer == post_transform.transformer { return Err(Error::duplicate_field(&transformer.to_string()).with_span(mi));
} else { return Err(Error::custom(format!( "Options `{}` and `{}` are mutually exclusive",
transformer, post_transform.transformer
))
.with_span(mi));
}
}
impl ParseData for Core { fn parse_variant(&mutself, variant: &syn::Variant) -> Result<()> { let v = InputVariant::from_variant(variant, Some(self))?;
matchself.data {
Data::Enum(refmut variants) => {
variants.push(v);
Ok(())
}
Data::Struct(_) => panic!("Core::parse_variant should never be called for a struct"),
}
}
fn parse_field(&mutself, field: &syn::Field) -> Result<()> { let f = InputField::from_field(field, Some(self))?;
matchself.data {
Data::Struct(Fields {
style: Style::Unit, ..
}) => panic!("Core::parse_field should not be called on unit"),
Data::Struct(Fields { refmut fields, .. }) => {
fields.push(f);
Ok(())
}
Data::Enum(_) => panic!("Core::parse_field should never be called for an enum"),
}
}
if flatten_targets.len() > 1 { for flatten in flatten_targets {
errors.push(
Error::custom("`#[darling(flatten)]` can only be applied to one field")
.with_span(&flatten.span()),
);
}
}
}
}
}
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.