aboutsummaryrefslogtreecommitdiff
path: root/examples/example-error-handling
diff options
context:
space:
mode:
Diffstat (limited to 'examples/example-error-handling')
-rw-r--r--examples/example-error-handling/Cargo.lock76
-rw-r--r--examples/example-error-handling/Cargo.toml7
-rw-r--r--examples/example-error-handling/src/main.rs97
3 files changed, 180 insertions, 0 deletions
diff --git a/examples/example-error-handling/Cargo.lock b/examples/example-error-handling/Cargo.lock
new file mode 100644
index 0000000..d2deef4
--- /dev/null
+++ b/examples/example-error-handling/Cargo.lock
@@ -0,0 +1,76 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 4
+
+[[package]]
+name = "example-error-handling"
+version = "0.1.0"
+dependencies = [
+ "mingling",
+]
+
+[[package]]
+name = "just_fmt"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5454cda0d57db59778608d7a47bff5b16c6705598265869fb052b657f66cf05e"
+
+[[package]]
+name = "mingling"
+version = "0.1.9"
+dependencies = [
+ "mingling_core",
+ "mingling_macros",
+]
+
+[[package]]
+name = "mingling_core"
+version = "0.1.9"
+dependencies = [
+ "just_fmt",
+]
+
+[[package]]
+name = "mingling_macros"
+version = "0.1.9"
+dependencies = [
+ "just_fmt",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.106"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.45"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "syn"
+version = "2.0.117"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.24"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
diff --git a/examples/example-error-handling/Cargo.toml b/examples/example-error-handling/Cargo.toml
new file mode 100644
index 0000000..c6e7ea3
--- /dev/null
+++ b/examples/example-error-handling/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "example-error-handling"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
+mingling = { path = "../../mingling" }
diff --git a/examples/example-error-handling/src/main.rs b/examples/example-error-handling/src/main.rs
new file mode 100644
index 0000000..d8db852
--- /dev/null
+++ b/examples/example-error-handling/src/main.rs
@@ -0,0 +1,97 @@
+//! Example Error Handling
+//!
+//! > This example demonstrates how to handle errors in Mingling, including custom error types and error rendering.
+//!
+//! Run:
+//! ```bash
+//! cargo run --manifest-path examples/example-error-handling/Cargo.toml --quiet -- hallo
+//! cargo run --manifest-path examples/example-error-handling/Cargo.toml --quiet -- hello
+//! cargo run --manifest-path examples/example-error-handling/Cargo.toml --quiet -- hello Alice
+//! cargo run --manifest-path examples/example-error-handling/Cargo.toml --quiet -- hello MyBestFriendAlice
+//! cargo run --manifest-path examples/example-error-handling/Cargo.toml --quiet -- hello Peter
+//! ```
+//!
+//! Output:
+//! ```plaintext
+//! Command not found: "hallo"
+//! No name provided
+//! Name not available
+//! Name too long: 17 > 10
+//! Hello, Peter
+//! ```
+
+use mingling::prelude::*;
+
+// In Mingling, instead of using ? to propagate errors upward,
+// errors are treated as branches that continue execution.
+
+dispatcher!("hello", CMDHello => EntryHello);
+
+// Define error types
+pack!(ErrorNoNameProvided = ());
+pack!(ErrorNameTooLong = u16);
+pack!(ErrorNameNotAvailable = ());
+
+// Define success type
+pack!(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 {
+ // If no name is provided, pass ErrorNoNameProvided
+ return ErrorNoNameProvided::default().to_render();
+ };
+
+ if name.len() > 10 {
+ // If the name is too long, pass ErrorNameTooLong
+ return ErrorNameTooLong::new(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();
+ }
+
+ // If the name is valid, pass ResultName
+ ResultName::new(name).to_render()
+}
+
+#[renderer]
+fn render_result_name(name: ResultName) {
+ r_println!("Hello, {}", *name);
+}
+
+#[renderer]
+fn render_error_no_name_provided(_: ErrorNoNameProvided) {
+ // Prompt when no name is provided
+ r_println!("No name provided");
+}
+
+#[renderer]
+fn render_error_name_not_available(_: ErrorNameNotAvailable) {
+ // Prompt when name is already taken
+ r_println!("Name not available");
+}
+
+#[renderer]
+fn render_error_name_too_long(len: ErrorNameTooLong) {
+ // Prompt when name is too long, showing actual length
+ r_println!("Name too long: {} > 10", *len);
+}
+
+#[renderer]
+fn render_dispatcher_not_found(err: DispatcherNotFound) {
+ // Prompt when command is not found, showing the input command
+ r_println!("Command not found: \"{}\"", err.inner.join(" "));
+}
+
+gen_program!();
+
+fn main() {
+ let mut program = ThisProgram::new();
+ program.with_dispatcher(CMDHello);
+ program.exec_and_exit();
+}