Skip to content

Commit d8e8803

Browse files
authored
uucore: fix clippy::pedantic warnings in build.rs and generated locale code (#9837)
1 parent a4701f4 commit d8e8803

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

src/uucore/build.rs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
5858
}
5959

6060
/// Get the project root directory
61+
///
62+
/// # Errors
63+
///
64+
/// Returns an error if the `CARGO_MANIFEST_DIR` environment variable is not set
65+
/// or if the current directory structure does not allow determining the project root.
6166
fn project_root() -> Result<std::path::PathBuf, Box<dyn std::error::Error>> {
6267
let manifest_dir = env::var("CARGO_MANIFEST_DIR")?;
6368
let uucore_path = std::path::Path::new(&manifest_dir);
@@ -120,6 +125,11 @@ fn detect_target_utility() -> Option<String> {
120125
}
121126

122127
/// Embed locale for a single specific utility
128+
///
129+
/// # Errors
130+
///
131+
/// Returns an error if the locales for `util_name` or `uucore` cannot be found
132+
/// or if writing to the `embedded_file` fails.
123133
fn embed_single_utility_locale(
124134
embedded_file: &mut std::fs::File,
125135
project_root: &Path,
@@ -142,7 +152,12 @@ fn embed_single_utility_locale(
142152
Ok(())
143153
}
144154

145-
/// Embed locale files for all utilities (multicall binary)
155+
/// Embed locale files for all utilities (multicall binary).
156+
///
157+
/// # Errors
158+
///
159+
/// Returns an error if the `src/uu` directory cannot be read, if any utility
160+
/// locales cannot be embedded, or if flushing the `embedded_file` fails.
146161
fn embed_all_utility_locales(
147162
embedded_file: &mut std::fs::File,
148163
project_root: &Path,
@@ -188,6 +203,12 @@ fn embed_all_utility_locales(
188203
Ok(())
189204
}
190205

206+
/// Embed static utility locales for crates.io builds.
207+
///
208+
/// # Errors
209+
///
210+
/// Returns an error if the directory containing the crate cannot be read or
211+
/// if writing to the `embedded_file` fails.
191212
fn embed_static_utility_locales(
192213
embedded_file: &mut std::fs::File,
193214
locales_to_embed: &(String, Option<String>),
@@ -213,7 +234,7 @@ fn embed_static_utility_locales(
213234
let mut entries: Vec<_> = std::fs::read_dir(registry_dir)?
214235
.filter_map(Result::ok)
215236
.collect();
216-
entries.sort_by_key(|e| e.file_name());
237+
entries.sort_by_key(std::fs::DirEntry::file_name);
217238

218239
for entry in entries {
219240
let file_name = entry.file_name();
@@ -256,6 +277,11 @@ fn get_locales_to_embed() -> (String, Option<String>) {
256277
}
257278

258279
/// Helper function to iterate over the locales to embed.
280+
///
281+
/// # Errors
282+
///
283+
/// Returns an error if the provided closure `f` returns an error when called
284+
/// on either the primary or system locale.
259285
fn for_each_locale<F>(
260286
locales: &(String, Option<String>),
261287
mut f: F,
@@ -271,6 +297,11 @@ where
271297
}
272298

273299
/// Helper function to embed a single locale file.
300+
///
301+
/// # Errors
302+
///
303+
/// Returns an error if the file at `locale_path` cannot be read or if
304+
/// writing to `embedded_file` fails.
274305
fn embed_locale_file(
275306
embedded_file: &mut std::fs::File,
276307
locale_path: &Path,
@@ -286,9 +317,11 @@ fn embed_locale_file(
286317
embedded_file,
287318
" // Locale for {component} ({locale})"
288319
)?;
320+
// Determine if we need a hash. If content contains ", we need r#""#
321+
let delimiter = if content.contains('"') { "#" } else { "" };
289322
writeln!(
290323
embedded_file,
291-
" \"{locale_key}\" => Some(r###\"{content}\"###),"
324+
" \"{locale_key}\" => Some(r{delimiter}\"{content}\"{delimiter}),"
292325
)?;
293326

294327
// Tell Cargo to rerun if this file changes
@@ -298,7 +331,13 @@ fn embed_locale_file(
298331
}
299332

300333
/// Higher-level helper to embed locale files for a component with a path pattern.
301-
/// This eliminates the repetitive for_each_locale + embed_locale_file pattern.
334+
///
335+
/// This eliminates the repetitive `for_each_locale` + `embed_locale_file` pattern.
336+
///
337+
/// # Errors
338+
///
339+
/// Returns an error if `for_each_locale` fails, which typically happens if
340+
/// reading a locale file or writing to the `embedded_file` fails.
302341
fn embed_component_locales<F>(
303342
embedded_file: &mut std::fs::File,
304343
locales: &(String, Option<String>),

0 commit comments

Comments
 (0)