Skip to content

Commit

Permalink
Update dependencies, use syn 2.0 (#74)
Browse files Browse the repository at this point in the history
* Upgrade deps

* Update test deps

* Update deps and cleanup
  • Loading branch information
novacrazy authored Oct 16, 2024
1 parent a6d020e commit 5bd9975
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 23 deletions.
5 changes: 2 additions & 3 deletions ramhorns-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
89 changes: 78 additions & 11 deletions ramhorns-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<LitStr>,
rename_all: Option<LitStr>,
callback: Option<Path>,
}

impl Ramhorns {
pub fn try_from_attributes(attrs: &[syn::Attribute]) -> syn::Result<Option<Self>> {
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::<Self>(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<Self> {
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::<syn::Ident>()?;

match &*attr_ident.to_string() {
"skip" => skip = true,
"md" => md = true,
"flatten" => flatten = true,
"rename" => {
input.parse::<syn::Token![=]>()?;
rename = Some(input.parse()?);
}
"rename_all" => {
input.parse::<syn::Token![=]>()?;
rename_all = Some(input.parse()?);
}
"callback" => {
input.parse::<syn::Token![=]>()?;
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::<syn::Token![,]>().ok();
}
syn::Result::Ok(Self {
skip,
md,
flatten,
rename,
rename_all,
callback,
})
}
}

#[allow(clippy::enum_variant_names)]
enum RenameAll {
UpperCase,
PascalCase,
Expand All @@ -73,6 +136,7 @@ enum RenameAll {
KebabCase,
ScreamingKebabCase,
}

impl TryFrom<LitStr> for RenameAll {
type Error = syn::Error;

Expand All @@ -91,6 +155,7 @@ impl TryFrom<LitStr> for RenameAll {
})
}
}

impl RenameAll {
fn rename(&self, input: &str) -> String {
match &self {
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
Expand Down Expand Up @@ -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) )*
Expand Down
4 changes: 2 additions & 2 deletions ramhorns/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
4 changes: 2 additions & 2 deletions ramhorns/src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion ramhorns/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl<W: io::Write> Encoder for EscapingIOEncoder<W> {
#[cfg(feature = "pulldown-cmark")]
#[inline]
fn write_html<'a, I: Iterator<Item = Event<'a>>>(&mut self, iter: I) -> io::Result<()> {
html::write_html(&mut self.inner, iter)
html::write_html_io(&mut self.inner, iter)
}

#[inline]
Expand Down
8 changes: 4 additions & 4 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

0 comments on commit 5bd9975

Please sign in to comment.