Skip to content

Commit bb32e7f

Browse files
Alonely0cakebakerxtqqczze
committed
fix(ls): apply reviewed changes
Renames and documents PathDataDisplayName::Moo to _::Custom; also makes code more idiomatic and restores an accidentally-removed warning. Co-authored-by: Daniel Hofstetter <daniel.hofstetter@42dh.com> Co-authored-by: xtqqczze <45661989+xtqqczze@users.noreply.github.com>
1 parent edfe26c commit bb32e7f

2 files changed

Lines changed: 23 additions & 19 deletions

File tree

src/uu/ls/src/config.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -345,11 +345,6 @@ fn extract_time(options: &clap::ArgMatches) -> MetadataTimeField {
345345
fn is_color_compatible_term() -> bool {
346346
let term = std::env::var_os("TERM");
347347
let colorterm = std::env::var_os("COLORTERM");
348-
let is_term_set = term.is_some();
349-
let is_colorterm_set = colorterm.is_some();
350-
351-
let term = term.unwrap_or_default();
352-
let colorterm = colorterm.unwrap_or_default();
353348

354349
// Search function in the TERM struct to manage the wildcards
355350
let term_matches = |term: &OsStr| -> bool {
@@ -362,14 +357,11 @@ fn is_color_compatible_term() -> bool {
362357
})
363358
};
364359

365-
if is_term_set && term.is_empty() && is_colorterm_set && colorterm.is_empty() {
366-
return false;
367-
}
368-
369-
if !term.is_empty() && !term_matches(&term) {
370-
return false;
360+
match (term, colorterm) {
361+
(Some(t), Some(c)) if t.is_empty() && c.is_empty() => false,
362+
(Some(t), _) if !t.is_empty() => term_matches(&t),
363+
_ => true,
371364
}
372-
true
373365
}
374366

375367
/// Extracts the color option to use based on the options provided.
@@ -1030,8 +1022,8 @@ fn parse_time_style(options: &clap::ArgMatches) -> Result<(String, Option<String
10301022

10311023
if let Some(field) = options
10321024
.get_one::<String>(options::TIME_STYLE)
1033-
.map(|s| Cow::Borrowed(s.as_str()))
1034-
.or_else(|| std::env::var("TIME_STYLE").ok().map(Cow::Owned))
1025+
.map(Cow::from)
1026+
.or_else(|| std::env::var("TIME_STYLE").ok().map(Cow::from))
10351027
{
10361028
//If both FULL_TIME and TIME_STYLE are present
10371029
//The one added last is dominant

src/uu/ls/src/ls.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -784,10 +784,12 @@ pub fn uu_app() -> Command {
784784
.after_help(translate!("ls-after-help"))
785785
}
786786

787+
/// Represents the possible values of [`PathData::display_name`]. The reason this is a
788+
/// separate enum is to avoid a self-referential struct, as it is moved in hot loops.
787789
#[derive(Debug)]
788790
enum PathDataDisplayName<'a> {
789791
SelfReferential,
790-
Moo(Cow<'a, OsStr>),
792+
Custom(Cow<'a, OsStr>),
791793
}
792794

793795
/// Represents a Path along with it's associated data.
@@ -821,11 +823,11 @@ impl<'a> PathData<'a> {
821823
// We cannot use `Path::ends_with` or `Path::Components`, because they remove occurrences of '.'
822824
// For '..', the filename is None
823825
let display_name = if let Some(name) = file_name {
824-
PathDataDisplayName::Moo(name)
826+
PathDataDisplayName::Custom(name)
825827
} else if command_line {
826828
PathDataDisplayName::SelfReferential
827829
} else {
828-
PathDataDisplayName::Moo(
830+
PathDataDisplayName::Custom(
829831
dir_entry
830832
.as_ref()
831833
.map(DirEntry::file_name)
@@ -952,7 +954,7 @@ impl<'a> PathData<'a> {
952954
fn display_name(&self) -> &OsStr {
953955
match self.display_name {
954956
PathDataDisplayName::SelfReferential => self.p_buf.as_os_str(),
955-
PathDataDisplayName::Moo(ref cow) => cow,
957+
PathDataDisplayName::Custom(ref cow) => cow,
956958
}
957959
}
958960
}
@@ -1452,7 +1454,17 @@ fn get_security_context<'a>(
14521454

14531455
let res: String = match str::from_utf8(context) {
14541456
Ok(s) => s.to_string(),
1455-
Err(_) => String::from_utf8_lossy(context).into_owned(),
1457+
Err(e) => {
1458+
show_warning!(
1459+
"{}",
1460+
translate!(
1461+
"ls-warning-getting-security-context",
1462+
"path" => path.quote(),
1463+
"error" => e
1464+
)
1465+
);
1466+
String::from_utf8_lossy(context).into_owned()
1467+
}
14561468
};
14571469

14581470
return Cow::Owned(res);

0 commit comments

Comments
 (0)