aboutsummaryrefslogtreecommitdiff
path: root/examples/example-error-handling/src/main.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-08-17 05:49:19 +0800
committer魏曹先生 <1992414357@qq.com>2026-08-17 05:49:19 +0800
commit57c53affe3542cb6bd4e79ee4c18f20a1bd76b2d (patch)
tree1cd4aef44cb7a45a8cd9d520b598f5f181e24c76 /examples/example-error-handling/src/main.rs
parentef23cd944402939605c78a4a853ef6e33af02c21 (diff)
refactor!: replace pack! macros with derive-based pipeline types
Remove the `pack!`, `pack_err!`, `pack_structural!`, and `pack_err_structural!` macros, replacing all pipeline type definitions with `#[derive(Grouped)]` and `#[derive(Grouped, Wrap)]` attributes. This changes the generated struct shape from named-field structs with an `inner` field to tuple structs accessed via `.0`, and removes the auto-generated `name` and `info` fields from error types.
Diffstat (limited to 'examples/example-error-handling/src/main.rs')
-rw-r--r--examples/example-error-handling/src/main.rs31
1 files changed, 16 insertions, 15 deletions
diff --git a/examples/example-error-handling/src/main.rs b/examples/example-error-handling/src/main.rs
index 05b451b..ec7e6c9 100644
--- a/examples/example-error-handling/src/main.rs
+++ b/examples/example-error-handling/src/main.rs
@@ -29,35 +29,41 @@ use std::io::Write;
dispatcher!("hello", EntryHello);
// Define error types
-pack!(ErrorNoNameProvided = ());
-pack!(ErrorNameTooLong = u16);
-pack!(ErrorNameNotAvailable = ());
+#[derive(Grouped)]
+pub struct ErrorNoNameProvided;
+
+#[derive(Grouped, Wrap)]
+pub struct ErrorNameTooLong(u16);
+
+#[derive(Grouped)]
+pub struct ErrorNameNotAvailable;
// Define success type
-pack!(ResultName = String);
+#[derive(Grouped, Wrap)]
+pub struct ResultName(String);
// Pre-registered names
static VEC_REGISTERED_NAMES: &[&str] = &["Alice", "Bob", "Charlie", "David", "Eve"];
#[chain]
fn handle_hello(args: EntryHello) -> Next {
- let Some(name) = args.inner.first().cloned() else {
+ let Some(name) = args.0.first().cloned() else {
// If no name is provided, pass ErrorNoNameProvided
- return ErrorNoNameProvided::default().to_render();
+ return ErrorNoNameProvided.to_render();
};
if name.len() > 10 {
// If the name is too long, pass ErrorNameTooLong
- return ErrorNameTooLong::new(name.len() as u16).to_render();
+ return ErrorNameTooLong(name.len() as u16).to_render();
}
if VEC_REGISTERED_NAMES.contains(&name.as_str()) {
// If the name already exists, pass ErrorNameNotAvailable
- return ErrorNameNotAvailable::default().to_render();
+ return ErrorNameNotAvailable.to_render();
}
// If the name is valid, pass ResultName
- ResultName::new(name).to_render()
+ ResultName(name).to_render()
}
/// Renders a successful greeting with the given name.
@@ -96,12 +102,7 @@ fn render_error_name_too_long(len: ErrorNameTooLong) -> RenderResult {
#[renderer]
fn render_entry_fallback(err: EntryFallback) -> RenderResult {
let mut render_result = RenderResult::new();
- writeln!(
- render_result,
- "Command not found: \"{}\"",
- err.inner.join(" ")
- )
- .ok();
+ writeln!(render_result, "Command not found: \"{}\"", err.0.join(" ")).ok();
render_result
}