Skip to content

Commit

Permalink
cat: refactor get_parentdir_and_file helper fn
Browse files Browse the repository at this point in the history
no need to convert parent and filename to strl only to join them as strings. Join directly.
  • Loading branch information
jqnatividad committed Dec 31, 2023
1 parent 68061dc commit 117f08f
Showing 1 changed file with 5 additions and 19 deletions.
24 changes: 5 additions & 19 deletions src/cmd/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,25 +118,11 @@ fn get_parentdir_and_file<P: AsRef<Path>>(path: P, stem_only: bool) -> Option<St
path.file_stem()
} else {
path.file_name()
};

let file_name = file_info.and_then(|f| f.to_str());

let parent_dir = path
.parent()
.and_then(|p| p.to_str())
.filter(|s| !s.is_empty());

match (parent_dir, file_name) {
(Some(parent_dir), Some(file_name)) => Some(
Path::new(parent_dir)
.join(file_name)
.to_string_lossy()
.into_owned(),
),
(None, Some(file_name)) => Some(file_name.to_string()),
_ => None,
}
}?;

let parent_dir = path.parent()?;

Some(parent_dir.join(file_info).to_string_lossy().into_owned())
}

pub fn run(argv: &[&str]) -> CliResult<()> {
Expand Down

0 comments on commit 117f08f

Please sign in to comment.