diff options
| author | Weicao-CatilGrass <1992414357@qq.com> | 2026-05-23 23:41:04 +0800 |
|---|---|---|
| committer | Weicao-CatilGrass <1992414357@qq.com> | 2026-05-23 23:49:34 +0800 |
| commit | 0a2ef958c0dca21d19e4ffc38ba5a7c4078e182a (patch) | |
| tree | c82fc4242ed393b132ba514eb434d722e7d9c387 /examples/example-resources | |
| parent | ccab1940c019dfbfb7dfcbbe4cb927258933755f (diff) | |
Rework examples and add entry macro for testing
Diffstat (limited to 'examples/example-resources')
| -rw-r--r-- | examples/example-resources/Cargo.lock | 2 | ||||
| -rw-r--r-- | examples/example-resources/Cargo.toml | 7 | ||||
| -rw-r--r-- | examples/example-resources/src/main.rs | 73 |
3 files changed, 45 insertions, 37 deletions
diff --git a/examples/example-resources/Cargo.lock b/examples/example-resources/Cargo.lock index 63467d0..bab1a19 100644 --- a/examples/example-resources/Cargo.lock +++ b/examples/example-resources/Cargo.lock @@ -4,7 +4,7 @@ version = 4 [[package]] name = "example-resources" -version = "0.0.1" +version = "0.1.0" dependencies = [ "mingling", ] diff --git a/examples/example-resources/Cargo.toml b/examples/example-resources/Cargo.toml index ca783f3..2655654 100644 --- a/examples/example-resources/Cargo.toml +++ b/examples/example-resources/Cargo.toml @@ -1,7 +1,8 @@ [package] name = "example-resources" -version = "0.0.1" +version = "0.1.0" edition = "2024" -[dependencies] -mingling = { path = "../../mingling", features = ["parser"] } +[dependencies.mingling] +path = "../../mingling" +features = ["parser"] diff --git a/examples/example-resources/src/main.rs b/examples/example-resources/src/main.rs index 9d8dde6..ae2c8f2 100644 --- a/examples/example-resources/src/main.rs +++ b/examples/example-resources/src/main.rs @@ -1,57 +1,64 @@ -//! `Mingling` Example - Global Resource Injection +//! Example 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. +//! > This example demonstrates how to read and write the program's global state using Mingling's resource system //! -//! # How to Run +//! Run: //! ```bash -//! cargo run --manifest-path ./examples/example-resources/Cargo.toml -- setup +//! 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::*; -use std::{env::current_dir, path::PathBuf}; -// Define a resource for storing global state +// Create resource +// ______________ Resource needs to +// / / implement the following two traits +// vvvvvvv vvvvv #[derive(Default, Clone)] -pub struct MyResource { +struct ResCurrentDir { current_dir: PathBuf, } fn main() { let mut program = ThisProgram::new(); - // Add the resource to the program - program.with_resource(MyResource::default()); + // --------- 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_dispatcher(SetupCommand); + program.with_dispatchers((CMDCurrent, CMDModifyCurrent)); 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` -) -> Next { - // Set the global resource - resource.current_dir = current_dir().unwrap(); - - StateRead::default() -} +dispatcher!("current", CMDCurrent => EntryCurrent); +dispatcher!("modify-current", CMDModifyCurrent => EntryModifyCurrent); -#[chain] -fn read(_prev: StateRead, resource: &MyResource) -> Next { - // Read the global resource - let current_dir = resource.current_dir.clone(); - ResultCurrentDir::new(current_dir).to_render() +// 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() } -#[renderer] -fn render_current_dir(dir: ResultCurrentDir) { - r_println!("Current dir: {}", dir.to_string_lossy()) +// Define renderer for output current path _____________ Injected resource +// / +#[renderer] // vvvvvvvvvvvvvv +fn render_current(_: EntryCurrent, current_dir: &ResCurrentDir) { + r_println!("Current directory: {}", current_dir.current_dir.display()); } gen_program!(); |
