Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ All notable changes to this project will be documented in this file.
- `masterror::Error` now uses the in-tree derive, removing the dependency on
`thiserror` while keeping the same runtime behaviour and diagnostics.

## [0.5.5] - 2025-09-27

### Fixed
- Derive formatter generation now matches on every `TemplateFormatter`
variant and calls the corresponding `::core::fmt` trait (including the
default `Display` path), mirroring `thiserror`'s placeholder handling.

## [0.5.4] - 2025-09-26

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "masterror"
version = "0.5.4"
version = "0.5.5"
rust-version = "1.90"
edition = "2024"
license = "MIT OR Apache-2.0"
Expand Down Expand Up @@ -49,7 +49,7 @@ turnkey = []
openapi = ["dep:utoipa"]

[workspace.dependencies]
masterror-derive = { version = "0.1.2", path = "masterror-derive" }
masterror-derive = { version = "0.1.3", path = "masterror-derive" }
masterror-template = { version = "0.1.2", path = "masterror-template" }

[dependencies]
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ Stable categories, conservative HTTP mapping, no `unsafe`.

~~~toml
[dependencies]
masterror = { version = "0.5.4", default-features = false }
masterror = { version = "0.5.5", default-features = false }
# or with features:
# masterror = { version = "0.5.4", features = [
# masterror = { version = "0.5.5", features = [
# "axum", "actix", "openapi", "serde_json",
# "sqlx", "sqlx-migrate", "reqwest", "redis",
# "validator", "config", "tokio", "multipart",
Expand Down Expand Up @@ -66,10 +66,10 @@ masterror = { version = "0.5.4", default-features = false }
~~~toml
[dependencies]
# lean core
masterror = { version = "0.5.4", default-features = false }
masterror = { version = "0.5.5", default-features = false }

# with Axum/Actix + JSON + integrations
# masterror = { version = "0.5.4", features = [
# masterror = { version = "0.5.5", features = [
# "axum", "actix", "openapi", "serde_json",
# "sqlx", "sqlx-migrate", "reqwest", "redis",
# "validator", "config", "tokio", "multipart",
Expand Down Expand Up @@ -262,13 +262,13 @@ assert_eq!(resp.status, 401);
Minimal core:

~~~toml
masterror = { version = "0.5.4", default-features = false }
masterror = { version = "0.5.5", default-features = false }
~~~

API (Axum + JSON + deps):

