blob: 6c5bf84560f72d5bb915c8460ad4a96ae71e565c (
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
|
//! Example Resource Injection
//!
//! > This example demonstrates how to read and write the program's global state using Mingling's resource system
//!
//! Run:
//! ```bash
//! cargo run --manifest-path examples/example-resources/Cargo.toml --quiet current
//! cargo run --manifest-path examples/example-resources/Cargo.toml --quiet modify-current src
//! ```
//!
//! Output:
//! ```plaintext
//! Current directory: /home/alice/mingling
//! Current directory: /home/alice/mingling/src
//! ```
use std::path::PathBuf;
use mingling::prelude::*;
// Create resource
// ______________ Resource needs to
// / / implement the following two traits
// vvvvvvv vvvvv
#[derive(Default, Clone)]
struct ResCurrentDir {
current_dir: PathBuf,
}
fn main() {
let mut program = ThisProgram::new();
// --------- IMPORTANT ---------
// Use `with_resource` to inject a singleton into the program
program.with_resource(ResCurrentDir {
current_dir: std::env::current_dir().unwrap(),
});
// --------- IMPORTANT ---------
program.with_dispatchers((CMDCurrent, CMDModifyCurrent));
program.exec_and_exit();
}
dispatcher!("current", CMDCurrent => EntryCurrent);
dispatcher!("modify-current", CMDModifyCurrent => EntryModifyCurrent);
// Define chain for modifying current directory _________________ Injected muttable resource
// /
#[chain] // vvvvvvvvvvvvvvvvvv
fn render_modify_current(args: EntryModifyCurrent, current_dir: &mut ResCurrentDir) -> Next {
current_dir.current_dir = current_dir
.current_dir
.join(args.pick::<String>(()).unpack());
EntryCurrent::default()
}
// Define renderer for output current path _____________ Injected resource
// /
/// Renders the current directory path. |
#[renderer] // vvvvvvvvvvvvvv
fn render_current(_: EntryCurrent, current_dir: &ResCurrentDir) {
r_println!("Current directory: {}", current_dir.current_dir.display());
}
gen_program!();
|