aboutsummaryrefslogtreecommitdiff
path: root/docs/res/changlog_examples/feat_program_res.rs
blob: 11a147124f807f31150ed4d96c9c07b66bceaec1 (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
use mingling::{
    macros::{chain, dispatcher, gen_program, pack, r_println, renderer},
    parser::Picker,
    this,
};

// Define a struct with Default and Clone derives
#[derive(Debug, Default, Clone)]
struct Global {
    name: String,
    age: i32,
}

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

    // Add a global resource here
    program.with_resource(Global::default());

    program.exec();
}

dispatcher!("modify", CMDModify => EntryModify);

pack!(DisplayGlobal = ());

#[chain]
fn modify(prev: EntryModify) {
    let (name, age) = Picker::<()>::new(prev.inner)
        .pick::<String>("--name")
        .pick::<i32>("--age")
        .unpack_directly();

    // Modify the global resource
    this::<ThisProgram>().modify_res(|r: &mut Global| {
        r.name = name;
        r.age = age
    });

    DisplayGlobal::default()
}

#[renderer]
fn render_global(_prev: DisplayGlobal) {
    // Read the global resource
    let global = this::<ThisProgram>().res_or_default::<Global>();
    r_println!("Name: {}, Age: {}", global.name, global.age)
}

gen_program!();