aboutsummaryrefslogtreecommitdiff
path: root/mingling_picker/src/parselib/style.rs
blob: 4ea161f0bec7e15961c89ac7cc412026b135c979 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use std::sync::OnceLock;
use std::sync::atomic::{AtomicBool, Ordering};

use crate::parselib::ParserStyleNamingCase::{Kebab, Pascal};

/// 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., `--`)
    pub end_of_options: &'a str,

    /// Prefix for long options (e.g., `--` or `/`)
    pub long_prefix: &'a str,

    /// Prefix for short options (e.g., `-` or `/`)
    pub short_prefix: &'a str,

    /// Prefix for combined short flags (e.g., `-abc`)
    pub combine_prefix: &'a str,

    /// Separator between name and value (e.g., `=` or `:`)
    pub value_separator: char,

    /// Whether option names are case-sensitive
    pub case_sensitive: bool,

    /// Whether combining short flags is allowed (e.g., `-abc` for `-a -b -c`)
    pub allow_combine: bool,

    /// Naming case
    pub naming_case: ParserStyleNamingCase,
}

impl<'a> ParserStyle<'a> {
    /// Formats a flag (short or long) into a full command-line option string.
    ///
    /// This method takes any type that can be converted into a `FlagStr` and produces
    /// a complete option string by prepending the appropriate prefix.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use mingling_picker::parselib::{ParserStyle, FlagStr, UNIX_STYLE};
    /// let style = &UNIX_STYLE;
    ///
    /// assert_eq!(style.flag_string('v'), "-v");
    /// assert_eq!(style.flag_string("verbose"), "--verbose");
    /// ```
    ///
    /// # Parameters
    ///
    /// * `flag` - A value that can be converted to `FlagStr`, either a `char` for short flags
    ///   or a `&str` for long flags.
    ///
    /// # Returns
    ///
    /// A `String` with the prefix and the flag name combined.
    #[must_use]
    #[inline(always)]
    pub fn flag_string<F>(&self, flag: F) -> String
    where
        F: Into<FlagStr<'a>>,
    {
        match flag.into() {
            FlagStr::Short(short) => format!("{}{}", self.short_prefix, short),
            FlagStr::Long(long) => format!("{}{}", self.long_prefix, long),
        }
    }
}

/// Represents a flag name for command-line argument parsing.
///
/// This enum can hold either a short flag (a single character, e.g., `'v'` for `-v`)
/// or a long flag (a string, e.g., `"verbose"` for `--verbose`).
///
/// # Examples
///
/// ```
/// use mingling_picker::parselib::FlagStr;
///
/// let short: FlagStr = 'v'.into();
/// let long: FlagStr = "verbose".into();
/// ```
pub enum FlagStr<'a> {
    /// A short flag represented by a single character.
    Short(char),
    /// A long flag represented by a string slice.
    Long(&'a str),
}

impl<'a> From<char> for FlagStr<'a> {
    /// Converts a single character into a `FlagStr::Short`.
    fn from(c: char) -> Self {
        FlagStr::Short(c)
    }
}

impl<'a> From<&'a str> for FlagStr<'a> {
    /// Converts a string slice into a `FlagStr::Long`.
    fn from(s: &'a str) -> Self {
        FlagStr::Long(s)
    }
}

impl<'a> From<&'a String> for FlagStr<'a> {
    /// Converts a reference to a `String` into a `FlagStr::Long`.
    fn from(s: &'a String) -> Self {
        FlagStr::Long(s.as_str())
    }
}

#[repr(u8)]
#[derive(Default, Clone, Copy, PartialEq, Eq)]
pub enum ParserStyleNamingCase {
    /// snake_case format (e.g., `brew_coffee`)
    #[default]
    Snake,
    /// camelCase format (e.g., `brewCoffee`)
    Camel,
    /// PascalCase format (e.g., `BrewCoffee`)
    Pascal,
    /// kebab-case format (e.g., `brew-coffee`)
    Kebab,
    /// dot.case format (e.g., `brew.coffee`)
    Dot,
    /// Title Case format (e.g., `Brew Coffee`)
    Title,
    /// lower case format (e.g., `brew coffee`)
    Lower,
    /// UPPER CASE format (e.g., `BREW COFFEE`)
    Upper,
}

impl ParserStyleNamingCase {
    /// Converts the input string `s` to the naming case represented by this variant.
    ///
    /// This method takes any type `S` that can be converted into a `String` and
    /// produced from a `String`, applies the corresponding case transformation,
    /// and returns the result.
    ///
    /// # Examples
    ///
    /// ```
    /// use mingling_picker::parselib::ParserStyleNamingCase;
    ///
    /// let camel = ParserStyleNamingCase::Camel;
    /// assert_eq!(camel.convert("brew_coffee".to_string()), "brewCoffee");
    ///
    /// let kebab = ParserStyleNamingCase::Kebab;
    /// assert_eq!(kebab.convert("BrewCoffee".to_string()), "brew-coffee");
    /// ```
    pub fn convert<S>(&self, s: S) -> S
    where
        S: Into<String> + From<String>,
    {
        match self {
            ParserStyleNamingCase::Camel => just_fmt::camel_case!(s.into()).into(),
            ParserStyleNamingCase::Pascal => just_fmt::pascal_case!(s.into()).into(),
            ParserStyleNamingCase::Kebab => just_fmt::kebab_case!(s.into()).into(),
            ParserStyleNamingCase::Snake => just_fmt::snake_case!(s.into()).into(),
            ParserStyleNamingCase::Dot => just_fmt::dot_case!(s.into()).into(),
            ParserStyleNamingCase::Title => just_fmt::title_case!(s.into()).into(),
            ParserStyleNamingCase::Lower => just_fmt::lower_case!(s.into()).into(),
            ParserStyleNamingCase::Upper => just_fmt::upper_case!(s.into()).into(),
        }
    }
}

/// 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,
    naming_case: Kebab,
};

/// 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,
    naming_case: Pascal,
};

/// 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,
    naming_case: Pascal,
};

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, falling back to `UNIX_STYLE` if not set.
    pub fn global_style() -> &'static ParserStyle<'static> {
        GLOBAL_STYLE.get().unwrap_or(&UNIX_STYLE)
    }
}