| Crates.io | masterror-template |
| lib.rs | masterror-template |
| version | 0.3.6 |
| created_at | 2025-09-21 00:07:31.233173+00 |
| updated_at | 2025-09-21 00:07:31.233173+00 |
| description | Template utilities for masterror and its derive macros |
| homepage | |
| repository | https://github.com/RAprogramm/masterror |
| max_upload_size | |
| id | 1848316 |
| size | 52,733 |
masterror-template packages the template parser shared by the masterror runtime crate and the masterror-derive procedural macros. It understands the #[error("...")] formatting language popularised by thiserror v2, producing a structured representation that downstream code can inspect or render.
The crate is intentionally small: it exposes just enough API for advanced applications that want to inspect derived error displays, implement custom derive helpers, or perform static analysis over formatting placeholders.
Add the crate alongside masterror if you need direct access to the parser:
[dependencies]
masterror-template = { version = "0.3.6" }
masterror-template targets Rust 1.90 and builds on stable and nightly toolchains alike.
Call ErrorTemplate::parse to turn an &str into a structured template:
use masterror_template::template::{ErrorTemplate, TemplateIdentifier};
fn inspect(template: &str) {
let parsed = ErrorTemplate::parse(template).expect("valid template");
for placeholder in parsed.placeholders() {
match placeholder.identifier() {
TemplateIdentifier::Named(name) => println!("named placeholder: {name}"),
TemplateIdentifier::Positional(index) => println!("positional placeholder: {index}"),
TemplateIdentifier::Implicit(index) => println!("implicit placeholder: {index}"),
}
}
}
The parser preserves literal text and exposes every placeholder with span metadata, making it straightforward to surface diagnostics or transform templates programmatically.
Each TemplatePlaceholder advertises the requested formatter through TemplateFormatter and TemplateFormatterKind:
use masterror_template::template::{ErrorTemplate, TemplateFormatterKind};
let template = ErrorTemplate::parse("{value:#x}").expect("parse");
let placeholder = template.placeholders().next().expect("placeholder");
let formatter = placeholder.formatter();
assert_eq!(formatter.kind(), TemplateFormatterKind::LowerHex);
assert!(formatter.is_alternate());
This mirrors the formatting traits accepted by core::fmt, enabling consumers to route values through Display, Debug, hexadecimal, binary, pointer, or exponential renderers.
Parsing failures produce TemplateError variants with precise byte ranges. The metadata simplifies IDE integrations and procedural macros that need to point at the offending part of the template.
use masterror_template::template::ErrorTemplate;
let err = ErrorTemplate::parse("{foo").unwrap_err();
assert!(matches!(err, masterror_template::template::TemplateError::UnterminatedPlaceholder { .. }));
Dual licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.