aboutsummaryrefslogtreecommitdiff
path: root/docs/pages/3-features/2-general-renderer.md
blob: c3b81a2e1581641a7deaa0f63a7bc0b75ee08195 (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
<h1 align="center">General Renderer</h1>
<p align="center">
    Mingling's Features
</p>

---

## Enable Feature

`general_renderer` is a feature provided by **Mingling**. You can enable it in the following way:

```toml
[dependencies]
mingling = { 
    version = "...", 
    features = ["general_renderer"] 
}
```

## Setup

`general_renderer` requires you to implement the `serde::Serialize` trait for **all** structs, so your project needs to include `serde`

```toml
[dependencies]
serde = { 
    version = "1", 
    features = ["derive"] 
}
```

For types wrapped with the `pack!` macro, `serde::Serialize` will be automatically implemented

```rust
pack!(YourInfo = ()); // Auto derive `serde::Serialize`
```

For types using the derive macro `Groupped`, you need to manually implement `serde::Serialize`

```rust
#[derive(Default, Groupped, Serialize)]
struct YourInfo {
    name: String,
    age: i32,
}
```

> [!Tip]
> If there are types that do not implement `serde::Serialize`, compilation will fail.

## Import GeneralRendererSetup

`general_renderer` provides a Setup type called `GeneralRendererSetup`. 

After importing it into your program, 
user inputs like `--json`, `--yaml`, `--toml`, `--ron`, `--json-pretty`, and `--ron-pretty` will be automatically recognized. 

During the **rendering phase**, instead of the **default renderer**, the serialized content will be displayed to the terminal.

```rust
fn main() {
    let mut program = ThisProgram::new();

    // Add General Renderer
    program.with_setup(GeneralRendererSetup);

    // Add Dispatchers
    program.with_dispatchers((
        // Your dispatchers
    ));

    // Execute
    program.exec();
}
```