Skip to content

Commit

Permalink
Take comments into account
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvestre committed Dec 9, 2024
1 parent 070bbe2 commit 465800b
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/uu/stat/src/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ pub enum OutputType {
Unknown,
}

#[derive(Default)]
enum QuotingStyle {
Locale,
Shell,
Default,
#[default]
ShellEscapeAlways,
Quote,
}

Expand All @@ -111,6 +113,7 @@ impl std::str::FromStr for QuotingStyle {
match s {
"locale" => Ok(QuotingStyle::Locale),
"shell" => Ok(QuotingStyle::Shell),
"shell-escape-always" => Ok(QuotingStyle::ShellEscapeAlways),
// The others aren't exposed to the user
_ => Err(format!("Invalid quoting style: {}", s)),
}
Expand Down Expand Up @@ -324,7 +327,7 @@ fn quote_file_name(file_name: &str, quoting_style: &QuotingStyle) -> String {
let escaped = file_name.replace('\'', r"\'");
format!("'{}'", escaped)
}
QuotingStyle::Default => format!("\"{}\"", file_name),
QuotingStyle::ShellEscapeAlways => format!("\"{}\"", file_name),
QuotingStyle::Quote => file_name.to_string(),
}
}
Expand All @@ -338,10 +341,12 @@ fn get_quoted_file_name(
let quoting_style = env::var("QUOTING_STYLE")
.ok()
.and_then(|style| style.parse().ok())
.unwrap_or(QuotingStyle::Default);
.unwrap_or_default();

if file_type.is_symlink() {
let quoted_display_name = quote_file_name(display_name, &quoting_style);
let display_name: Cow<'_, str> = file.to_string_lossy();

let quoted_display_name = quote_file_name(&display_name, &quoting_style);
match fs::read_link(file) {
Ok(dst) => {
let quoted_dst = quote_file_name(&dst.to_string_lossy(), &quoting_style);
Expand All @@ -358,11 +363,11 @@ fn get_quoted_file_name(
} else {
QuotingStyle::Quote
};
Ok(quote_file_name(display_name, &style))
Ok(quote_file_name(&display_name, &style))

Check failure on line 366 in src/uu/stat/src/stat.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-24.04, unix)

ERROR: `cargo clippy`: this expression creates a reference which is immediately dereferenced by the compiler (file:'src/uu/stat/src/stat.rs', line:366)
}
}

fn process_token_fs(t: &Token, meta: StatFs, display_name: &str) {
fn process_token_filesystem(t: &Token, meta: StatFs, display_name: &str) {
match *t {
Token::Byte(byte) => write_raw_byte(byte),
Token::Char(c) => print!("{c}"),
Expand Down Expand Up @@ -856,7 +861,7 @@ impl Stater {
// quoted file name with dereference if symbolic link
'N' => {
let file_name =
get_quoted_file_name(display_name, file, file_type, from_user)?;
get_quoted_file_name(&display_name, file, file_type, from_user)?;

Check failure on line 864 in src/uu/stat/src/stat.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-24.04, unix)

ERROR: `cargo clippy`: this expression creates a reference which is immediately dereferenced by the compiler (file:'src/uu/stat/src/stat.rs', line:864)
OutputType::Str(file_name)
}
// optimal I/O transfer size hint
Expand Down Expand Up @@ -943,7 +948,7 @@ impl Stater {

// Usage
for t in tokens {
process_token_fs(t, meta, &display_name);
process_token_filesystem(t, meta, &display_name);
}
}
Err(e) => {
Expand Down

0 comments on commit 465800b

Please sign in to comment.