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/picker/structural_renderer.rs | 87 +++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 mingling/src/setups/picker/structural_renderer.rs (limited to 'mingling/src/setups/picker/structural_renderer.rs') 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