pubfn enum_table_inner(ast: &DeriveInput) -> syn::Result<TokenStream> { let name = &ast.ident; letgen = &ast.generics; let vis = &ast.vis; letmut doc_comment = format!("A map over the variants of `{}`", name);
ifgen.lifetimes().count() > 0 { return Err(syn::Error::new(
Span::call_site(), "`EnumTable` doesn't support enums with lifetimes.",
));
}
let variants = match &ast.data {
Data::Enum(v) => &v.variants,
_ => return Err(non_enum_error()),
};
let table_name = format_ident!("{}Table", name);
// the identifiers of each variant, in PascalCase letmut pascal_idents = Vec::new(); // the identifiers of each struct field, in snake_case letmut snake_idents = Vec::new(); // match arms in the form `MyEnumTable::Variant => &self.variant,` letmut get_matches = Vec::new(); // match arms in the form `MyEnumTable::Variant => &mut self.variant,` letmut get_matches_mut = Vec::new(); // match arms in the form `MyEnumTable::Variant => self.variant = new_value` letmut set_matches = Vec::new(); // struct fields of the form `variant: func(MyEnum::Variant),* letmut closure_fields = Vec::new(); // struct fields of the form `variant: func(MyEnum::Variant, self.variant),` letmut transform_fields = Vec::new();
// identifiers for disabled variants letmut disabled_variants = Vec::new(); // match arms for disabled variants letmut disabled_matches = Vec::new();
for variant in variants { // skip disabled variants if variant.get_variant_properties()?.disabled.is_some() { let disabled_ident = &variant.ident; let panic_message = format!( "Can't use `{}` with `{}` - variant is disabled for Strum features",
disabled_ident, table_name
);
disabled_variants.push(disabled_ident);
disabled_matches.push(quote!(#name::#disabled_ident => panic!(#panic_message),)); continue;
}
// Error on variants with data if variant.fields != Fields::Unit { return Err(syn::Error::new(
variant.fields.span(), "`EnumTable` doesn't support enums with non-unit variants",
));
};
let pascal_case = &variant.ident; let snake_case = format_ident!("_{}", snakify(&pascal_case.to_string()));
// Error on empty enums if pascal_idents.is_empty() { return Err(syn::Error::new(
variants.span(), "`EnumTable` requires at least one non-disabled variant",
));
}
// if the index operation can panic, add that to the documentation if !disabled_variants.is_empty() {
doc_comment.push_str(&format!( "\n# Panics\nIndexing `{}` with any of the following variants will cause a panic:",
table_name
)); for variant in disabled_variants {
doc_comment.push_str(&format!("\n\n- `{}::{}`", name, variant));
}
}
let doc_new = format!( "Create a new {} with a value for each variant of {}",
table_name, name
); let doc_closure = format!( "Create a new {} by running a function on each variant of `{}`",
table_name, name
); let doc_transform = format!("Create a new `{}` by running a function on each variant of `{}` and the corresponding value in the current `{0}`", table_name, name); let doc_filled = format!( "Create a new `{}` with the same value in each field.",
table_name
); let doc_option_all = format!("Converts `{}<Option<T>>` into `Option<{0}<T>>`. Returns `Some` if all fields are `Some`, otherwise returns `None`.", table_name); let doc_result_all_ok = format!("Converts `{}<Result<T, E>>` into `Result<{0}<T>, E>`. Returns `Ok` if all fields are `Ok`, otherwise returns `Err`.", table_name);
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.