aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-07-18 06:12:31 +0800
committer魏曹先生 <1992414357@qq.com>2026-07-18 06:12:31 +0800
commit8d79bf3a2eceafc2316f3eb78140995cbaf072f1 (patch)
tree742688e97b4ab0081dd0792fcefd355187c8669b
parent92127d6c2929057eb909427b96d4ff4abd566dd0 (diff)
feat(setups): add picker module with flag-based program setup
-rw-r--r--mingling/src/setups.rs3
-rw-r--r--mingling/src/setups/picker.rs10
-rw-r--r--mingling/src/setups/picker/basic.rs128
-rw-r--r--mingling/src/setups/picker/consts.rs105
-rw-r--r--mingling/src/setups/picker/structural_renderer.rs87
5 files changed, 333 insertions, 0 deletions
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<C> ProgramSetup<C> for BasicProgramSetup
+where
+ C: ProgramCollect<Enum = C>,
+{
+ fn setup(self, program: &mut Program<C>) {
+ 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<Flag>) -> Self {
+ Self { flag }
+ }
+}
+
+impl<'a, C> ProgramSetup<C> for HelpFlagSetup<'a>
+where
+ C: ProgramCollect<Enum = C>,
+{
+ fn setup(self, program: &mut Program<C>) {
+ let args = program.take_args();
+ let remains_arg = PickerArg::<PickerArgs<'a>>::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<Flag>) -> Self {
+ Self { flag }
+ }
+}
+
+impl<'a, C> ProgramSetup<C> for QuietFlagSetup<'a>
+where
+ C: ProgramCollect<Enum = C>,
+{
+ fn setup(self, program: &mut Program<C>) {
+ let args = program.take_args();
+ let remains_arg = PickerArg::<PickerArgs<'a>>::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<Flag>) -> Self {
+ Self { flag }
+ }
+}
+
+impl<'a, C> ProgramSetup<C> for ConfirmFlagSetup<'a>
+where
+ C: ProgramCollect<Enum = C>,
+{
+ fn setup(self, program: &mut Program<C>) {
+ let args = program.take_args();
+ let remains_arg = PickerArg::<PickerArgs<'a>>::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<Flag> = PickerArg::<Flag> {
+ full: &["help"],
+ short: Some('h'),
+ positional: false,
+ internal_type: PhantomData,
+};
+
+/// Quiet flag: suppress output.
+/// - `full`: `["quiet"]`
+/// - `short`: `'q'`
+pub const QUIET_FLAG: PickerArg<Flag> = PickerArg::<Flag> {
+ 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<Flag> = PickerArg::<Flag> {
+ 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<Flag> = PickerArg::<Flag> {
+ 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<Flag> = PickerArg::<Flag> {
+ 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<Flag> = PickerArg::<Flag> {
+ 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<Flag> = PickerArg::<Flag> {
+ 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<Flag> = PickerArg::<Flag> {
+ 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<Flag> = PickerArg::<Flag> {
+ 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<C> ProgramSetup<C> for StructuralRendererSimpleSetup
+where
+ C: ProgramCollect<Enum = C>,
+{
+ fn setup(self, program: &mut Program<C>) {
+ 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<C> ProgramSetup<C> for StructuralRendererSetup
+where
+ C: ProgramCollect<Enum = C>,
+{
+ fn setup(self, program: &mut Program<C>) {
+ let args = program.take_args();
+ let args = process_renderer_flags(args, program);
+ program.replace_args(args);
+ }
+}
+
+fn process_renderer_flags<C>(args: Vec<String>, program: &mut Program<C>) -> Vec<String>
+where
+ C: ProgramCollect<Enum = C>,
+{
+ let remains_arg = PickerArg::<PickerArgs>::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()
+}