aboutsummaryrefslogtreecommitdiff
path: root/examples/example-error-handling/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/example-error-handling/src/main.rs')
-rw-r--r--examples/example-error-handling/src/main.rs37
1 files changed, 18 insertions, 19 deletions
diff --git a/examples/example-error-handling/src/main.rs b/examples/example-error-handling/src/main.rs
index 0cd973a..ec7e6c9 100644
--- a/examples/example-error-handling/src/main.rs
+++ b/examples/example-error-handling/src/main.rs
@@ -26,38 +26,44 @@ use std::io::Write;
// In Mingling, instead of using ? to propagate errors upward,
// errors are treated as branches that continue execution.
-dispatcher!("hello", CMDHello => EntryHello);
+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,19 +102,12 @@ 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
}
gen_program!();
fn main() {
- let mut program = ThisProgram::new();
- program.with_dispatcher(CMDHello);
- program.exec_and_exit();
+ ThisProgram::new().exec_and_exit();
}