| 1 | use proc_macro::TokenStream; |
| 2 | use quote::quote; |
| 3 | use syn::parse::{Parse, ParseStream}; |
| 4 | use syn::punctuated::Punctuated; |
| 5 | use syn::spanned::Spanned; |
| 6 | use syn::{ |
| 7 | Error, Expr, ExprLit, ItemStruct, Lit, LitStr, Meta, MetaList, MetaNameValue, Path, Result, |
| 8 | Token, |
| 9 | }; |
| 10 | |
| 11 | enum SupportedFormat { |
| 12 | Full(Path), |
| 13 | ReadOnly(Path), |
| 14 | } |
| 15 | |
| 16 | impl SupportedFormat { |
| 17 | fn emit_doc_comment(&self) -> String { |
| 18 | match self { |
| 19 | SupportedFormat::Full(path) => format!( |
| 20 | "* [`FileType::{ft}`](crate::file::FileType::{ft})\n", |
| 21 | ft = path.get_ident().unwrap() |
| 22 | ), |
| 23 | SupportedFormat::ReadOnly(path) => format!( |
| 24 | "* [`FileType::{ft}`](crate::file::FileType::{ft}) **(READ ONLY)**\n", |
| 25 | ft = path.get_ident().unwrap() |
| 26 | ), |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | fn path(&self) -> &Path { |
| 31 | match self { |
| 32 | SupportedFormat::Full(path) | SupportedFormat::ReadOnly(path) => path, |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | fn read_only(&self) -> Option<&Path> { |
| 37 | match self { |
| 38 | SupportedFormat::ReadOnly(path) => Some(path), |
| 39 | _ => None, |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | pub(crate) struct LoftyTag { |
| 45 | attribute: LoftyTagAttribute, |
| 46 | input: ItemStruct, |
| 47 | } |
| 48 | |
| 49 | impl LoftyTag { |
| 50 | pub(crate) fn new(attribute: LoftyTagAttribute, input: ItemStruct) -> Self { |
| 51 | LoftyTag { attribute, input } |
| 52 | } |
| 53 | |
| 54 | pub(crate) fn emit(&self) -> TokenStream { |
| 55 | let ident = &self.input.ident; |
| 56 | let desc = &self.attribute.description; |
| 57 | |
| 58 | let supported_types_iter = self |
| 59 | .attribute |
| 60 | .supported_formats |
| 61 | .iter() |
| 62 | .map(SupportedFormat::emit_doc_comment); |
| 63 | let flattened_file_types = self |
| 64 | .attribute |
| 65 | .supported_formats |
| 66 | .iter() |
| 67 | .map(SupportedFormat::path); |
| 68 | let read_only_file_types = self |
| 69 | .attribute |
| 70 | .supported_formats |
| 71 | .iter() |
| 72 | .filter_map(SupportedFormat::read_only); |
| 73 | |
| 74 | let input = &self.input; |
| 75 | TokenStream::from(quote! { |
| 76 | use ::lofty::_this_is_internal; |
| 77 | |
| 78 | #[doc = #desc] |
| 79 | #[doc = "\n"] |
| 80 | #[doc = "## Supported file types\n\n"] |
| 81 | #( #[doc = #supported_types_iter] )* |
| 82 | #[doc = "\n"] |
| 83 | #input |
| 84 | |
| 85 | impl #ident { |
| 86 | pub(crate) const SUPPORTED_FORMATS: &'static [::lofty::file::FileType] = &[ |
| 87 | #( ::lofty::file::FileType:: #flattened_file_types ),* |
| 88 | ]; |
| 89 | |
| 90 | pub(crate) const READ_ONLY_FORMATS: &'static [::lofty::file::FileType] = &[ |
| 91 | #( ::lofty::file::FileType:: #read_only_file_types ),* |
| 92 | ]; |
| 93 | } |
| 94 | }) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | pub(crate) struct LoftyTagAttribute { |
| 99 | description: LitStr, |
| 100 | supported_formats: Vec<SupportedFormat>, |
| 101 | } |
| 102 | |
| 103 | impl Parse for LoftyTagAttribute { |
| 104 | fn parse(input: ParseStream<'_>) -> Result<Self> { |
| 105 | let mut description = None; |
| 106 | let mut supported_formats = Vec::new(); |
| 107 | |
| 108 | let start_span = input.span(); |
| 109 | |
| 110 | let args = Punctuated::<Meta, Token![,]>::parse_separated_nonempty(input)?; |
| 111 | for nested_meta in args { |
| 112 | match nested_meta { |
| 113 | Meta::NameValue(mnv) if mnv.path.is_ident("description") => { |
| 114 | if description.is_some() { |
| 115 | return Err(Error::new(mnv.span(), "Duplicate `description` entry")); |
| 116 | } |
| 117 | |
| 118 | description = Some(parse_description(mnv)?); |
| 119 | }, |
| 120 | Meta::List(list) if list.path.is_ident("supported_formats") => { |
| 121 | parse_supported_formats(list, &mut supported_formats)?; |
| 122 | }, |
| 123 | _ => { |
| 124 | return Err(Error::new( |
| 125 | nested_meta.span(), |
| 126 | "Unexpected input, check the format of the arguments", |
| 127 | )) |
| 128 | }, |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if description.is_none() { |
| 133 | return Err(Error::new(start_span, "No description provided")); |
| 134 | } |
| 135 | |
| 136 | Ok(Self { |
| 137 | description: description.unwrap(), |
| 138 | supported_formats, |
| 139 | }) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | fn parse_description(name_value: MetaNameValue) -> Result<LitStr> { |
| 144 | match name_value.value { |
| 145 | Expr::Lit(ExprLit { |
| 146 | lit: Lit::Str(lit_str), |
| 147 | .. |
| 148 | }) => Ok(lit_str), |
| 149 | _ => Err(Error::new( |
| 150 | name_value.span(), |
| 151 | "Invalid `description` entry, expected string value", |
| 152 | )), |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | fn parse_supported_formats( |
| 157 | meta_list: MetaList, |
| 158 | supported_formats: &mut Vec<SupportedFormat>, |
| 159 | ) -> Result<()> { |
| 160 | let mut read_only_encountered = false; |
| 161 | meta_list.parse_nested_meta(|meta| { |
| 162 | if meta.path.is_ident("read_only") { |
| 163 | if read_only_encountered { |
| 164 | return Err(meta.error("Duplicate `read_only` entry")); |
| 165 | } |
| 166 | |
| 167 | read_only_encountered = true; |
| 168 | |
| 169 | meta.parse_nested_meta(|nested_meta| { |
| 170 | supported_formats.push(SupportedFormat::ReadOnly(nested_meta.path)); |
| 171 | Ok(()) |
| 172 | })?; |
| 173 | } else { |
| 174 | supported_formats.push(SupportedFormat::Full(meta.path)); |
| 175 | } |
| 176 | |
| 177 | Ok(()) |
| 178 | }) |
| 179 | } |