Skip to content

Commit ce0cf7a

Browse files
authored
Merge pull request #5367 from sylvestre/join
join: remove a clippy::cognitive_complexity by moving some content in functions
2 parents 6c10934 + f843672 commit ce0cf7a

File tree

1 file changed

+66
-37
lines changed

1 file changed

+66
-37
lines changed

src/uu/join/src/join.rs

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -591,62 +591,82 @@ impl<'a> State<'a> {
591591
}
592592
}
593593

594-
#[uucore::main]
595-
#[allow(clippy::cognitive_complexity)]
596-
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
597-
let matches = uu_app().try_get_matches_from(args)?;
598-
599-
let keys = parse_field_number_option(matches.get_one::<String>("j").map(|s| s.as_str()))?;
600-
let key1 = parse_field_number_option(matches.get_one::<String>("1").map(|s| s.as_str()))?;
601-
let key2 = parse_field_number_option(matches.get_one::<String>("2").map(|s| s.as_str()))?;
594+
fn parse_separator(value_os: &OsString) -> UResult<Sep> {
595+
#[cfg(unix)]
596+
let value = value_os.as_bytes();
597+
#[cfg(not(unix))]
598+
let value = match value_os.to_str() {
599+
Some(value) => value.as_bytes(),
600+
None => {
601+
return Err(USimpleError::new(
602+
1,
603+
"unprintable field separators are only supported on unix-like platforms",
604+
));
605+
}
606+
};
607+
match value.len() {
608+
0 => Ok(Sep::Line),
609+
1 => Ok(Sep::Char(value[0])),
610+
2 if value[0] == b'\\' && value[1] == b'0' => Ok(Sep::Char(0)),
611+
_ => Err(USimpleError::new(
612+
1,
613+
format!("multi-character tab {}", value_os.to_string_lossy()),
614+
)),
615+
}
616+
}
602617

603-
let mut settings = Settings::default();
618+
fn parse_print_settings(matches: &clap::ArgMatches) -> UResult<(bool, bool, bool)> {
619+
let mut print_joined = true;
620+
let mut print_unpaired1 = false;
621+
let mut print_unpaired2 = false;
604622

605623
let v_values = matches.get_many::<String>("v");
606624
if v_values.is_some() {
607-
settings.print_joined = false;
625+
print_joined = false;
608626
}
609627

610628
let unpaired = v_values
611629
.unwrap_or_default()
612630
.chain(matches.get_many("a").unwrap_or_default());
613631
for file_num in unpaired {
614632
match parse_file_number(file_num)? {
615-
FileNum::File1 => settings.print_unpaired1 = true,
616-
FileNum::File2 => settings.print_unpaired2 = true,
633+
FileNum::File1 => print_unpaired1 = true,
634+
FileNum::File2 => print_unpaired2 = true,
617635
}
618636
}
619637

638+
Ok((print_joined, print_unpaired1, print_unpaired2))
639+
}
640+
641+
fn get_and_parse_field_number(matches: &clap::ArgMatches, key: &str) -> UResult<Option<usize>> {
642+
let value = matches.get_one::<String>(key).map(|s| s.as_str());
643+
parse_field_number_option(value)
644+
}
645+
646+
/// Parses the command-line arguments and constructs a `Settings` struct.
647+
///
648+
/// This function takes the matches from the command-line arguments, processes them,
649+
/// and returns a `Settings` struct that encapsulates the configuration for the program.
650+
#[allow(clippy::field_reassign_with_default)]
651+
fn parse_settings(matches: &clap::ArgMatches) -> UResult<Settings> {
652+
let keys = get_and_parse_field_number(matches, "j")?;
653+
let key1 = get_and_parse_field_number(matches, "1")?;
654+
let key2 = get_and_parse_field_number(matches, "2")?;
655+
656+
let (print_joined, print_unpaired1, print_unpaired2) = parse_print_settings(matches)?;
657+
658+
let mut settings = Settings::default();
659+
660+
settings.print_joined = print_joined;
661+
settings.print_unpaired1 = print_unpaired1;
662+
settings.print_unpaired2 = print_unpaired2;
663+
620664
settings.ignore_case = matches.get_flag("i");
621665
settings.key1 = get_field_number(keys, key1)?;
622666
settings.key2 = get_field_number(keys, key2)?;
623-
624667
if let Some(value_os) = matches.get_one::<OsString>("t") {
625-
#[cfg(unix)]
626-
let value = value_os.as_bytes();
627-
#[cfg(not(unix))]
628-
let value = match value_os.to_str() {
629-
Some(value) => value.as_bytes(),
630-
None => {
631-
return Err(USimpleError::new(
632-
1,
633-
"unprintable field separators are only supported on unix-like platforms",
634-
))
635-
}
636-
};
637-
settings.separator = match value.len() {
638-
0 => Sep::Line,
639-
1 => Sep::Char(value[0]),
640-
2 if value[0] == b'\\' && value[1] == b'0' => Sep::Char(0),
641-
_ => {
642-
return Err(USimpleError::new(
643-
1,
644-
format!("multi-character tab {}", value_os.to_string_lossy()),
645-
))
646-
}
647-
};
668+
settings.separator = parse_separator(value_os)?;
648669
}
649-
650670
if let Some(format) = matches.get_one::<String>("o") {
651671
if format == "auto" {
652672
settings.autoformat = true;
@@ -677,6 +697,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
677697

678698
settings.line_ending = LineEnding::from_zero_flag(matches.get_flag("z"));
679699

700+
Ok(settings)
701+
}
702+
703+
#[uucore::main]
704+
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
705+
let matches = uu_app().try_get_matches_from(args)?;
706+
707+
let settings = parse_settings(&matches)?;
708+
680709
let file1 = matches.get_one::<String>("file1").unwrap();
681710
let file2 = matches.get_one::<String>("file2").unwrap();
682711

0 commit comments

Comments
 (0)