Skip to content

Commit af8afe0

Browse files
committed
refactor(ls): structure quoting style match
1 parent c365b44 commit af8afe0

File tree

1 file changed

+47
-28
lines changed

1 file changed

+47
-28
lines changed

src/uu/ls/src/ls.rs

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -662,43 +662,62 @@ fn extract_hyperlink(options: &clap::ArgMatches) -> bool {
662662
/// # Returns
663663
///
664664
/// * An option with None if the style string is invalid, or a `QuotingStyle` wrapped in `Some`.
665+
struct QuotingStyleSpec {
666+
style: QuotingStyle,
667+
fixed_control: bool,
668+
locale: Option<LocaleQuoting>,
669+
}
670+
671+
impl QuotingStyleSpec {
672+
fn new(style: QuotingStyle) -> Self {
673+
Self {
674+
style,
675+
fixed_control: false,
676+
locale: None,
677+
}
678+
}
679+
680+
fn with_locale(style: QuotingStyle, locale: LocaleQuoting) -> Self {
681+
Self {
682+
style,
683+
fixed_control: true,
684+
locale: Some(locale),
685+
}
686+
}
687+
}
688+
665689
fn match_quoting_style_name(
666690
style: &str,
667691
show_control: bool,
668692
) -> Option<(QuotingStyle, Option<LocaleQuoting>)> {
669-
let (qs, fixed_control, locale) = match style {
670-
"literal" => (
671-
QuotingStyle::Literal {
672-
show_control: false,
673-
},
674-
false,
675-
None,
676-
),
677-
"shell" => (QuotingStyle::SHELL, false, None),
678-
"shell-always" => (QuotingStyle::SHELL_QUOTE, false, None),
679-
"shell-escape" => (QuotingStyle::SHELL_ESCAPE, false, None),
680-
"shell-escape-always" => (QuotingStyle::SHELL_ESCAPE_QUOTE, false, None),
681-
"c" => (QuotingStyle::C_DOUBLE, false, None),
682-
"escape" => (QuotingStyle::C_NO_QUOTES, false, None),
683-
"locale" => (
684-
QuotingStyle::Literal {
693+
let spec = match style {
694+
"literal" => QuotingStyleSpec::new(QuotingStyle::Literal {
695+
show_control: false,
696+
}),
697+
"shell" => QuotingStyleSpec::new(QuotingStyle::SHELL),
698+
"shell-always" => QuotingStyleSpec::new(QuotingStyle::SHELL_QUOTE),
699+
"shell-escape" => QuotingStyleSpec::new(QuotingStyle::SHELL_ESCAPE),
700+
"shell-escape-always" => QuotingStyleSpec::new(QuotingStyle::SHELL_ESCAPE_QUOTE),
701+
"c" => QuotingStyleSpec::new(QuotingStyle::C_DOUBLE),
702+
"escape" => QuotingStyleSpec::new(QuotingStyle::C_NO_QUOTES),
703+
"locale" => QuotingStyleSpec {
704+
style: QuotingStyle::Literal {
685705
show_control: false,
686706
},
687-
true,
688-
Some(LocaleQuoting::Single),
689-
),
690-
"clocale" => (QuotingStyle::C_DOUBLE, true, Some(LocaleQuoting::Double)),
707+
fixed_control: true,
708+
locale: Some(LocaleQuoting::Single),
709+
},
710+
"clocale" => QuotingStyleSpec::with_locale(QuotingStyle::C_DOUBLE, LocaleQuoting::Double),
691711
_ => return None,
692712
};
693713

694-
Some((
695-
if fixed_control {
696-
qs
697-
} else {
698-
qs.show_control(show_control)
699-
},
700-
locale,
701-
))
714+
let style = if spec.fixed_control {
715+
spec.style
716+
} else {
717+
spec.style.show_control(show_control)
718+
};
719+
720+
Some((style, spec.locale))
702721
}
703722

704723
/// Extracts the quoting style to use based on the options provided.

0 commit comments

Comments
 (0)