~~~toml
masterror = { version = "0.5.4", features = [
masterror = { version = "0.5.5", features = [
"axum", "serde_json", "openapi",
"sqlx", "reqwest", "redis", "validator", "config", "tokio"
] }
Expand All @@ -277,7 +277,7 @@ masterror = { version = "0.5.4", features = [
API (Actix + JSON + deps):

~~~toml
masterror = { version = "0.5.4", features = [
masterror = { version = "0.5.5", features = [
"actix", "serde_json", "openapi",
"sqlx", "reqwest", "redis", "validator", "config", "tokio"
] }
Expand Down
2 changes: 1 addition & 1 deletion masterror-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "masterror-derive"
rust-version = "1.90"
version = "0.1.2"
version = "0.1.3"
edition = "2024"
license = "MIT OR Apache-2.0"
repository = "https://github.com/RAprogramm/masterror"
Expand Down
116 changes: 50 additions & 66 deletions masterror-derive/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,87 +338,71 @@ fn format_placeholder(
} = resolved;

match formatter {
TemplateFormatter::Display => quote! {
core::fmt::Display::fmt(#expr, f)?;
},
TemplateFormatter::Display => format_with_trait(expr, "Display"),
TemplateFormatter::Debug {
alternate: false
} => quote! {
core::fmt::Debug::fmt(#expr, f)?;
},
TemplateFormatter::Debug {
alternate: true
} => quote! {
write!(f, "{:#?}", #expr)?;
},
alternate
} => format_with_optional_alternate(expr, "Debug", '?', alternate),
TemplateFormatter::LowerHex {
alternate
} => {
if alternate {
quote! { write!(f, "{:#x}", #expr)?; }
} else {
quote! { core::fmt::LowerHex::fmt(#expr, f)?; }
}
}
} => format_with_optional_alternate(expr, "LowerHex", 'x', alternate),
TemplateFormatter::UpperHex {
alternate
} => {
if alternate {
quote! { write!(f, "{:#X}", #expr)?; }
} else {
quote! { core::fmt::UpperHex::fmt(#expr, f)?; }
}
}
} => format_with_optional_alternate(expr, "UpperHex", 'X', alternate),
TemplateFormatter::Pointer {
alternate
} => {
if alternate {
quote! { write!(f, "{:#p}", #expr)?; }
} else if pointer_value {
quote! {{
let value = #expr;
core::fmt::Pointer::fmt(&value, f)?;
}}
} else {
quote! { core::fmt::Pointer::fmt(#expr, f)?; }
}
}
} => format_pointer(expr, pointer_value, alternate),
TemplateFormatter::Binary {
alternate
} => {
if alternate {
quote! { write!(f, "{:#b}", #expr)?; }
} else {
quote! { core::fmt::Binary::fmt(#expr, f)?; }
}
}
} => format_with_optional_alternate(expr, "Binary", 'b', alternate),
TemplateFormatter::Octal {
alternate
} => {
if alternate {
quote! { write!(f, "{:#o}", #expr)?; }
} else {
quote! { core::fmt::Octal::fmt(#expr, f)?; }
}
}
} => format_with_optional_alternate(expr, "Octal", 'o', alternate),
TemplateFormatter::LowerExp {
alternate
} => {
if alternate {
quote! { write!(f, "{:#e}", #expr)?; }
} else {
quote! { core::fmt::LowerExp::fmt(#expr, f)?; }
}
}
} => format_with_optional_alternate(expr, "LowerExp", 'e', alternate),
TemplateFormatter::UpperExp {
alternate
} => {
if alternate {
quote! { write!(f, "{:#E}", #expr)?; }
} else {
quote! { core::fmt::UpperExp::fmt(#expr, f)?; }
}
}
} => format_with_optional_alternate(expr, "UpperExp", 'E', alternate)
}
}

fn format_with_trait(expr: TokenStream, trait_name: &str) -> TokenStream {
let trait_ident = format_ident!("{}", trait_name);
quote! {
::core::fmt::#trait_ident::fmt(#expr, f)?;
}
}

fn format_with_optional_alternate(
expr: TokenStream,
trait_name: &str,
specifier: char,
alternate: bool
) -> TokenStream {
if alternate {
format_with_alternate(expr, specifier)
} else {
format_with_trait(expr, trait_name)
}
}

fn format_with_alternate(expr: TokenStream, specifier: char) -> TokenStream {
let format_string = format!("{{:#{}}}", specifier);
quote! {
::core::write!(f, #format_string, #expr)?;
}
Comment on lines +389 to +393

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P0] Generate string literal using non-tokenizable String

The new helper format_with_alternate constructs a String with format! and then interpolates it directly into quote! (::core::write!(f, #format_string, #expr)?;). quote! requires the interpolated value to implement ToTokens, which String does not, so the proc-macro crate will fail to compile as soon as this arm is exercised. The previous implementation used a literal format string and compiled successfully. Convert the computed string into a token type (e.g. syn::LitStr::new(&format_string, Span::call_site())) before interpolation.

Useful? React with 👍 / 👎.

}

fn format_pointer(expr: TokenStream, pointer_value: bool, alternate: bool) -> TokenStream {
if alternate {
format_with_alternate(expr, 'p')
} else if pointer_value {
quote! {{
let value = #expr;
::core::fmt::Pointer::fmt(&value, f)?;
}}
} else {
format_with_trait(expr, "Pointer")
}
}

Expand Down
Loading