From 5bd997577313756761e67318e15a14d59ac2ebc5 Mon Sep 17 00:00:00 2001 From: Nova Date: Wed, 16 Oct 2024 02:17:03 -0500 Subject: [PATCH] Update dependencies, use syn 2.0 (#74) * Upgrade deps * Update test deps * Update deps and cleanup --- ramhorns-derive/Cargo.toml | 5 +-- ramhorns-derive/src/lib.rs | 89 +++++++++++++++++++++++++++++++++----- ramhorns/Cargo.toml | 4 +- ramhorns/src/content.rs | 4 +- ramhorns/src/encoding.rs | 2 +- tests/Cargo.toml | 8 ++-- 6 files changed, 89 insertions(+), 23 deletions(-) diff --git a/ramhorns-derive/Cargo.toml b/ramhorns-derive/Cargo.toml index ba96a7b..d7d7894 100644 --- a/ramhorns-derive/Cargo.toml +++ b/ramhorns-derive/Cargo.toml @@ -14,9 +14,8 @@ name = "ramhorns_derive" proc-macro = true [dependencies] -bae = "0.1.7" fnv = "1.0" -syn = { version = "1.0", features = ["full"] } +syn = { version = "2.0", features = ["full"] } quote = "1.0" proc-macro2 = "1.0" -heck = "0.4" +heck = "0.5" diff --git a/ramhorns-derive/src/lib.rs b/ramhorns-derive/src/lib.rs index 862e93e..328de22 100644 --- a/ramhorns-derive/src/lib.rs +++ b/ramhorns-derive/src/lib.rs @@ -13,7 +13,6 @@ extern crate proc_macro; -use bae::FromAttributes; use fnv::FnvHasher; use proc_macro::TokenStream; use proc_macro2::{Span, TokenStream as TokenStream2}; @@ -54,16 +53,80 @@ impl Ord for Field { } } -#[derive(FromAttributes)] struct Ramhorns { - skip: Option<()>, - md: Option<()>, - flatten: Option<()>, + skip: bool, + md: bool, + flatten: bool, rename: Option, rename_all: Option, callback: Option, } +impl Ramhorns { + pub fn try_from_attributes(attrs: &[syn::Attribute]) -> syn::Result> { + for attr in attrs { + if let syn::Meta::List(ref list) = attr.meta { + if matches!(list.path.get_ident(), Some(path) if path == "ramhorns") { + return Some(syn::parse2::(list.tokens.clone())).transpose(); + } + } + } + + Ok(None) + } +} + +impl syn::parse::Parse for Ramhorns { + #[allow(unreachable_code, unused_imports, unused_variables)] + fn parse(input: syn::parse::ParseStream) -> syn::Result { + let mut skip = false; + let mut md = false; + let mut flatten = false; + let mut rename = None; + let mut rename_all = None; + let mut callback = None; + + while !input.is_empty() { + let attr_ident = input.parse::()?; + + match &*attr_ident.to_string() { + "skip" => skip = true, + "md" => md = true, + "flatten" => flatten = true, + "rename" => { + input.parse::()?; + rename = Some(input.parse()?); + } + "rename_all" => { + input.parse::()?; + rename_all = Some(input.parse()?); + } + "callback" => { + input.parse::()?; + callback = Some(input.parse()?); + } + other => { + return Err(syn::Error::new( + attr_ident.span(), + format!("`ramhorns` got unknown `{other}` argument. Supported arguments are `callback`, `flatten`, `md`, `rename_all`, `rename`, `skip`"), + )); + } + } + + input.parse::().ok(); + } + syn::Result::Ok(Self { + skip, + md, + flatten, + rename, + rename_all, + callback, + }) + } +} + +#[allow(clippy::enum_variant_names)] enum RenameAll { UpperCase, PascalCase, @@ -73,6 +136,7 @@ enum RenameAll { KebabCase, ScreamingKebabCase, } + impl TryFrom for RenameAll { type Error = syn::Error; @@ -91,6 +155,7 @@ impl TryFrom for RenameAll { }) } } + impl RenameAll { fn rename(&self, input: &str) -> String { match &self { @@ -154,13 +219,13 @@ pub fn content_derive(input: TokenStream) -> TokenStream { match Ramhorns::try_from_attributes(&field.attrs) { Ok(Some(ramhorns)) => { - if ramhorns.skip.is_some() { - skip = true; - } - if ramhorns.md.is_some() { + skip = ramhorns.skip; + + if ramhorns.md { callback = Some(md_callback.clone()); } - if ramhorns.flatten.is_some() { + + if ramhorns.flatten { flatten.push(field.ident.as_ref().map_or_else( || { let index = index.to_string(); @@ -171,9 +236,11 @@ pub fn content_derive(input: TokenStream) -> TokenStream { )); skip = true; } + if let Some(lit_str) = ramhorns.rename { rename = Some(lit_str.value()); } + if let Some(path) = ramhorns.callback { callback = Some(path); } @@ -294,7 +361,7 @@ pub fn content_derive(input: TokenStream) -> TokenStream { // FIXME: decouple lifetimes from actual generics with trait boundaries let tokens = quote! { - impl#generics ::ramhorns::Content for #name#generics #where_clause { + impl #generics ::ramhorns::Content for #name #generics #where_clause { #[inline] fn capacity_hint(&self, tpl: &::ramhorns::Template) -> usize { tpl.capacity_hint() #( + self.#fields.capacity_hint(tpl) )* diff --git a/ramhorns/Cargo.toml b/ramhorns/Cargo.toml index 4b8608f..ff2b7f1 100644 --- a/ramhorns/Cargo.toml +++ b/ramhorns/Cargo.toml @@ -15,12 +15,12 @@ categories = ["template-engine"] arrayvec = "0.7.4" beef = "0.5.2" fnv = "1.0" -pulldown-cmark = { version = "0.10", default_features = false, features = ["html"], optional = true } +pulldown-cmark = { version = "0.12", default_features = false, features = ["html"], optional = true } ramhorns-derive = { version = "1.0.0", path = "../ramhorns-derive", optional = true } logos = "0.14.0" [dev-dependencies] -pretty_assertions = "0.6.1" +pretty_assertions = "1.4" [features] default = ["export_derive", "pulldown-cmark"] diff --git a/ramhorns/src/content.rs b/ramhorns/src/content.rs index 1eb4101..7ba8a19 100644 --- a/ramhorns/src/content.rs +++ b/ramhorns/src/content.rs @@ -255,7 +255,7 @@ impl Content for f32 { #[inline] fn is_truthy(&self) -> bool { // Floats shoudn't be directly compared to 0 - self.abs() > std::f32::EPSILON + self.abs() > f32::EPSILON } #[inline] @@ -274,7 +274,7 @@ impl Content for f64 { #[inline] fn is_truthy(&self) -> bool { // Floats shoudn't be directly compared to 0 - self.abs() > std::f64::EPSILON + self.abs() > f64::EPSILON } #[inline] diff --git a/ramhorns/src/encoding.rs b/ramhorns/src/encoding.rs index 0296855..95342b2 100644 --- a/ramhorns/src/encoding.rs +++ b/ramhorns/src/encoding.rs @@ -144,7 +144,7 @@ impl Encoder for EscapingIOEncoder { #[cfg(feature = "pulldown-cmark")] #[inline] fn write_html<'a, I: Iterator>>(&mut self, iter: I) -> io::Result<()> { - html::write_html(&mut self.inner, iter) + html::write_html_io(&mut self.inner, iter) } #[inline] diff --git a/tests/Cargo.toml b/tests/Cargo.toml index ce529df..7cde04c 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -8,13 +8,13 @@ edition = "2018" [dev-dependencies] ramhorns = { path = "../ramhorns" } -askama = "0.9" -handlebars = "3.1.0-beta.2" +askama = "0.12" +handlebars = "5" serde = "1.0" serde_derive = "1.0" mustache = "0.9" tera = "1.2.0" -ahash = "0.7" +ahash = "0.8" [features] -indexes = [ "ramhorns/indexes" ] +indexes = ["ramhorns/indexes"]