From 5e460470f6ea2ce31a403b6e936ec2fe8f45312a Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sun, 19 Jul 2026 00:22:23 +0800 Subject: feat: migrate mingling-specific picker code into mingling crate BREAKING CHANGE: Remove `mingling_support` feature from arg-picker crate. The `EntryPicker` trait and `corebind` module are now part of the mingling crate directly, and `build_pattern1` is now a public method. --- mingling/src/setups/picker.rs | 3 - mingling/src/setups/picker/basic.rs | 39 ++----- mingling/src/setups/picker/consts.rs | 118 ---------------------- mingling/src/setups/picker/structural_renderer.rs | 93 ++++++++--------- 4 files changed, 46 insertions(+), 207 deletions(-) delete mode 100644 mingling/src/setups/picker/consts.rs (limited to 'mingling/src/setups') diff --git a/mingling/src/setups/picker.rs b/mingling/src/setups/picker.rs index 3c0d1c8..0b7bd33 100644 --- a/mingling/src/setups/picker.rs +++ b/mingling/src/setups/picker.rs @@ -5,6 +5,3 @@ pub use basic::*; 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 index 318edbd..b8cc237 100644 --- a/mingling/src/setups/picker/basic.rs +++ b/mingling/src/setups/picker/basic.rs @@ -1,27 +1,11 @@ -use arg_picker::{IntoPicker, PickerArg, value::Flag}; +use arg_picker::{PickerArg, value::Flag}; use mingling_core::{Program, ProgramCollect, setup::ProgramSetup}; use crate::{ - setup::picker::REMAINS, - setups::picker::{CONFIRM_FLAG, HELP_FLAG, QUIET_FLAG}, + consts::{CONFIRM_FLAG, HELP_FLAG, QUIET_FLAG}, + picker::pick_global_flag, }; -/// Helper: picks a boolean flag from the program arguments, calls `f` with the -/// flag value, then replaces the program arguments with the remaining args. -fn pick_flag<'a, C>( - program: &mut Program, - flag: &PickerArg<'a, Flag>, - f: impl FnOnce(bool, &mut Program), -) where - C: ProgramCollect, -{ - let args = program.take_args(); - let remains_arg = PickerArg::>::new(&[], None, true); - let (active, remains) = args.pick(flag).pick(&remains_arg).unwrap(); - f(*active, program); - program.replace_args(remains.into()); -} - /// Performs basic program initialization: /// /// - Collects `--quiet` flag to control message rendering @@ -59,9 +43,7 @@ where C: ProgramCollect, { fn setup(self, program: &mut Program) { - pick_flag(program, self.flag, |active, ctx| { - ctx.user_context.help = active; - }); + pick_global_flag(program, self.flag); } } @@ -90,12 +72,7 @@ where C: ProgramCollect, { fn setup(self, program: &mut Program) { - pick_flag(program, self.flag, |active, ctx| { - if active { - ctx.stdout_setting.render_output = false; - ctx.stdout_setting.error_output = false; - } - }); + pick_global_flag(program, self.flag); } } @@ -124,11 +101,7 @@ where C: ProgramCollect, { fn setup(self, program: &mut Program) { - pick_flag(program, self.flag, |active, ctx| { - if active { - ctx.user_context.confirm = true; - } - }); + pick_global_flag(program, self.flag); } } diff --git a/mingling/src/setups/picker/consts.rs b/mingling/src/setups/picker/consts.rs deleted file mode 100644 index 5e93f3f..0000000 --- a/mingling/src/setups/picker/consts.rs +++ /dev/null @@ -1,118 +0,0 @@ -use std::marker::PhantomData; - -use arg_picker::{PickerArg, PickerArgs, value::Flag}; - -/// Remaining positional arguments (anything not consumed as an option). -/// - `full`: `[]` (empty — not triggered by any `--` prefix). -/// - `short`: (none) -/// - `positional`: `false` (this is a meta‑argument that collects everything left). -/// This constant is used internally to access any leftover arguments after -/// all defined flags/options have been processed. -pub const REMAINS: PickerArg = PickerArg:: { - full: &[], - short: None, - positional: false, - internal_type: PhantomData, -}; - -/// 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 index 8e5eac6..1fa48fd 100644 --- a/mingling/src/setups/picker/structural_renderer.rs +++ b/mingling/src/setups/picker/structural_renderer.rs @@ -1,11 +1,8 @@ -use arg_picker::IntoPicker; use mingling_core::{Program, ProgramCollect, setup::ProgramSetup}; use crate::{ - setup::picker::REMAINS, - setups::picker::{ - JSON_FLAG, JSON_PRETTY_FLAG, RON_FLAG, RON_PRETTY_FLAG, TOML_FLAG, YAML_FLAG, - }, + consts::RENDERER_ARG, + picker::{pick_global_argument, pick_global_flag}, }; /// Sets up the structural renderer for the program: @@ -18,9 +15,9 @@ where C: ProgramCollect, { fn setup(self, program: &mut Program) { - program.global_argument("--renderer", |p, renderer| { - p.structural_renderer_name = renderer.into(); - }); + if let Some(renderer) = pick_global_argument(program, &RENDERER_ARG) { + program.structural_renderer_name = renderer.into(); + } } } @@ -33,6 +30,17 @@ where /// * `--toml` for TOML output /// * `--ron` for RON output /// * `--ron-pretty` for pretty-printed RON output +/// +/// # Flag priority +/// +/// If multiple flags are specified, the last matching flag in the following +/// declaration order takes precedence: +/// 1. `--json` +/// 2. `--json-pretty` +/// 3. `--yaml` +/// 4. `--toml` +/// 5. `--ron` +/// 6. `--ron-pretty` pub struct StructuralRendererSetup; impl ProgramSetup for StructuralRendererSetup @@ -40,50 +48,29 @@ 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); + #[cfg(feature = "json_serde_fmt")] + if pick_global_flag(program, &crate::consts::JSON_FLAG) { + program.structural_renderer_name = crate::StructuralRendererSetting::Json; + } + #[cfg(feature = "json_serde_fmt")] + if pick_global_flag(program, &crate::consts::JSON_PRETTY_FLAG) { + program.structural_renderer_name = crate::StructuralRendererSetting::JsonPretty; + } + #[cfg(feature = "yaml_serde_fmt")] + if pick_global_flag(program, &crate::consts::YAML_FLAG) { + program.structural_renderer_name = crate::StructuralRendererSetting::Yaml; + } + #[cfg(feature = "toml_serde_fmt")] + if pick_global_flag(program, &crate::consts::TOML_FLAG) { + program.structural_renderer_name = crate::StructuralRendererSetting::Toml; + } + #[cfg(feature = "ron_serde_fmt")] + if pick_global_flag(program, &crate::consts::RON_FLAG) { + program.structural_renderer_name = crate::StructuralRendererSetting::Ron; + } + #[cfg(feature = "ron_serde_fmt")] + if pick_global_flag(program, &crate::consts::RON_PRETTY_FLAG) { + program.structural_renderer_name = crate::StructuralRendererSetting::RonPretty; + } } } - -fn process_renderer_flags(args: Vec, program: &mut Program) -> Vec -where - C: ProgramCollect, -{ - 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) - .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