From 3ed2f229307e3394df209603441c4037a4cc0220 Mon Sep 17 00:00:00 2001 From: Frando Date: Fri, 31 Oct 2025 00:41:34 +0100 Subject: [PATCH 1/2] deps: remove darling --- Cargo.lock | 54 --------------------- n0-error-macros/Cargo.toml | 1 - n0-error-macros/src/lib.rs | 96 ++++++++++++++++++++++++++++++++------ 3 files changed, 82 insertions(+), 69 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3903845..e273990 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,41 +8,6 @@ version = "1.0.100" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61" -[[package]] -name = "darling" -version = "0.21.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cdf337090841a411e2a7f3deb9187445851f91b309c0c0a29e05f74a00a48c0" -dependencies = [ - "darling_core", - "darling_macro", -] - -[[package]] -name = "darling_core" -version = "0.21.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1247195ecd7e3c85f83c8d2a366e4210d588e802133e1e355180a9870b517ea4" -dependencies = [ - "fnv", - "ident_case", - "proc-macro2", - "quote", - "strsim", - "syn", -] - -[[package]] -name = "darling_macro" -version = "0.21.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d38308df82d1080de0afee5d069fa14b0326a88c14f15c5ccda35b4a6c414c81" -dependencies = [ - "darling_core", - "quote", - "syn", -] - [[package]] name = "derive_more" version = "2.0.1" @@ -64,24 +29,12 @@ dependencies = [ "unicode-xid", ] -[[package]] -name = "fnv" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" - [[package]] name = "heck" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" -[[package]] -name = "ident_case" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" - [[package]] name = "n0-error" version = "0.1.0" @@ -96,7 +49,6 @@ dependencies = [ name = "n0-error-macros" version = "0.1.0" dependencies = [ - "darling", "heck", "proc-macro2", "quote", @@ -132,12 +84,6 @@ dependencies = [ "syn", ] -[[package]] -name = "strsim" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" - [[package]] name = "syn" version = "2.0.106" diff --git a/n0-error-macros/Cargo.toml b/n0-error-macros/Cargo.toml index 81b0c5e..c2923bb 100644 --- a/n0-error-macros/Cargo.toml +++ b/n0-error-macros/Cargo.toml @@ -16,4 +16,3 @@ proc-macro2 = "1" quote = "1" syn = { version = "2", features = ["full", "extra-traits"] } heck = "0.5" -darling = "0.21.3" diff --git a/n0-error-macros/src/lib.rs b/n0-error-macros/src/lib.rs index a59b74d..00cb099 100644 --- a/n0-error-macros/src/lib.rs +++ b/n0-error-macros/src/lib.rs @@ -1,4 +1,3 @@ -use darling::FromAttributes; use proc_macro::TokenStream; use proc_macro2::Span; use quote::{quote, ToTokens}; @@ -164,12 +163,12 @@ fn add_meta_field(fields: &mut Fields) { pub fn derive_stack_error(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as syn::DeriveInput); match derive_error_inner(input) { - Err(tokens) => tokens.write_errors().into(), + Err(err) => err.to_compile_error().into(), Ok(tokens) => tokens.into(), } } -fn derive_error_inner(input: DeriveInput) -> Result { +fn derive_error_inner(input: DeriveInput) -> Result { match &input.data { syn::Data::Enum(item) => { let args = EnumAttrArgs::from_attributes(&input.attrs)?; @@ -191,8 +190,7 @@ fn derive_error_inner(input: DeriveInput) -> Result Err(err( &input, "#[derive(StackError)] only supports enums or structs", - ) - .into()), + )), } } @@ -222,8 +220,7 @@ enum SourceKind { Std, } -#[derive(Default, Clone, Copy, darling::FromMeta)] -#[darling(default, derive_syn_parse)] +#[derive(Default, Clone, Copy)] struct StackErrAttrArgs { add_meta: bool, derive: bool, @@ -231,15 +228,53 @@ struct StackErrAttrArgs { std_sources: bool, } -#[derive(Default, Clone, Copy, FromAttributes)] -#[darling(default, attributes(error, stackerr))] +impl syn::parse::Parse for StackErrAttrArgs { + fn parse(input: syn::parse::ParseStream<'_>) -> syn::Result { + let mut out = Self::default(); + while !input.is_empty() { + let ident: Ident = input.parse()?; + match ident.to_string().as_str() { + "add_meta" => out.add_meta = true, + "derive" => out.derive = true, + "from_sources" => out.from_sources = true, + "std_sources" => out.std_sources = true, + other => Err(err( + ident, + format!("unknown stack_error option `{}`", other), + ))?, + } + if input.peek(syn::Token![,]) { + let _ = input.parse::()?; + } + } + Ok(out) + } +} + +#[derive(Default, Clone, Copy)] struct EnumAttrArgs { from_sources: bool, std_sources: bool, } -#[derive(Default, Clone, Copy, FromAttributes)] -#[darling(default, attributes(error))] +impl EnumAttrArgs { + fn from_attributes(attrs: &[Attribute]) -> Result { + let mut out = Self::default(); + for ident in parse_error_attr_as_idents(attrs)? { + match ident.to_string().as_str() { + "from_sources" => out.from_sources = true, + "std_sources" => out.std_sources = true, + _ => Err(err( + ident, + "Invalid argument for the `error` attribute on fields", + ))?, + } + } + Ok(out) + } +} + +#[derive(Default, Clone, Copy)] struct FieldAttrArgs { source: bool, from: bool, @@ -248,6 +283,26 @@ struct FieldAttrArgs { meta: bool, } +impl FieldAttrArgs { + fn from_attributes(attrs: &[Attribute]) -> Result { + let mut out = Self::default(); + for ident in parse_error_attr_as_idents(attrs)? { + match ident.to_string().as_str() { + "source" => out.source = true, + "from" => out.from = true, + "std_err" => out.std_err = true, + "stack_err" => out.stack_err = true, + "meta" => out.meta = true, + _ => Err(err( + ident, + "Invalid argument for the `error` attribute on fields", + ))?, + } + } + Ok(out) + } +} + #[derive(Debug, Clone, Copy)] enum VariantIdent<'a> { Struct(&'a Ident), @@ -888,8 +943,8 @@ impl DisplayArgs { } // #[error("...", args...)] - let mut it = args.into_iter(); - let first = it.next().unwrap(); + let mut args = args.into_iter(); + let first = args.next().unwrap(); let fmt_lit = match first { Expr::Lit(syn::ExprLit { lit: syn::Lit::Str(s), .. }) => s, other => return Err(err( @@ -898,7 +953,7 @@ impl DisplayArgs { )) }; - let rest: Vec = it.collect(); + let rest: Vec = args.collect(); Ok(DisplayArgs::Format( quote! { write!(f, #fmt_lit #(, #rest)* ) }, )) @@ -908,3 +963,16 @@ impl DisplayArgs { fn err(ident: impl ToTokens, err: impl ToString) -> syn::Error { syn::Error::new_spanned(ident, err.to_string()) } + +fn parse_error_attr_as_idents(attrs: &[Attribute]) -> Result, syn::Error> { + let mut out = vec![]; + for attr in attrs.iter().filter(|a| a.path().is_ident("error")) { + let idents = attr.parse_args_with(|input: syn::parse::ParseStream<'_>| { + let list: Punctuated = + Punctuated::::parse_terminated(input)?; + Ok(list.into_iter()) + })?; + out.extend(idents); + } + Ok(out) +} From c82a11711118a3f903e1651b84218e77bcdf9b40 Mon Sep 17 00:00:00 2001 From: Frando Date: Fri, 31 Oct 2025 00:43:14 +0100 Subject: [PATCH 2/2] cleanup --- Cargo.toml | 4 ++-- n0-error-macros/Cargo.toml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ce7ee2c..c598b93 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,12 +7,12 @@ description = "ergonomic errors with call-site location" license = "MIT OR Apache-2.0" authors = ["Frando ", "n0 team"] repository = "https://github.com/n0-computer/n0-error" -keywords = ["error", "backtrace", "location"] +keywords = ["error", "location", "ergonomic"] [dependencies] -n0-error-macros = { path = "n0-error-macros" } anyhow = { version = "1.0.100", optional = true } derive_more = { version = "2.0.1", features = ["debug", "display"] } +n0-error-macros = { path = "n0-error-macros" } spez = "0.1.2" [dev-dependencies] diff --git a/n0-error-macros/Cargo.toml b/n0-error-macros/Cargo.toml index c2923bb..8d82c3f 100644 --- a/n0-error-macros/Cargo.toml +++ b/n0-error-macros/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" description = "macros for n0-error" license = "MIT OR Apache-2.0" -authors = ["Frando ", "n0 team"] +authors = ["Frando ", "n0 team"] repository = "https://github.com/n0-computer/n0-error" keywords = ["error", "location", "ergonomic"] @@ -12,7 +12,7 @@ keywords = ["error", "location", "ergonomic"] proc-macro = true [dependencies] +heck = "0.5" proc-macro2 = "1" quote = "1" syn = { version = "2", features = ["full", "extra-traits"] } -heck = "0.5"