diff options
| author | HampusM <hampus@hampusmat.com> | 2022-07-31 13:26:41 +0200 | 
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2022-08-01 15:52:23 +0200 | 
| commit | 3383faeaf8342cf4637b6d9a9dfba30b1684edca (patch) | |
| tree | 3a4e23d299d077eaf82cb09a093eaaae384e4ec2 /macros/src/injectable_macro_args.rs | |
| parent | 8d2c6412fec2f35581a48433421db4b03a8d6657 (diff) | |
feat: add hide impl of Injectable from documentation
This will make it so that by default the impl of Injectable is hidden from user code documentation. This commit also includes a flag for the injectable macro to disable the aforementioned feature
Diffstat (limited to 'macros/src/injectable_macro_args.rs')
| -rw-r--r-- | macros/src/injectable_macro_args.rs | 85 | 
1 files changed, 80 insertions, 5 deletions
diff --git a/macros/src/injectable_macro_args.rs b/macros/src/injectable_macro_args.rs index 4ef4389..43f8e11 100644 --- a/macros/src/injectable_macro_args.rs +++ b/macros/src/injectable_macro_args.rs @@ -1,17 +1,92 @@  use syn::parse::{Parse, ParseStream}; -use syn::TypePath; +use syn::punctuated::Punctuated; +use syn::{braced, Ident, LitBool, Token, TypePath}; + +use crate::util::iterator_ext::IteratorExt; + +pub const INJECTABLE_MACRO_FLAGS: &[&str] = &["no_doc_hidden"]; + +pub struct InjectableMacroFlag +{ +    pub flag: Ident, +    pub is_on: LitBool, +} + +impl Parse for InjectableMacroFlag +{ +    fn parse(input: ParseStream) -> syn::Result<Self> +    { +        let input_forked = input.fork(); + +        let flag: Ident = input_forked.parse()?; + +        let flag_str = flag.to_string(); + +        if !INJECTABLE_MACRO_FLAGS.contains(&flag_str.as_str()) { +            return Err(input.error(format!( +                "Unknown flag '{}'. Expected one of [ {} ]", +                flag_str, +                INJECTABLE_MACRO_FLAGS.join(",") +            ))); +        } + +        input.parse::<Ident>()?; + +        input.parse::<Token![=]>()?; + +        let is_on: LitBool = input.parse()?; + +        Ok(Self { flag, is_on }) +    } +}  pub struct InjectableMacroArgs  { -    pub interface: TypePath, +    pub interface: Option<TypePath>, +    pub flags: Punctuated<InjectableMacroFlag, Token![,]>,  }  impl Parse for InjectableMacroArgs  {      fn parse(input: ParseStream) -> syn::Result<Self>      { -        Ok(Self { -            interface: input.parse()?, -        }) +        let interface = input.parse::<TypePath>().ok(); + +        if interface.is_some() { +            let comma_input_lookahead = input.lookahead1(); + +            if !comma_input_lookahead.peek(Token![,]) { +                return Ok(Self { +                    interface, +                    flags: Punctuated::new(), +                }); +            } + +            input.parse::<Token![,]>()?; +        } + +        if input.is_empty() { +            return Ok(Self { +                interface, +                flags: Punctuated::new(), +            }); +        } + +        let braced_content; + +        braced!(braced_content in input); + +        let flags = braced_content.parse_terminated(InjectableMacroFlag::parse)?; + +        let flag_names = flags +            .iter() +            .map(|flag| flag.flag.to_string()) +            .collect::<Vec<_>>(); + +        if let Some(dupe_flag_name) = flag_names.iter().find_duplicate() { +            return Err(input.error(format!("Duplicate flag '{}'", dupe_flag_name))); +        } + +        Ok(Self { interface, flags })      }  }  | 
