summaryrefslogtreecommitdiff
path: root/macros/src/util.rs
diff options
context:
space:
mode:
Diffstat (limited to 'macros/src/util.rs')
-rw-r--r--macros/src/util.rs40
1 files changed, 24 insertions, 16 deletions
diff --git a/macros/src/util.rs b/macros/src/util.rs
index 2e12ef17..389fed06 100644
--- a/macros/src/util.rs
+++ b/macros/src/util.rs
@@ -5,16 +5,16 @@ use super::*;
/// Return an error at the given item.
macro_rules! bail {
- (callsite, $fmt:literal $($tts:tt)*) => {
+ (callsite, $($tts:tt)*) => {
return Err(syn::Error::new(
proc_macro2::Span::call_site(),
- format!(concat!("typst: ", $fmt) $($tts)*)
+ format!("typst: {}", format!($($tts)*))
))
};
- ($item:expr, $fmt:literal $($tts:tt)*) => {
+ ($item:expr, $($tts:tt)*) => {
return Err(syn::Error::new_spanned(
&$item,
- format!(concat!("typst: ", $fmt) $($tts)*)
+ format!("typst: {}", format!($($tts)*))
))
};
}
@@ -51,7 +51,13 @@ pub fn parse_attr<T: Parse>(
target: &str,
) -> Result<Option<Option<T>>> {
take_attr(attrs, target)
- .map(|attr| (!attr.tokens.is_empty()).then(|| attr.parse_args()).transpose())
+ .map(|attr| {
+ Ok(match attr.meta {
+ syn::Meta::Path(_) => None,
+ syn::Meta::List(list) => Some(list.parse_args()?),
+ syn::Meta::NameValue(meta) => bail!(meta, "not valid here"),
+ })
+ })
.transpose()
}
@@ -62,16 +68,16 @@ pub fn take_attr(
) -> Option<syn::Attribute> {
attrs
.iter()
- .position(|attr| attr.path.is_ident(target))
+ .position(|attr| attr.path().is_ident(target))
.map(|i| attrs.remove(i))
}
/// Ensure that no unrecognized attributes remain.
pub fn validate_attrs(attrs: &[syn::Attribute]) -> Result<()> {
for attr in attrs {
- if !attr.path.is_ident("doc") {
- let ident = attr.path.get_ident().unwrap();
- bail!(ident, "unrecognized attribute: {:?}", ident.to_string());
+ if !attr.path().is_ident("doc") && !attr.path().is_ident("derive") {
+ let ident = attr.path().get_ident().unwrap();
+ bail!(ident, "unrecognized attribute: {ident}");
}
}
Ok(())
@@ -88,13 +94,15 @@ pub fn documentation(attrs: &[syn::Attribute]) -> String {
// Parse doc comments.
for attr in attrs {
- if let Ok(syn::Meta::NameValue(meta)) = attr.parse_meta() {
+ if let syn::Meta::NameValue(meta) = &attr.meta {
if meta.path.is_ident("doc") {
- if let syn::Lit::Str(string) = &meta.lit {
- let full = string.value();
- let line = full.strip_prefix(' ').unwrap_or(&full);
- doc.push_str(line);
- doc.push('\n');
+ if let syn::Expr::Lit(lit) = &meta.value {
+ if let syn::Lit::Str(string) = &lit.lit {
+ let full = string.value();
+ let line = full.strip_prefix(' ').unwrap_or(&full);
+ doc.push_str(line);
+ doc.push('\n');
+ }
}
}
}
@@ -110,7 +118,7 @@ pub fn meta_line<'a>(lines: &mut Vec<&'a str>, key: &str) -> Result<&'a str> {
lines.pop();
Ok(value.trim())
}
- None => bail!(callsite, "missing metadata key: {}", key),
+ None => bail!(callsite, "missing metadata key: {key}"),
}
}