diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-04-28 22:39:59 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-04-28 22:39:59 +0800 |
| commit | 85ee549f68449bc70a7f1271a93ad26a8207ee40 (patch) | |
| tree | bfb0b678d0f96c06b196417fd612a9cad2baf7fe /docs/pages/2-basic/2-setup.md | |
| parent | 5bf4209bd138faf76e3bd316fdfa128a08f2bb2e (diff) | |
Rebuild and rewrite the documentation site infrastructure
Diffstat (limited to 'docs/pages/2-basic/2-setup.md')
| -rw-r--r-- | docs/pages/2-basic/2-setup.md | 150 |
1 files changed, 0 insertions, 150 deletions
diff --git a/docs/pages/2-basic/2-setup.md b/docs/pages/2-basic/2-setup.md deleted file mode 100644 index 102e213..0000000 --- a/docs/pages/2-basic/2-setup.md +++ /dev/null @@ -1,150 +0,0 @@ -<h1 align="center">Setup</h1> -<p align="center"> - Mingling's Basic Components -</p> - ---- - -## Intro - -`Setup` is used to organize and package the initialization process of a `Program`, making the project easier to manage. - -## Usage - -It is defined as follows: - -```rust -struct MySetup; -impl ProgramSetup<ThisProgram> - for MySetup -{ - fn setup( - &mut self, - program: &mut Program<ThisProgram> - ) { - // Your setup logic - } -} -``` - -For example: - -```rust -use std::{env::current_dir, path::PathBuf}; - -use mingling::{ - Program, - macros::{dispatcher, gen_program, renderer}, - setup::ProgramSetup, -}; - -// Global state -static OUTPUT_PATH: std::sync::OnceLock<PathBuf> - = std::sync::OnceLock::new(); - -fn main() { - let mut program = ThisProgram::new(); - program.with_setup(MySetup); - program.exec(); -} - -// Define two Dispatchers using `dispatcher!` -dispatcher!("member.add", - AddMemberCommand => AddMemberEntry); -dispatcher!("member.rm", - RemoveMemberCommand => RemoveMemberEntry); - -struct MySetup; -impl ProgramSetup<ThisProgram, ThisProgram> for MySetup { - fn setup( - &mut self, program: &mut Program<ThisProgram, ThisProgram> - ) { - // Register Dispatchers - program.with_dispatcher(AddMemberCommand); - program.with_dispatcher(RemoveMemberCommand); - - // Initialize global output once - OUTPUT_PATH.get_or_init(|| current_dir().unwrap()); - - // Pick the "--quiet" or "-q" flag - program.global_flag(["--quiet", "-q"], |p| { - // Disable render output - p.stdout_setting.render_output = false; - }); - - // Pick the "--output" or "-O" flag, write to output - program.global_argument(["--output", "-O"], |_, v| { - let _ = OUTPUT_PATH.set(PathBuf::from(v)); - }); - } -} - -gen_program!(); -``` - -## Simplified Syntax - -If you find the above declaration method too **verbose**, you can use the `program_setup!` macro to simplify it. The format is: - -```rust -#[program_setup] -fn my_setup( - program: &mut Program<ThisProgram> -) { - // Your setup logic -} -``` - -For example: - -```rust -use std::{env::current_dir, path::PathBuf}; - -use mingling::{ - Program, - macros::{ - dispatcher, - gen_program, - program_setup, - renderer - }, -}; - -static OUTPUT_PATH: std::sync::OnceLock<PathBuf> - = std::sync::OnceLock::new(); - -#[tokio::main] -async fn main() { - let mut program = ThisProgram::new(); - program.with_setup(MySetup); - program.exec().await; -} - -dispatcher!("member.add", - AddMemberCommand => AddMemberEntry); -dispatcher!("member.rm", - RemoveMemberCommand => RemoveMemberEntry); - -#[program_setup] -fn my_setup( - program: &mut Program<ThisProgram> -) { - program.with_dispatcher(AddMemberCommand); - program.with_dispatcher(RemoveMemberCommand); - - OUTPUT_PATH.get_or_init(|| current_dir().unwrap()); - - program.global_flag(["--quiet", "-q"], |p| { - p.stdout_setting.render_output = false; - }); - - program.global_argument(["--output", "-O"], |_, v| { - let _ = OUTPUT_PATH.set(PathBuf::from(v)); - }); -} - -gen_program!(); -``` - -## 💡 Next Page -> **Basic Component** - Dispatcher [Go](./pages/2-basic/3-dispatcher) |
