diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-07-14 18:23:45 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-07-14 18:23:45 +0800 |
| commit | e66b1335a22df2a9f9555e59e30200644bb3483f (patch) | |
| tree | 1b3d4194548d543049a7bccf8c469a04011a81a5 | |
| parent | a18efb4aaaab51cb9ef1663f58286747e2e951ee (diff) | |
feat(mingling): add ParserStyle and re-export parselib module
Add a configurable `ParserStyle` struct defining option prefix,
separator
and case-sensitivity conventions, with built-in constants for Unix,
PowerShell and Windows styles. Provide a one-time global style setter.
Re-export the new `parselib` module from `mingling_picker` and wire it
through `mingling`'s `picker` facade. Also relocate `Groupped` and
`RenderResult` re-exports in `prelude` for better ordering.
| -rw-r--r-- | mingling/src/lib.rs | 12 | ||||
| -rw-r--r-- | mingling_picker/src/parselib.rs | 2 | ||||
| -rw-r--r-- | mingling_picker/src/parselib/style.rs | 86 |
3 files changed, 96 insertions, 4 deletions
diff --git a/mingling/src/lib.rs b/mingling/src/lib.rs index bee6fbb..49faceb 100644 --- a/mingling/src/lib.rs +++ b/mingling/src/lib.rs @@ -83,6 +83,10 @@ pub mod parser; #[cfg(feature = "picker")] pub mod picker { pub use mingling_picker::*; + + pub mod parselib { + pub use mingling_picker::parselib::*; + } } /// Re-export of all macros from `mingling_macros`. @@ -222,6 +226,10 @@ pub mod res; /// use mingling::prelude::*; /// ``` pub mod prelude { + /// Re-export of the `Groupped` derive macro for grouping types. + pub use crate::Groupped; + /// Re-export of the `RenderResult` struct for outputting rendering result + pub use crate::RenderResult; /// Re-export of the `chain` macro for defining a chain of commands. pub use crate::macros::chain; /// Re-export of the `dispatcher` macro for routing commands. @@ -238,10 +246,6 @@ pub mod prelude { pub use crate::macros::pack_err; /// Re-export of the `renderer` macro for defining renderer functions. pub use crate::macros::renderer; - /// Re-export of the `Groupped` derive macro for grouping types. - pub use crate::Groupped; - /// Re-export of the `RenderResult` struct for outputting rendering result - pub use crate::RenderResult; /// Like `pack_err!` but also marks the type for structured output #[cfg(all(feature = "structural_renderer", feature = "extra_macros"))] pub use mingling_macros::pack_err_structural; diff --git a/mingling_picker/src/parselib.rs b/mingling_picker/src/parselib.rs index e69de29..132045b 100644 --- a/mingling_picker/src/parselib.rs +++ b/mingling_picker/src/parselib.rs @@ -0,0 +1,2 @@ +mod style; +pub use style::*; diff --git a/mingling_picker/src/parselib/style.rs b/mingling_picker/src/parselib/style.rs new file mode 100644 index 0000000..c0eb58f --- /dev/null +++ b/mingling_picker/src/parselib/style.rs @@ -0,0 +1,86 @@ +use std::sync::OnceLock; +use std::sync::atomic::{AtomicBool, Ordering}; + +/// Defines the style of command-line argument parsing (prefixes, separators, etc.). +#[derive(Clone, Copy, PartialEq, Eq)] +pub struct ParserStyle<'a> { + /// End-of-options marker (e.g., `--`) + end_of_options: &'a str, + + /// Prefix for long options (e.g., `--` or `/`) + long_prefix: &'a str, + + /// Prefix for short options (e.g., `-` or `/`) + short_prefix: &'a str, + + /// Prefix for combined short flags (e.g., `-abc`) + combine_prefix: &'a str, + + /// Separator between name and value (e.g., `=` or `:`) + value_separator: char, + + /// Whether option names are case-sensitive + case_sensitive: bool, + + /// Whether combining short flags is allowed (e.g., `-abc` for `-a -b -c`) + allow_combine: bool, +} + +/// Unix-like style (e.g., `--verbose`, `-v`, `--name=value`) +pub const UNIX_STYLE: ParserStyle = ParserStyle { + end_of_options: "--", + long_prefix: "--", + short_prefix: "-", + combine_prefix: "-", + value_separator: '=', + case_sensitive: true, + allow_combine: true, +}; + +/// PowerShell style (e.g., `-Verbose`, `-Name:value`) +pub const POWERSHELL_STYLE: ParserStyle = ParserStyle { + end_of_options: "--", + long_prefix: "-", + short_prefix: "-", + combine_prefix: "-", + value_separator: ':', + case_sensitive: false, + allow_combine: false, +}; + +/// Windows-style command-line (e.g., `/Verbose`, `/Name:value`) +pub const WINDOWS_STYLE: ParserStyle = ParserStyle { + end_of_options: "--", + long_prefix: "/", + short_prefix: "/", + combine_prefix: "/", + value_separator: ':', + case_sensitive: false, + allow_combine: false, +}; + +static GLOBAL_STYLE: OnceLock<ParserStyle<'static>> = OnceLock::new(); +static GLOBAL_STYLE_SET: AtomicBool = AtomicBool::new(false); + +impl<'a> ParserStyle<'a> { + /// Sets the global parser style. + /// + /// This function can only be called once. Subsequent calls will have no effect. + /// The style is stored as a static reference; the provided style must be a static + /// constant (e.g., `&'static ParserStyle`). Use the built-in constants like + /// `UNIX_STYLE`, `POWERSHELL_STYLE`, or `WINDOWS_STYLE`. + pub fn set_global_style(style: &'static ParserStyle<'static>) { + if !GLOBAL_STYLE_SET.load(Ordering::Acquire) && GLOBAL_STYLE.set(*style).is_ok() { + GLOBAL_STYLE_SET.store(true, Ordering::Release); + } + } + + /// Returns the global parser style, or `None` if not yet set. + pub fn global_style() -> Option<&'static ParserStyle<'static>> { + if GLOBAL_STYLE_SET.load(Ordering::Acquire) { + GLOBAL_STYLE.get() + } else { + None + } + } +} |
