aboutsummaryrefslogtreecommitdiff
path: root/examples/example-error-handling/src/main.rs
blob: de9792d6cd4b0c0ab2fe815ef35da7170950790e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//! 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::*;
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);

// 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()
}

/// Renders a successful greeting with the given name.
#[renderer]
fn render_result_name(name: ResultName) -> RenderResult {
    let mut render_result = RenderResult::new();
    writeln!(render_result, "Hello, {}", *name).ok();
    render_result
}

/// Renders the error when no name is provided.
#[renderer]
fn render_error_no_name_provided(_: ErrorNoNameProvided) -> RenderResult {
    let mut render_result = RenderResult::new();
    writeln!(render_result, "No name provided").ok();
    render_result
}

/// Renders the error when the name is already taken.
#[renderer]
fn render_error_name_not_available(_: ErrorNameNotAvailable) -> RenderResult {
    let mut render_result = RenderResult::new();
    writeln!(render_result, "Name not available").ok();
    render_result
}

/// Renders the error when the name exceeds the maximum length.
#[renderer]
fn render_error_name_too_long(len: ErrorNameTooLong) -> RenderResult {
    let mut render_result = RenderResult::new();
    writeln!(render_result, "Name too long: {} > 10", *len).ok();
    render_result
}

/// Renders the error when the dispatcher (subcommand) is not found.
#[renderer]
fn render_dispatcher_not_found(err: ErrorDispatcherNotFound) -> RenderResult {
    let mut render_result = RenderResult::new();
    writeln!(
        render_result,
        "Command not found: \"{}\"",
        err.inner.join(" ")
    )
    .ok();
    render_result
}

gen_program!();

fn main() {
    let mut program = ThisProgram::new();
    program.with_dispatcher(CMDHello);
    program.exec_and_exit();
}