|
| 1 | +#![allow(unused)] |
| 2 | +use std::fmt::Debug; |
| 3 | + |
| 4 | +use attribute_derive::{Attribute, AttributeIdent}; |
| 5 | +use proc_macro::TokenStream; |
| 6 | +use syn::{Block, DeriveInput, Result}; |
| 7 | + |
| 8 | +// #[derive(Attribute)] |
| 9 | +// #[attribute(ident = "positional")] |
| 10 | +// struct PositionalAttr { |
| 11 | +// #[attribute(positional)] |
| 12 | +// a: u8, |
| 13 | +// #[attribute(positional)] |
| 14 | +// b: String, |
| 15 | +// #[attribute(positional)] |
| 16 | +// c: bool, |
| 17 | +// } |
| 18 | +// |
| 19 | +// #[proc_macro_derive(Positional, attributes(attribute))] |
| 20 | +// pub fn positional_derive(input: TokenStream) -> proc_macro::TokenStream { |
| 21 | +// all_attrs::<PositionalAttr>(input).unwrap_or_else(|e| |
| 22 | +// e.to_compile_error().into()) } |
| 23 | +#[derive(Attribute)] |
| 24 | +#[attribute(ident = empty)] |
| 25 | +struct Empty {} |
| 26 | + |
| 27 | +#[derive(Attribute)] |
| 28 | +#[attribute(ident = single)] |
| 29 | +struct Single { |
| 30 | + field: bool, |
| 31 | +} |
| 32 | + |
| 33 | +#[derive(Attribute, Debug)] |
| 34 | +#[attribute(ident = ident, aliases = [a, b])] |
| 35 | +struct Normal { |
| 36 | + optional_implicit: Option<u8>, |
| 37 | + #[attribute(optional)] |
| 38 | + optional_explicit: u8, |
| 39 | + #[attribute(optional, default = 2 * 5)] |
| 40 | + optional_default: u8, |
| 41 | + #[attribute(default = 33)] |
| 42 | + default: u8, |
| 43 | + #[attribute(conflicts = [conflict_b])] |
| 44 | + conflict_a: Option<String>, |
| 45 | + conflict_b: Option<String>, |
| 46 | + #[attribute(example = "2.5")] |
| 47 | + example: f32, |
| 48 | + flag: bool, |
| 49 | + #[attribute(optional = false)] |
| 50 | + mandatory_flag: bool, |
| 51 | +} |
| 52 | +#[proc_macro_derive(Normal, attributes(ident, a, b, empty, single))] |
| 53 | +pub fn normal_derive(input: TokenStream) -> proc_macro::TokenStream { |
| 54 | + let mut tokens = |
| 55 | + all_attrs::<Normal>(input.clone()).unwrap_or_else(|e| e.to_compile_error().into()); |
| 56 | + tokens |
| 57 | + .extend(all_attrs::<Empty>(input.clone()).unwrap_or_else(|e| e.to_compile_error().into())); |
| 58 | + tokens.extend(all_attrs::<Single>(input).unwrap_or_else(|e| e.to_compile_error().into())); |
| 59 | + tokens |
| 60 | +} |
| 61 | + |
| 62 | +#[derive(Attribute, Debug)] |
| 63 | +#[attribute(ident = ident, aliases = [a, b])] |
| 64 | +#[attribute(error( |
| 65 | + unknown_field = "found `{found_field}` but expected one of {expected_fields:i(`{}`)(, )}", |
| 66 | + duplicate_field = "duplicate `{field}`", |
| 67 | + missing_field = "missing field `{field}`", |
| 68 | + field_help = "try {attribute}: {field}={example}", |
| 69 | + missing_flag = "missing flag `{flag}`", |
| 70 | + flag_help = "try {attribute}: {flag}", |
| 71 | + conflict = "{first} !!! {second}" |
| 72 | +))] |
| 73 | +struct Custom { |
| 74 | + optional_implicit: Option<Block>, |
| 75 | + #[attribute(optional)] |
| 76 | + optional_explicit: u8, |
| 77 | + #[attribute(optional, default = 2 * 5)] |
| 78 | + optional_default: u8, |
| 79 | + #[attribute(default = 33)] |
| 80 | + default: u8, |
| 81 | + #[attribute(conflicts = [conflict_b])] |
| 82 | + conflict_a: Option<String>, |
| 83 | + conflict_b: Option<String>, |
| 84 | + #[attribute(example = "2.5")] |
| 85 | + example: f32, |
| 86 | + flag: bool, |
| 87 | + #[attribute(optional = false)] |
| 88 | + mandatory_flag: bool, |
| 89 | +} |
| 90 | +#[derive(Attribute)] |
| 91 | +#[attribute(ident = empty, error(unknown_field_empty = "found {found_field}, but expected none"))] |
| 92 | +struct EmptyCustom {} |
| 93 | + |
| 94 | +#[derive(Attribute)] |
| 95 | +#[attribute(ident = single, error(unknown_field_single = "found {found_field}, but expected {expected_field}"))] |
| 96 | +struct SingleCustom { |
| 97 | + field: bool, |
| 98 | +} |
| 99 | + |
| 100 | +#[proc_macro_derive(Custom, attributes(ident, a, b, empty, single))] |
| 101 | +pub fn custom_derive(input: TokenStream) -> proc_macro::TokenStream { |
| 102 | + let mut tokens = |
| 103 | + all_attrs::<Custom>(input.clone()).unwrap_or_else(|e| e.to_compile_error().into()); |
| 104 | + tokens.extend( |
| 105 | + all_attrs::<EmptyCustom>(input.clone()).unwrap_or_else(|e| e.to_compile_error().into()), |
| 106 | + ); |
| 107 | + tokens.extend(all_attrs::<SingleCustom>(input).unwrap_or_else(|e| e.to_compile_error().into())); |
| 108 | + tokens |
| 109 | +} |
| 110 | + |
| 111 | +fn all_attrs<T: Attribute + AttributeIdent>(input: TokenStream) -> Result<TokenStream> { |
| 112 | + let DeriveInput { attrs, data, .. } = syn::parse(input)?; |
| 113 | + T::from_attributes(&attrs)?; |
| 114 | + match data { |
| 115 | + syn::Data::Struct(data) => { |
| 116 | + for field in data.fields { |
| 117 | + T::from_attributes(&field.attrs)?; |
| 118 | + } |
| 119 | + } |
| 120 | + syn::Data::Enum(data) => { |
| 121 | + for variant in data.variants { |
| 122 | + T::from_attributes(&variant.attrs)?; |
| 123 | + for field in variant.fields { |
| 124 | + T::from_attributes(&field.attrs)?; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + syn::Data::Union(data) => { |
| 129 | + for field in data.fields.named { |
| 130 | + T::from_attributes(&field.attrs)?; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + Ok(TokenStream::new()) |
| 135 | +} |
0 commit comments