blob: 63ebfe6bb0cc102a32372aca682a0731d4516ac2 (
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
|
//! `Mingling` Example - Global Resource Injection
//!
//! This example demonstrates how to use global resource injection in `#[chain]` functions.
//! You can inject both immutable (`&T`) and mutable (`&mut T`) references to global resources.
//!
//! # How to Run
//! ```bash
//! cargo run --manifest-path ./examples/example-resources/Cargo.toml -- setup
//! ```
use mingling::prelude::*;
use std::{env::current_dir, path::PathBuf};
// Define a resource for storing global state
#[derive(Default, Clone)]
pub struct MyResource {
current_dir: PathBuf,
}
fn main() {
let mut program = ThisProgram::new();
// Add the resource to the program
program.with_resource(MyResource::default());
program.with_dispatcher(SetupCommand);
program.exec_and_exit();
}
dispatcher!("setup", SetupCommand => SetupEntry);
pack!(StateRead = ());
pack!(ResultCurrentDir = PathBuf);
#[chain]
fn setup(
_prev: SetupEntry,
resource: &mut MyResource, // Import the resource into `setup`
) -> NextProcess {
// Set the global resource
resource.current_dir = current_dir().unwrap();
StateRead::default()
}
#[chain]
fn read(_prev: StateRead, resource: &MyResource) -> NextProcess {
// Read the global resource
let current_dir = resource.current_dir.clone();
ResultCurrentDir::new(current_dir).to_render()
}
#[renderer]
fn render_current_dir(dir: ResultCurrentDir) {
r_println!("Current dir: {}", dir.to_string_lossy())
}
gen_program!();
|