diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-06-18 22:48:16 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-06-18 22:48:16 +0800 |
| commit | 0f7b2a50b05f38d886234ff6b031766c7af1dabb (patch) | |
| tree | 9c9b5d4aa11c91c117b08e829ec33361c4aa6275 /mingling_macros/src/lib.rs | |
| parent | dd28430b67dcfda6dd2e91750a4c1a62c085150a (diff) | |
Add `pack_err!` macro for error structs with automatic name field
Diffstat (limited to 'mingling_macros/src/lib.rs')
| -rw-r--r-- | mingling_macros/src/lib.rs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/mingling_macros/src/lib.rs b/mingling_macros/src/lib.rs index 97dd824..408450a 100644 --- a/mingling_macros/src/lib.rs +++ b/mingling_macros/src/lib.rs @@ -158,6 +158,8 @@ mod help; mod node; mod pack; #[cfg(feature = "extra_macros")] +mod pack_err; +#[cfg(feature = "extra_macros")] mod program_setup; mod render; mod renderer; @@ -319,6 +321,74 @@ pub fn pack(input: TokenStream) -> TokenStream { pack::pack(input) } +/// Creates an error struct with a `name: String` field and optional `info: Type` field. +/// +/// This macro provides a concise way to define error types that implement `Groupped` +/// and are registered for inclusion in the program enum. +/// +/// The `name` field is automatically set to the snake_case version of the struct name +/// at compile time. +/// +/// # Syntax +/// +/// Two forms are supported: +/// +/// ```rust,ignore +/// // Simple form — generates a struct with only `name: String` and a `Default` impl: +/// pack_err!(ErrorNotFound); +/// +/// // Typed form — generates a struct with `name: String` + `info: Type` and a `new(info)` constructor: +/// pack_err!(ErrorNotDir = PathBuf); +/// ``` +/// +/// # Generated code +/// +/// For `pack_err!(ErrorNotFound)`: +/// +/// ```rust,ignore +/// #[derive(::mingling::Groupped)] +/// pub struct ErrorNotFound { +/// name: String, +/// } +/// +/// impl Default for ErrorNotFound { +/// fn default() -> Self { +/// Self { +/// name: "error_not_found".into(), +/// } +/// } +/// } +/// ``` +/// +/// For `pack_err!(ErrorNotDir = PathBuf)`: +/// +/// ```rust,ignore +/// #[derive(::mingling::Groupped)] +/// pub struct ErrorNotDir { +/// name: String, +/// info: PathBuf, +/// } +/// +/// impl ErrorNotDir { +/// pub fn new(info: PathBuf) -> Self { +/// Self { +/// name: "error_not_dir".into(), +/// info, +/// } +/// } +/// } +/// ``` +/// +/// When the `general_renderer` feature is enabled, the struct also gets +/// `#[derive(serde::Serialize)]`. +/// +/// This macro is only available with the `extra_macros` feature. +#[cfg(feature = "extra_macros")] +#[proc_macro] +pub fn pack_err(input: TokenStream) -> TokenStream { + pack_err::pack_err(input) +} + /// Early-returns an error from a `Result`, converting the `Ok` branch to a /// `ChainProcess`. /// |
