Skip to content

Commit d0a5a4c

Browse files
Boshenclaude
andauthored
chore: clean up code (#67)
- Use char literals instead of string literals for single characters - Replace match with if/else for simple boolean checks - Remove unnecessary string cloning - Simplify conditional logic with is_some_and() - Use Vec::new() instead of [].to_vec() - Use String::new() instead of String::from("") - Use unwrap_or_default() instead of unwrap_or_else - Use string interpolation shorthand in format macros - Simplify Default trait usage 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <[email protected]>
1 parent 56742bc commit d0a5a4c

File tree

4 files changed

+20
-25
lines changed

4 files changed

+20
-25
lines changed

src/lib.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ fn parse_global_package_name(specifier: &str) -> Option<(String, Option<String>)
7171
}
7272

7373
pub fn parse_bare_identifier(specifier: &str) -> Result<(String, Option<String>), Error> {
74-
let name = match specifier.starts_with("@") {
75-
true => parse_scoped_package_name(specifier),
76-
false => parse_global_package_name(specifier),
74+
let name = if specifier.starts_with('@') {
75+
parse_scoped_package_name(specifier)
76+
} else {
77+
parse_global_package_name(specifier)
7778
};
7879

7980
name.ok_or_else(|| {
@@ -139,7 +140,7 @@ pub fn load_pnp_manifest(p: &Path) -> Result<Manifest, Error> {
139140
}
140141
}
141142

142-
let mut manifest: Manifest = serde_json::from_str(&json_string.to_owned())
143+
let mut manifest: Manifest = serde_json::from_str(&json_string)
143144
.map_err(|err| Error::FailedManifestHydration(Box::new(FailedManifestHydration {
144145
message: format!("We failed to parse the PnP data payload as proper JSON; Did you manually edit the file?\n\nOriginal error: {err}"),
145146
manifest_path: p.to_path_buf(),
@@ -228,18 +229,17 @@ pub fn get_package<'a>(
228229
}
229230

230231
pub fn is_excluded_from_fallback(manifest: &Manifest, locator: &PackageLocator) -> bool {
231-
if let Some(references) = manifest.fallback_exclusion_list.get(&locator.name) {
232-
references.contains(&locator.reference)
233-
} else {
234-
false
235-
}
232+
manifest
233+
.fallback_exclusion_list
234+
.get(&locator.name)
235+
.is_some_and(|references| references.contains(&locator.reference))
236236
}
237237

238238
pub fn find_broken_peer_dependencies(
239239
_dependency: &str,
240240
_initial_package: &PackageLocator,
241241
) -> Vec<PackageLocator> {
242-
[].to_vec()
242+
Vec::new()
243243
}
244244

245245
pub fn resolve_to_unqualified_via_manifest(
@@ -279,9 +279,9 @@ pub fn resolve_to_unqualified_via_manifest(
279279
"Your application tried to access {dependency_name}. While this module is usually interpreted as a Node builtin, your resolver is running inside a non-Node resolution context where such builtins are ignored. Since {dependency_name} isn't otherwise declared in your dependencies, this makes the require call ambiguous and unsound.\n\nRequired package: {dependency_name}{via}\nRequired by: ${issuer_path}",
280280
dependency_name = &ident,
281281
via = if ident != specifier {
282-
format!(" (via \"{}\")", &specifier)
282+
format!(" (via \"{specifier}\")")
283283
} else {
284-
String::from("")
284+
String::new()
285285
},
286286
issuer_path = parent.to_string_lossy(),
287287
)
@@ -291,9 +291,9 @@ pub fn resolve_to_unqualified_via_manifest(
291291
issuer_locator_name = &parent_locator.name,
292292
dependency_name = &ident,
293293
via = if ident != specifier {
294-
format!(" (via \"{}\")", &specifier)
294+
format!(" (via \"{specifier}\")")
295295
} else {
296-
String::from("")
296+
String::new()
297297
},
298298
issuer_path = parent.to_string_lossy(),
299299
)
@@ -394,7 +394,7 @@ pub fn resolve_to_unqualified_via_manifest(
394394
dependency_name: ident,
395395
issuer_locator: parent_locator.clone(),
396396
issuer_path: parent.to_path_buf(),
397-
broken_ancestors: [].to_vec(),
397+
broken_ancestors: Vec::new(),
398398
})))
399399
}
400400
} else {

src/main.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ fn main() {
1414
println!("specifier = {specifier}");
1515
println!("parent = {parent:?}");
1616

17-
let resolution = pnp::resolve_to_unqualified(
18-
&specifier,
19-
&parent,
20-
&ResolutionConfig { ..Default::default() },
21-
);
17+
let resolution = pnp::resolve_to_unqualified(&specifier, &parent, &ResolutionConfig::default());
2218

2319
match resolution {
2420
Ok(res) => match res {

src/manifest.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,9 @@ where
119119

120120
let mut map = FxHashMap::default();
121121
for item in Vec::<Item>::deserialize(deserializer)? {
122-
let key = item.0.unwrap_or_else(|| "".to_string());
123-
let value = FxHashMap::from_iter(
124-
item.1.into_iter().map(|(k, v)| (k.unwrap_or_else(|| "".to_string()), v)),
125-
);
122+
let key = item.0.unwrap_or_default();
123+
let value =
124+
FxHashMap::from_iter(item.1.into_iter().map(|(k, v)| (k.unwrap_or_default(), v)));
126125
map.insert(key, value);
127126
}
128127
Ok(map)

src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ fn to_portable_path<'a>(str: &'a str) -> Cow<'a, str> {
8888
pub fn normalize_path<P: AsRef<str>>(original: P) -> String {
8989
let original_str = to_portable_path(original.as_ref());
9090

91-
let check_str_root = original_str.strip_prefix("/");
91+
let check_str_root = original_str.strip_prefix('/');
9292
let str_minus_root = check_str_root.unwrap_or(original_str.as_ref());
9393

9494
let components = str_minus_root.split(&['/', '\\'][..]);

0 commit comments

Comments
 (0)