aboutsummaryrefslogtreecommitdiff
path: root/examples/example-async/src/main.rs
blob: 7b0be38637949f288b3200f6e745ab2f64b753a7 (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
//! `Mingling` Example - Async
//!
//! After enabling the `async` feature:
//! 1. The `chain!` macro will support using **async** functions,
//! 2. The `exec` function of `Program` will return a `Future` for you to use with an async runtime
//!
//! ## Enable Feature
//! Enable the `async` feature for mingling in `Cargo.toml`
//! ```toml
//! [dependencies]
//! mingling = { version = "...", features = ["async"] }
//! ```
//!
//! # How to Run
//! ```bash
//! cargo run --manifest-path ./examples/example-async/Cargo.toml -- hello World
//! ```

use mingling::prelude::*;

dispatcher!("hello", HelloCommand => HelloEntry);

// Use Tokio async runtime
#[tokio::main]
async fn main() {
    let mut program = ThisProgram::new();
    program.with_dispatcher(HelloCommand);

    // Run program
    program.exec().await;
}

pack!(Hello = String);

// You can freely use async / non-async functions to declare your Chain

#[chain]
// fn parse_name(prev: HelloEntry) -> Next {
async fn parse_name(prev: HelloEntry) -> Next {
    let name = prev.first().cloned().unwrap_or_else(|| "World".to_string());
    Hello::new(name).to_render()
}

// For renderers, you can still only use synchronous functions
#[renderer]
fn render_hello_who(prev: Hello) {
    r_println!("Hello, {}!", *prev);
}

gen_program!();