From 8d79bf3a2eceafc2316f3eb78140995cbaf072f1 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sat, 18 Jul 2026 06:12:31 +0800 Subject: feat(setups): add picker module with flag-based program setup --- mingling/src/setups.rs | 3 + mingling/src/setups/picker.rs | 10 ++ mingling/src/setups/picker/basic.rs | 128 ++++++++++++++++++++++ mingling/src/setups/picker/consts.rs | 105 ++++++++++++++++++ mingling/src/setups/picker/structural_renderer.rs | 87 +++++++++++++++ 5 files changed, 333 insertions(+) create mode 100644 mingling/src/setups/picker.rs create mode 100644 mingling/src/setups/picker/basic.rs create mode 100644 mingling/src/setups/picker/consts.rs create mode 100644 mingling/src/setups/picker/structural_renderer.rs (limited to 'mingling') diff --git a/mingling/src/setups.rs b/mingling/src/setups.rs index e572cf5..a1290ff 100644 --- a/mingling/src/setups.rs +++ b/mingling/src/setups.rs @@ -7,6 +7,9 @@ pub use dirs::*; mod exit_code; pub use exit_code::*; +#[cfg(feature = "picker")] +pub mod picker; + #[cfg(feature = "structural_renderer")] mod structural_renderer; diff --git a/mingling/src/setups/picker.rs b/mingling/src/setups/picker.rs new file mode 100644 index 0000000..3c0d1c8 --- /dev/null +++ b/mingling/src/setups/picker.rs @@ -0,0 +1,10 @@ +mod basic; +pub use basic::*; + +#[cfg(feature = "structural_renderer")] +mod structural_renderer; +#[cfg(feature = "structural_renderer")] +pub use structural_renderer::*; + +mod consts; +pub use consts::*; diff --git a/mingling/src/setups/picker/basic.rs b/mingling/src/setups/picker/basic.rs new file mode 100644 index 0000000..6a301c7 --- /dev/null +++ b/mingling/src/setups/picker/basic.rs @@ -0,0 +1,128 @@ +use arg_picker::{IntoPicker, PickerArg, PickerArgs, value::Flag}; +use mingling_core::{Program, ProgramCollect, setup::ProgramSetup}; + +use crate::setups::picker::{CONFIRM_FLAG, HELP_FLAG, QUIET_FLAG}; + +/// Performs basic program initialization: +/// +/// - Collects `--quiet` flag to control message rendering +/// - Collects `--help` flag to enable help mode +/// - Collects `--confirm` flag to skip user confirmation +pub struct BasicProgramSetup; + +impl ProgramSetup for BasicProgramSetup +where + C: ProgramCollect, +{ + fn setup(self, program: &mut Program) { + program.with_setup(HelpFlagSetup::default()); + program.with_setup(QuietFlagSetup::default()); + program.with_setup(ConfirmFlagSetup::default()); + } +} + +/// Provides setup for parsing the user help flag +/// +/// The default value is `--help / -h` +pub struct HelpFlagSetup<'a> { + flag: &'a PickerArg<'a, Flag>, +} + +impl<'a> HelpFlagSetup<'a> { + /// Creates a new `HelpFlagSetup` with the given flag aliases. + pub fn new(flag: &'a PickerArg) -> Self { + Self { flag } + } +} + +impl<'a, C> ProgramSetup for HelpFlagSetup<'a> +where + C: ProgramCollect, +{ + fn setup(self, program: &mut Program) { + let args = program.take_args(); + let remains_arg = PickerArg::>::new(&[], None, true); + let (active, remains) = args.pick(self.flag).pick(&remains_arg).unwrap(); + program.user_context.help = *active; + program.replace_args(remains.into()); + } +} + +impl<'a> Default for HelpFlagSetup<'a> { + fn default() -> Self { + Self { flag: &HELP_FLAG } + } +} + +/// Provides setup for parsing the quiet flag +/// +/// The default value is `--quiet / -q` +pub struct QuietFlagSetup<'a> { + flag: &'a PickerArg<'a, Flag>, +} + +impl<'a> QuietFlagSetup<'a> { + /// Creates a new `QuietFlagSetup` with the given flag aliases. + pub fn new(flag: &'a PickerArg) -> Self { + Self { flag } + } +} + +impl<'a, C> ProgramSetup for QuietFlagSetup<'a> +where + C: ProgramCollect, +{ + fn setup(self, program: &mut Program) { + let args = program.take_args(); + let remains_arg = PickerArg::>::new(&[], None, true); + let (active, remains) = args.pick(self.flag).pick(&remains_arg).unwrap(); + if *active { + program.stdout_setting.render_output = false; + program.stdout_setting.error_output = false; + } + program.replace_args(remains.into()); + } +} + +impl<'a> Default for QuietFlagSetup<'a> { + fn default() -> Self { + Self { flag: &QUIET_FLAG } + } +} + +/// Provides setup for parsing the confirm flag +/// +/// The default value is `--confirm / -C` +pub struct ConfirmFlagSetup<'a> { + flag: &'a PickerArg<'a, Flag>, +} + +impl<'a> ConfirmFlagSetup<'a> { + /// Creates a new `ConfirmFlagSetup` with the given flag aliases. + pub fn new(flag: &'a PickerArg) -> Self { + Self { flag } + } +} + +impl<'a, C> ProgramSetup for ConfirmFlagSetup<'a> +where + C: ProgramCollect, +{ + fn setup(self, program: &mut Program) { + let args = program.take_args(); + let remains_arg = PickerArg::>::new(&[], None, true); + let (active, remains) = args.pick(self.flag).pick(&remains_arg).unwrap(); + if *active { + program.user_context.confirm = true; + } + program.replace_args(remains.into()); + } +} + +impl<'a> Default for ConfirmFlagSetup<'a> { + fn default() -> Self { + Self { + flag: &CONFIRM_FLAG, + } + } +} diff --git a/mingling/src/setups/picker/consts.rs b/mingling/src/setups/picker/consts.rs new file mode 100644 index 0000000..e254b4f --- /dev/null +++ b/mingling/src/setups/picker/consts.rs @@ -0,0 +1,105 @@ +use std::marker::PhantomData; + +use arg_picker::{PickerArg, value::Flag}; + +/// Help flag: display usage information. +/// - `full`: `["help"]` +/// - `short`: `'h'` +pub const HELP_FLAG: PickerArg = PickerArg:: { + full: &["help"], + short: Some('h'), + positional: false, + internal_type: PhantomData, +}; + +/// Quiet flag: suppress output. +/// - `full`: `["quiet"]` +/// - `short`: `'q'` +pub const QUIET_FLAG: PickerArg = PickerArg:: { + full: &["quiet"], + short: Some('q'), + positional: false, + internal_type: PhantomData, +}; + +/// Confirm flag: require user confirmation before proceeding. +/// - `full`: `["confirm"]` +/// - `short`: `'C'` +pub const CONFIRM_FLAG: PickerArg = PickerArg:: { + full: &["confirm"], + short: Some('C'), + positional: false, + internal_type: PhantomData, +}; + +/// JSON flag: enable JSON output format. +/// - `full`: `["json"]` +/// - `short`: (none) +/// Available only when the `json_serde_fmt` feature is enabled. +#[cfg(feature = "json_serde_fmt")] +pub const JSON_FLAG: PickerArg = PickerArg:: { + full: &["json"], + short: None, + positional: false, + internal_type: PhantomData, +}; + +/// JSON pretty flag: enable pretty-printed JSON output format. +/// - `full`: `["json_pretty"]` +/// - `short`: (none) +/// Available only when the `json_serde_fmt` feature is enabled. +#[cfg(feature = "json_serde_fmt")] +pub const JSON_PRETTY_FLAG: PickerArg = PickerArg:: { + full: &["json_pretty"], + short: None, + positional: false, + internal_type: PhantomData, +}; + +/// YAML flag: enable YAML output format. +/// - `full`: `["yaml"]` +/// - `short`: (none) +/// Available only when the `yaml_serde_fmt` feature is enabled. +#[cfg(feature = "yaml_serde_fmt")] +pub const YAML_FLAG: PickerArg = PickerArg:: { + full: &["yaml"], + short: None, + positional: false, + internal_type: PhantomData, +}; + +/// TOML flag: enable TOML output format. +/// - `full`: `["toml"]` +/// - `short`: (none) +/// Available only when the `toml_serde_fmt` feature is enabled. +#[cfg(feature = "toml_serde_fmt")] +pub const TOML_FLAG: PickerArg = PickerArg:: { + full: &["toml"], + short: None, + positional: false, + internal_type: PhantomData, +}; + +/// RON flag: enable RON output format. +/// - `full`: `["ron"]` +/// - `short`: (none) +/// Available only when the `ron_serde_fmt` feature is enabled. +#[cfg(feature = "ron_serde_fmt")] +pub const RON_FLAG: PickerArg = PickerArg:: { + full: &["ron"], + short: None, + positional: false, + internal_type: PhantomData, +}; + +/// RON pretty flag: enable pretty-printed RON output format. +/// - `full`: `["ron_pretty"]` +/// - `short`: (none) +/// Available only when the `ron_serde_fmt` feature is enabled. +#[cfg(feature = "ron_serde_fmt")] +pub const RON_PRETTY_FLAG: PickerArg = PickerArg:: { + full: &["ron_pretty"], + short: None, + positional: false, + internal_type: PhantomData, +}; diff --git a/mingling/src/setups/picker/structural_renderer.rs b/mingling/src/setups/picker/structural_renderer.rs new file mode 100644 index 0000000..69f05f1 --- /dev/null +++ b/mingling/src/setups/picker/structural_renderer.rs @@ -0,0 +1,87 @@ +use arg_picker::{IntoPicker, PickerArg, PickerArgs}; +use mingling_core::{Program, ProgramCollect, setup::ProgramSetup}; + +use crate::setups::picker::{ + JSON_FLAG, JSON_PRETTY_FLAG, RON_FLAG, RON_PRETTY_FLAG, TOML_FLAG, YAML_FLAG, +}; + +/// Sets up the structural renderer for the program: +/// +/// - Adds a `--renderer` global argument to specify the renderer type +pub struct StructuralRendererSimpleSetup; + +impl ProgramSetup for StructuralRendererSimpleSetup +where + C: ProgramCollect, +{ + fn setup(self, program: &mut Program) { + program.global_argument("--renderer", |p, renderer| { + p.structural_renderer_name = renderer.into(); + }); + } +} + +/// Sets up the structural renderer for the program: +/// +/// - Adds global flags to specify the renderer type: +/// * `--json` for JSON output +/// * `--json-pretty` for pretty-printed JSON output +/// * `--yaml` for YAML output +/// * `--toml` for TOML output +/// * `--ron` for RON output +/// * `--ron-pretty` for pretty-printed RON output +pub struct StructuralRendererSetup; + +impl ProgramSetup for StructuralRendererSetup +where + C: ProgramCollect, +{ + fn setup(self, program: &mut Program) { + let args = program.take_args(); + let args = process_renderer_flags(args, program); + program.replace_args(args); + } +} + +fn process_renderer_flags(args: Vec, program: &mut Program) -> Vec +where + C: ProgramCollect, +{ + let remains_arg = PickerArg::::new(&[], None, true); + let (json, json_pretty, yaml, toml, ron, ron_pretty, remains) = args + .pick(&JSON_FLAG) + .pick(&JSON_PRETTY_FLAG) + .pick(&YAML_FLAG) + .pick(&TOML_FLAG) + .pick(&RON_FLAG) + .pick(&RON_PRETTY_FLAG) + .pick(&remains_arg) + .unwrap(); + + #[cfg(feature = "json_serde_fmt")] + if *json { + program.structural_renderer_name = crate::StructuralRendererSetting::Json; + } + #[cfg(feature = "json_serde_fmt")] + if *json_pretty { + program.structural_renderer_name = crate::StructuralRendererSetting::JsonPretty; + } + #[cfg(feature = "yaml_serde_fmt")] + if *yaml { + program.structural_renderer_name = crate::StructuralRendererSetting::Yaml; + } + #[cfg(feature = "toml_serde_fmt")] + if *toml { + program.structural_renderer_name = crate::StructuralRendererSetting::Toml; + } + #[cfg(feature = "ron_serde_fmt")] + if *ron { + program.structural_renderer_name = crate::StructuralRendererSetting::Ron; + } + #[cfg(feature = "ron_serde_fmt")] + if *ron_pretty { + program.structural_renderer_name = crate::StructuralRendererSetting::RonPretty; + } + + remains.into() +} -- cgit