Skip to content

Commit 1f18dad

Browse files
committed
clippy+fmt
1 parent 3f34a04 commit 1f18dad

6 files changed

Lines changed: 55 additions & 52 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,5 @@ npm/
135135
.cursor
136136

137137
profile.json.gz
138+
139+
fixtures/*afm*.lock

crates/berry-bench-bin/src/main.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,10 @@ impl FixtureTarget {
8080

8181
fn from_path(path: impl Into<PathBuf>) -> Self {
8282
let path = path.into();
83-
let label = path
84-
.file_name()
85-
.and_then(|name| name.to_str())
86-
.map(std::string::ToString::to_string)
87-
.unwrap_or_else(|| path.display().to_string());
83+
let label = path.file_name().and_then(|name| name.to_str()).map_or_else(
84+
|| path.display().to_string(),
85+
std::string::ToString::to_string,
86+
);
8887
Self {
8988
label,
9089
source: FixtureSource::ArbitraryPath(path),
@@ -185,10 +184,8 @@ fn benchmark_fixture(
185184
let fixture_str = fixture.as_str();
186185

187186
println!("Benchmarking {} ({file_size} bytes)...", target.label);
188-
if verbose {
189-
if let Some(path) = target.source_path() {
190-
println!(" Source path: {}", path.display());
191-
}
187+
if verbose && let Some(path) = target.source_path() {
188+
println!(" Source path: {}", path.display());
192189
}
193190

194191
// Warmup runs

crates/berry-compare-bin/src/main.rs

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use serde::Deserialize;
1111
use std::collections::HashMap;
1212
use std::fs;
1313
use std::io::Write;
14-
use std::path::PathBuf;
14+
use std::path::{Path, PathBuf};
1515
use std::process::{Command, Stdio};
1616
use std::time::Instant;
1717

@@ -65,17 +65,17 @@ struct JsParserOutput {
6565
entries: Vec<CompareEntry>,
6666
}
6767

68-
/// Convert a berry Entry to CompareEntry
68+
/// Convert a berry Entry to `CompareEntry`
6969
fn entry_to_compare(entry: &Entry<'_>) -> CompareEntry {
7070
let mut descriptors: Vec<String> = entry
7171
.descriptors
7272
.iter()
7373
.map(|d| {
7474
let ident = d.ident();
75-
let name = match ident.scope() {
76-
Some(scope) => format!("{}/{}", scope, ident.name()),
77-
None => ident.name().to_string(),
78-
};
75+
let name = ident.scope().map_or_else(
76+
|| ident.name().to_string(),
77+
|scope| format!("{}/{}", scope, ident.name()),
78+
);
7979
format!("{}@{}", name, d.range())
8080
})
8181
.collect();
@@ -86,10 +86,10 @@ fn entry_to_compare(entry: &Entry<'_>) -> CompareEntry {
8686
.dependencies
8787
.iter()
8888
.map(|(ident, desc)| {
89-
let name = match ident.scope() {
90-
Some(scope) => format!("{}/{}", scope, ident.name()),
91-
None => ident.name().to_string(),
92-
};
89+
let name = ident.scope().map_or_else(
90+
|| ident.name().to_string(),
91+
|scope| format!("{}/{}", scope, ident.name()),
92+
);
9393
(name, desc.range().to_string())
9494
})
9595
.collect();
@@ -99,10 +99,10 @@ fn entry_to_compare(entry: &Entry<'_>) -> CompareEntry {
9999
.peer_dependencies
100100
.iter()
101101
.map(|(ident, desc)| {
102-
let name = match ident.scope() {
103-
Some(scope) => format!("{}/{}", scope, ident.name()),
104-
None => ident.name().to_string(),
105-
};
102+
let name = ident.scope().map_or_else(
103+
|| ident.name().to_string(),
104+
|scope| format!("{}/{}", scope, ident.name()),
105+
);
106106
(name, desc.range().to_string())
107107
})
108108
.collect();
@@ -174,10 +174,10 @@ fn entry_to_compare(entry: &Entry<'_>) -> CompareEntry {
174174
}
175175

176176
/// Generate a Node.js script to parse the given lockfile
177-
fn generate_js_script(fixture_path: &PathBuf) -> String {
177+
fn generate_js_script(fixture_path: &Path) -> String {
178178
let path_str = fixture_path.display();
179179
format!(
180-
r#"const fs = require('fs');
180+
r"const fs = require('fs');
181181
182182
let parseSyml;
183183
try {{
@@ -266,7 +266,7 @@ for (const [key, value] of Object.entries(parsed)) {{
266266
}}
267267
268268
console.log(JSON.stringify({{ entries }}));
269-
"#
269+
"
270270
)
271271
}
272272

@@ -282,7 +282,7 @@ fn get_npm_global_prefix() -> Option<String> {
282282
}
283283
}
284284

285-
fn run_js_parser(fixture_path: &PathBuf) -> Result<JsParserOutput, String> {
285+
fn run_js_parser(fixture_path: &Path) -> Result<JsParserOutput, String> {
286286
let script = generate_js_script(fixture_path);
287287
let script_path = std::env::temp_dir().join("berry-compare-parser.js");
288288
fs::write(&script_path, &script).map_err(|e| format!("Failed to write temp script: {e}"))?;
@@ -295,7 +295,7 @@ fn run_js_parser(fixture_path: &PathBuf) -> Result<JsParserOutput, String> {
295295

296296
// Set NODE_PATH to include global node_modules
297297
if let Some(prefix) = get_npm_global_prefix() {
298-
let node_path = format!("{}/lib/node_modules", prefix);
298+
let node_path = format!("{prefix}/lib/node_modules");
299299
cmd.env("NODE_PATH", node_path);
300300
}
301301

@@ -331,7 +331,7 @@ fn time_rust_parser(contents: &str, iterations: usize) -> (Vec<Entry<'_>>, f64)
331331
(entries, avg_time)
332332
}
333333

334-
fn time_js_parser(fixture_path: &PathBuf, iterations: usize) -> (JsParserOutput, f64) {
334+
fn time_js_parser(fixture_path: &Path, iterations: usize) -> (JsParserOutput, f64) {
335335
let mut total_time = 0.0;
336336
let mut output = None;
337337

@@ -354,6 +354,7 @@ struct Difference {
354354
js_value: String,
355355
}
356356

357+
#[allow(clippy::too_many_lines)]
357358
fn compare_entries(rust_entries: &[CompareEntry], js_entries: &[CompareEntry]) -> Vec<Difference> {
358359
let mut differences = Vec::new();
359360

@@ -424,15 +425,15 @@ fn compare_entries(rust_entries: &[CompareEntry], js_entries: &[CompareEntry]) -
424425
if rust_range != js_range {
425426
differences.push(Difference {
426427
resolution: (*resolution).to_string(),
427-
field: format!("dependencies[{}]", dep_name),
428+
field: format!("dependencies[{dep_name}]"),
428429
rust_value: rust_range.clone(),
429430
js_value: js_range.clone(),
430431
});
431432
}
432433
} else {
433434
differences.push(Difference {
434435
resolution: (*resolution).to_string(),
435-
field: format!("dependencies[{}]", dep_name),
436+
field: format!("dependencies[{dep_name}]"),
436437
rust_value: rust_range.clone(),
437438
js_value: "(missing)".to_string(),
438439
});
@@ -443,7 +444,7 @@ fn compare_entries(rust_entries: &[CompareEntry], js_entries: &[CompareEntry]) -
443444
if !rust_entry.dependencies.contains_key(dep_name) {
444445
differences.push(Difference {
445446
resolution: (*resolution).to_string(),
446-
field: format!("dependencies[{}]", dep_name),
447+
field: format!("dependencies[{dep_name}]"),
447448
rust_value: "(missing)".to_string(),
448449
js_value: js_range.clone(),
449450
});
@@ -456,15 +457,15 @@ fn compare_entries(rust_entries: &[CompareEntry], js_entries: &[CompareEntry]) -
456457
if rust_range != js_range {
457458
differences.push(Difference {
458459
resolution: (*resolution).to_string(),
459-
field: format!("peerDependencies[{}]", dep_name),
460+
field: format!("peerDependencies[{dep_name}]"),
460461
rust_value: rust_range.clone(),
461462
js_value: js_range.clone(),
462463
});
463464
}
464465
} else {
465466
differences.push(Difference {
466467
resolution: (*resolution).to_string(),
467-
field: format!("peerDependencies[{}]", dep_name),
468+
field: format!("peerDependencies[{dep_name}]"),
468469
rust_value: rust_range.clone(),
469470
js_value: "(missing)".to_string(),
470471
});
@@ -475,7 +476,7 @@ fn compare_entries(rust_entries: &[CompareEntry], js_entries: &[CompareEntry]) -
475476
if !rust_entry.peer_dependencies.contains_key(dep_name) {
476477
differences.push(Difference {
477478
resolution: (*resolution).to_string(),
478-
field: format!("peerDependencies[{}]", dep_name),
479+
field: format!("peerDependencies[{dep_name}]"),
479480
rust_value: "(missing)".to_string(),
480481
js_value: js_range.clone(),
481482
});
@@ -488,15 +489,15 @@ fn compare_entries(rust_entries: &[CompareEntry], js_entries: &[CompareEntry]) -
488489
if rust_path != js_path {
489490
differences.push(Difference {
490491
resolution: (*resolution).to_string(),
491-
field: format!("bin[{}]", bin_name),
492+
field: format!("bin[{bin_name}]"),
492493
rust_value: rust_path.clone(),
493494
js_value: js_path.clone(),
494495
});
495496
}
496497
} else {
497498
differences.push(Difference {
498499
resolution: (*resolution).to_string(),
499-
field: format!("bin[{}]", bin_name),
500+
field: format!("bin[{bin_name}]"),
500501
rust_value: rust_path.clone(),
501502
js_value: "(missing)".to_string(),
502503
});
@@ -507,7 +508,7 @@ fn compare_entries(rust_entries: &[CompareEntry], js_entries: &[CompareEntry]) -
507508
if !rust_entry.bin.contains_key(bin_name) {
508509
differences.push(Difference {
509510
resolution: (*resolution).to_string(),
510-
field: format!("bin[{}]", bin_name),
511+
field: format!("bin[{bin_name}]"),
511512
rust_value: "(missing)".to_string(),
512513
js_value: js_path.clone(),
513514
});
@@ -598,7 +599,7 @@ fn compare_entries(rust_entries: &[CompareEntry], js_entries: &[CompareEntry]) -
598599
}
599600

600601
// Check for entries in JS but not in Rust
601-
for (resolution, _) in &js_by_res {
602+
for resolution in js_by_res.keys() {
602603
if !rust_by_res.contains_key(resolution) {
603604
differences.push(Difference {
604605
resolution: (*resolution).to_string(),
@@ -616,15 +617,19 @@ fn main() {
616617
let args = Args::parse();
617618

618619
let fixture_path = args.fixture.canonicalize().unwrap_or_else(|e| {
619-
eprintln!("Error: Cannot find fixture file {:?}: {}", args.fixture, e);
620+
eprintln!(
621+
"Error: Cannot find fixture file {}: {}",
622+
args.fixture.display(),
623+
e
624+
);
620625
std::process::exit(1);
621626
});
622627

623628
let contents = load_fixture_from_path(&fixture_path);
624629
let file_size = contents.len();
625630

626631
println!("Comparing parsers for: {}", fixture_path.display());
627-
println!("File size: {} bytes", file_size);
632+
println!("File size: {file_size} bytes");
628633
println!("Iterations: {}", args.iterations);
629634
println!();
630635

@@ -645,10 +650,10 @@ fn main() {
645650

646651
println!();
647652
println!("=== Performance ===");
648-
println!("Rust: {:.3} ms avg", rust_time);
649-
println!("JS: {:.3} ms avg", js_time);
653+
println!("Rust: {rust_time:.3} ms avg");
654+
println!("JS: {js_time:.3} ms avg");
650655
let speedup = js_time / rust_time;
651-
println!("Speedup: {:.2}x", speedup);
656+
println!("Speedup: {speedup:.2}x");
652657

653658
println!();
654659
println!("=== Entry Counts ===");

crates/berry-core/src/ident.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,9 @@ impl<'a> Range<'a> {
5959

6060
/// Returns the selector part (e.g., "^1.2.3", "packages/a", or the full raw when no protocol).
6161
pub fn selector(&self) -> &'a str {
62-
match self.protocol_sep_index {
63-
Some(i) => &self.raw[i + 1..],
64-
None => self.raw,
65-
}
62+
self
63+
.protocol_sep_index
64+
.map_or(self.raw, |i| &self.raw[i + 1..])
6665
}
6766
}
6867

crates/berry-core/src/lockfile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub struct Lockfile<'a> {
2727
/// A single lockfile entry is a mapping of one or more descriptors to a single package
2828
#[derive(Debug)]
2929
pub struct Entry<'a> {
30-
/// The descriptors of the entry (using SmallVec for stack allocation in common case)
30+
/// The descriptors of the entry (using `SmallVec` for stack allocation in common case)
3131
pub descriptors: SmallVec<[Descriptor<'a>; 4]>,
3232
/// The package of the entry
3333
pub package: Package<'a>,

crates/berry-core/src/parse.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub fn parse_package_entry(
9595
}
9696

9797
/// Parse a package descriptor line like: "debug@npm:1.0.0":, eslint-config-turbo@latest:, or ? "conditional@npm:1.0.0":
98-
/// Uses SmallVec<[Descriptor; 4]> since most descriptor lines have 1-3 descriptors
98+
/// Uses `SmallVec<[Descriptor; 4]>` since most descriptor lines have 1-3 descriptors
9999
pub fn parse_descriptor_line(input: &str) -> IResult<&str, SmallVec<[Descriptor<'_>; 4]>> {
100100
// Check for optional '? ' prefix for wrapped-line descriptors
101101
let (rest, has_line_wrap_marker) = opt(tag("? ")).parse(input)?;
@@ -171,7 +171,7 @@ fn convert_to_descriptor<'a>((name_part, full_range): (&'a str, &'a str)) -> Des
171171

172172
/// Parse a single descriptor string like "debug@npm:1.0.0", "c@*", or "is-odd@patch:is-odd@npm%3A3.0.1#~/.yarn/patches/is-odd-npm-3.0.1-93c3c3f41b.patch"
173173
/// Returns borrowed strings to avoid allocations during parsing
174-
/// Returns (name_part, full_range) where full_range includes protocol if present (e.g., "npm:^1.0.0")
174+
/// Returns (`name_part`, `full_range`) where `full_range` includes protocol if present (e.g., "npm:^1.0.0")
175175
fn parse_single_descriptor(input: &str) -> IResult<&str, (&str, &str)> {
176176
// Parse package name first
177177
let (after_name, name_part) = parse_package_name(input)?;
@@ -322,7 +322,7 @@ pub fn parse_package_properties(input: &str) -> IResult<&str, Package<'_>> {
322322
.parse(input)?;
323323

324324
// Consume any trailing whitespace and blank lines
325-
let (rest, _) = fold_many0(
325+
let (rest, ()) = fold_many0(
326326
alt((tag("\n"), tag(" "), tag("\t"), tag("\r"))),
327327
|| (),
328328
|(), _| (),

0 commit comments

Comments
 (0)