aboutsummaryrefslogtreecommitdiff
path: root/docs/pages/1-get-started.md
blob: b05e72b9c21b734a13c15d44ba85dacd7d4d9d31 (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
# Get Started
This article explains how to quickly create your first **Mingling** command-line program.

## Quick Start
1. Add `mingling` to your Rust project.
```bash
cargo add mingling
```
Or add the following to your `Cargo.toml`:
```toml
[dependencies]
mingling = "0.1.6"
```

2. Write the basic code in your `main.rs` or other program entry point.
```rust
use mingling::macros::{dispatcher, gen_program, r_println, renderer};
 
#[tokio::main]
async fn main() {
    // Create ThisProgram
    let mut program = ThisProgram::new();
 
    // Import the dispatcher `HelloCommand`
    program.with_dispatcher(HelloCommand);
 
    // Run the program
    program.exec().await;
}
 
// Define the dispatcher `HelloCommand`, which routes the "hello" subcommand to `HelloEntry`
dispatcher!("hello", HelloCommand => HelloEntry);
 
// Define the renderer, which receives `HelloEntry` and renders the content
#[renderer]
fn render_hello(_prev: HelloEntry) {
    r_println!("Hello, World!")
}
 
// Create ThisProgram at the end of the code
gen_program!();
```

3. Install your command-line program and run it.
```bash
cargo install --path ./
your_bin hello
```
Result:
```bash
Hello, World!
```

## About Async Runtime

**Mingling** supports **async runtime**, you can enable the `async` feature to activate it.

After enabling it, **Mingling** will have the following changes:

- The `Chain` trait and `chain!` macro will require you to use **async functions**
- `Program::exec` will become an async function
- The `gen_program!` macro will generate async functions

**Mingling** does not depend on any specific asynchronous runtime internally, which means you can freely choose a suitable asynchronous runtime for your program (such as `async-std`, `tokio`)

## 💡 Next Steps
> **Mingling**'s basic components [Go](./pages/2-basic)