From 1223e7a6dd537cc34067fdea59738cf006222e9a Mon Sep 17 00:00:00 2001 From: Shekhar Date: Thu, 25 Dec 2025 16:24:34 +0530 Subject: [PATCH 1/2] uucore: fix clippy warnings in build.rs and locale embedding --- src/uucore/build.rs | 47 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/src/uucore/build.rs b/src/uucore/build.rs index f79b3922b7b..60bed5d381d 100644 --- a/src/uucore/build.rs +++ b/src/uucore/build.rs @@ -58,6 +58,11 @@ pub fn main() -> Result<(), Box> { } /// Get the project root directory +/// +/// # Errors +/// +/// Returns an error if the `CARGO_MANIFEST_DIR` environment variable is not set +/// or if the current directory structure does not allow determining the project root. fn project_root() -> Result> { let manifest_dir = env::var("CARGO_MANIFEST_DIR")?; let uucore_path = std::path::Path::new(&manifest_dir); @@ -120,6 +125,11 @@ fn detect_target_utility() -> Option { } /// Embed locale for a single specific utility +/// +/// # Errors +/// +/// Returns an error if the locales for `util_name` or `uucore` cannot be found +/// or if writing to the `embedded_file` fails. fn embed_single_utility_locale( embedded_file: &mut std::fs::File, project_root: &Path, @@ -142,7 +152,12 @@ fn embed_single_utility_locale( Ok(()) } -/// Embed locale files for all utilities (multicall binary) +/// Embed locale files for all utilities (multicall binary). +/// +/// # Errors +/// +/// Returns an error if the `src/uu` directory cannot be read, if any utility +/// locales cannot be embedded, or if flushing the `embedded_file` fails. fn embed_all_utility_locales( embedded_file: &mut std::fs::File, project_root: &Path, @@ -188,6 +203,12 @@ fn embed_all_utility_locales( Ok(()) } +/// Embed static utility locales for crates.io builds. +/// +/// # Errors +/// +/// Returns an error if the directory containing the crate cannot be read or +/// if writing to the `embedded_file` fails. fn embed_static_utility_locales( embedded_file: &mut std::fs::File, locales_to_embed: &(String, Option), @@ -213,7 +234,7 @@ fn embed_static_utility_locales( let mut entries: Vec<_> = std::fs::read_dir(registry_dir)? .filter_map(Result::ok) .collect(); - entries.sort_by_key(|e| e.file_name()); + entries.sort_by_key(std::fs::DirEntry::file_name); for entry in entries { let file_name = entry.file_name(); @@ -256,6 +277,11 @@ fn get_locales_to_embed() -> (String, Option) { } /// Helper function to iterate over the locales to embed. +/// +/// # Errors +/// +/// Returns an error if the provided closure `f` returns an error when called +/// on either the primary or system locale. fn for_each_locale( locales: &(String, Option), mut f: F, @@ -271,6 +297,11 @@ where } /// Helper function to embed a single locale file. +/// +/// # Errors +/// +/// Returns an error if the file at `locale_path` cannot be read or if +/// writing to `embedded_file` fails. fn embed_locale_file( embedded_file: &mut std::fs::File, locale_path: &Path, @@ -286,9 +317,11 @@ fn embed_locale_file( embedded_file, " // Locale for {component} ({locale})" )?; + // Determine if we need a hash. If content contains ", we need r#""# + let delimiter = if content.contains('"') { "#" } else { "" }; writeln!( embedded_file, - " \"{locale_key}\" => Some(r###\"{content}\"###)," + " \"{locale_key}\" => Some(r{delimiter}\"{content}\"{delimiter})," )?; // Tell Cargo to rerun if this file changes @@ -298,7 +331,13 @@ fn embed_locale_file( } /// Higher-level helper to embed locale files for a component with a path pattern. -/// This eliminates the repetitive for_each_locale + embed_locale_file pattern. +/// +/// This eliminates the repetitive `for_each_locale` + `embed_locale_file` pattern. +/// +/// # Errors +/// +/// Returns an error if `for_each_locale` fails, which typically happens if +/// reading a locale file or writing to the `embedded_file` fails. fn embed_component_locales( embedded_file: &mut std::fs::File, locales: &(String, Option), From 34aaa39e78ef1f56d0fe39bcf439ff35644d9f4c Mon Sep 17 00:00:00 2001 From: Shekhar Date: Thu, 25 Dec 2025 16:57:37 +0530 Subject: [PATCH 2/2] style: fix formatting with cargo fmt --- src/uucore/build.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/uucore/build.rs b/src/uucore/build.rs index 60bed5d381d..935394cd4cc 100644 --- a/src/uucore/build.rs +++ b/src/uucore/build.rs @@ -156,7 +156,7 @@ fn embed_single_utility_locale( /// /// # Errors /// -/// Returns an error if the `src/uu` directory cannot be read, if any utility +/// Returns an error if the `src/uu` directory cannot be read, if any utility /// locales cannot be embedded, or if flushing the `embedded_file` fails. fn embed_all_utility_locales( embedded_file: &mut std::fs::File, @@ -207,7 +207,7 @@ fn embed_all_utility_locales( /// /// # Errors /// -/// Returns an error if the directory containing the crate cannot be read or +/// Returns an error if the directory containing the crate cannot be read or /// if writing to the `embedded_file` fails. fn embed_static_utility_locales( embedded_file: &mut std::fs::File, @@ -300,7 +300,7 @@ where /// /// # Errors /// -/// Returns an error if the file at `locale_path` cannot be read or if +/// Returns an error if the file at `locale_path` cannot be read or if /// writing to `embedded_file` fails. fn embed_locale_file( embedded_file: &mut std::fs::File, @@ -336,7 +336,7 @@ fn embed_locale_file( /// /// # Errors /// -/// Returns an error if `for_each_locale` fails, which typically happens if +/// Returns an error if `for_each_locale` fails, which typically happens if /// reading a locale file or writing to the `embedded_file` fails. fn embed_component_locales( embedded_file: &mut std::fs::File,