| 1 | use quote::ToTokens; |
| 2 | use syn::{spanned::Spanned, Expr, Lit, LitStr, Meta, MetaNameValue, Path}; |
| 3 | |
| 4 | #[inline] |
| 5 | pub(crate) fn meta_name_value_2_path(name_value: &MetaNameValue) -> syn::Result<Path> { |
| 6 | match &name_value.value { |
| 7 | Expr::Lit(lit) => { |
| 8 | if let Lit::Str(lit) = &lit.lit { |
| 9 | return lit.parse(); |
| 10 | } |
| 11 | }, |
| 12 | Expr::Path(path) => return Ok(path.path.clone()), |
| 13 | _ => (), |
| 14 | } |
| 15 | |
| 16 | Err(syn::Error::new( |
| 17 | name_value.value.span(), |
| 18 | format!("expected `{path} = Path`", path = path_to_string(&name_value.path)), |
| 19 | )) |
| 20 | } |
| 21 | |
| 22 | #[inline] |
| 23 | pub(crate) fn meta_2_path(meta: &Meta) -> syn::Result<Path> { |
| 24 | match &meta { |
| 25 | Meta::NameValue(name_value) => meta_name_value_2_path(name_value), |
| 26 | Meta::List(list) => { |
| 27 | if let Ok(lit) = list.parse_args::<LitStr>() { |
| 28 | lit.parse() |
| 29 | } else { |
| 30 | list.parse_args() |
| 31 | } |
| 32 | }, |
| 33 | Meta::Path(path) => Err(syn::Error::new( |
| 34 | path.span(), |
| 35 | format!("expected `{path} = Path` or `{path}(Path)`", path = path_to_string(path)), |
| 36 | )), |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | #[inline] |
| 41 | pub(crate) fn path_to_string(path: &Path) -> String { |
| 42 | path.into_token_stream().to_string().replace(' ', "") |
| 43 | } |