|
| 1 | +use quote::ToTokens; |
| 2 | +use syn::spanned::Spanned; |
| 3 | +use proc_macro2::Span; |
| 4 | + |
| 5 | +// do not derive debug since this needs "extra-traits" |
| 6 | +// feature for crate `syn`, which slows compile time |
| 7 | +// too much, and is not needed as this struct is not |
| 8 | +// public. |
| 9 | +pub struct Attribute { |
| 10 | + pub help: Option<syn::LitStr>, |
| 11 | + pub unit: Option<syn::LitStr>, |
| 12 | + pub rename: Option<syn::LitStr>, |
| 13 | + pub skip: bool, |
| 14 | +} |
| 15 | + |
| 16 | +impl Default for Attribute { |
| 17 | + fn default() -> Self { |
| 18 | + Attribute { |
| 19 | + help: None, |
| 20 | + unit: None, |
| 21 | + rename: None, |
| 22 | + skip: false, |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl Attribute { |
| 28 | + fn with_help(mut self, doc: syn::LitStr) -> Self { |
| 29 | + self.help = Some(doc); |
| 30 | + self |
| 31 | + } |
| 32 | + |
| 33 | + pub(super) fn merge(self, other: Self) -> syn::Result<Self> { |
| 34 | + let mut merged = self; |
| 35 | + |
| 36 | + if let Some(doc) = other.help { |
| 37 | + // trim leading and trailing whitespace |
| 38 | + // and add a space between the two doc strings |
| 39 | + let mut acc = merged |
| 40 | + .help |
| 41 | + .unwrap_or_else(|| syn::LitStr::new("", doc.span())) |
| 42 | + .value().trim() |
| 43 | + .to_string(); |
| 44 | + acc.push(' '); |
| 45 | + acc.push_str(&doc.value().trim()); |
| 46 | + merged.help = Some(syn::LitStr::new(&acc, Span::call_site())); |
| 47 | + } |
| 48 | + if let Some(unit) = other.unit { |
| 49 | + if merged.unit.is_some() { |
| 50 | + return Err(syn::Error::new_spanned( |
| 51 | + merged.unit, |
| 52 | + "Duplicate `unit` attribute", |
| 53 | + )); |
| 54 | + } |
| 55 | + |
| 56 | + merged.unit = Some(unit); |
| 57 | + } |
| 58 | + if let Some(rename) = other.rename { |
| 59 | + if merged.rename.is_some() { |
| 60 | + return Err(syn::Error::new_spanned( |
| 61 | + merged.rename, |
| 62 | + "Duplicate `rename` attribute", |
| 63 | + )); |
| 64 | + } |
| 65 | + |
| 66 | + merged.rename = Some(rename); |
| 67 | + } |
| 68 | + if other.skip { |
| 69 | + merged.skip = merged.skip || other.skip; |
| 70 | + } |
| 71 | + |
| 72 | + Ok(merged) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl syn::parse::Parse for Attribute { |
| 77 | + fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> { |
| 78 | + let meta = input.parse::<syn::Meta>()?; |
| 79 | + let span = meta.span(); |
| 80 | + |
| 81 | + match meta { |
| 82 | + syn::Meta::NameValue(meta) if meta.path.is_ident("doc") => { |
| 83 | + if let syn::Expr::Lit(lit) = meta.value { |
| 84 | + let lit_str = syn::parse2::<syn::LitStr>(lit.lit.to_token_stream())?; |
| 85 | + return Ok(Attribute::default().with_help(lit_str)); |
| 86 | + } else { |
| 87 | + return Err(syn::Error::new_spanned( |
| 88 | + meta.value, |
| 89 | + "Expected a string literal for doc attribute", |
| 90 | + )); |
| 91 | + } |
| 92 | + } |
| 93 | + syn::Meta::List(meta) if meta.path.is_ident("registrant") => { |
| 94 | + let mut attr = Attribute::default(); |
| 95 | + meta.parse_nested_meta(|meta| { |
| 96 | + if meta.path.is_ident("unit") { |
| 97 | + let unit = meta.value()?.parse::<syn::LitStr>()?; |
| 98 | + |
| 99 | + if attr.unit.is_some() { |
| 100 | + return Err(syn::Error::new( |
| 101 | + meta.path.span(), |
| 102 | + "Duplicate `unit` attribute", |
| 103 | + )); |
| 104 | + } |
| 105 | + |
| 106 | + // unit should be lowercase |
| 107 | + let unit = syn::LitStr::new( |
| 108 | + unit.value().as_str().to_ascii_lowercase().as_str(), |
| 109 | + unit.span(), |
| 110 | + ); |
| 111 | + attr.unit = Some(unit); |
| 112 | + } else if meta.path.is_ident("rename") { |
| 113 | + let rename = meta.value()?.parse::<syn::LitStr>()?; |
| 114 | + |
| 115 | + if attr.rename.is_some() { |
| 116 | + return Err(syn::Error::new( |
| 117 | + meta.path.span(), |
| 118 | + "Duplicate `rename` attribute", |
| 119 | + )); |
| 120 | + } |
| 121 | + |
| 122 | + attr.rename = Some(rename); |
| 123 | + } else if meta.path.is_ident("skip") { |
| 124 | + if attr.skip { |
| 125 | + return Err(syn::Error::new( |
| 126 | + meta.path.span(), |
| 127 | + "Duplicate `skip` attribute", |
| 128 | + )); |
| 129 | + } |
| 130 | + attr.skip = true; |
| 131 | + } else { |
| 132 | + panic!("Attributes other than `unit` and `rename` should not reach here"); |
| 133 | + } |
| 134 | + Ok(()) |
| 135 | + })?; |
| 136 | + Ok(attr) |
| 137 | + } |
| 138 | + _ => { |
| 139 | + return Err(syn::Error::new( |
| 140 | + span, |
| 141 | + r#"Unknown attribute, expected `#[doc(...)]` or `#[registrant(<key>[=value], ...)]`"#, |
| 142 | + )) |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments