Skip to content

Commit 37fdebe

Browse files
committed
Fix Clippy warnings after updating the local Rust toolchain
Refactored several modules to use Rust's let-chains for more concise and readable pattern matching, replacing nested if-let and match statements. This change affects format detection, merge logic, validation, test cases, and language inference, improving code clarity and reducing nesting.
1 parent 3814f31 commit 37fdebe

6 files changed

Lines changed: 42 additions & 51 deletions

File tree

langcodec-cli/src/formats.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,16 @@ pub fn detect_custom_format(file_path: &str, file_content: &str) -> Option<Custo
126126
// Try to parse as JSON array of Resource objects
127127
if serde_json::from_str::<Vec<serde_json::Value>>(file_content).is_ok() {
128128
// Check if it looks like an array of Resource objects
129-
if let Ok(array) = serde_json::from_str::<Vec<serde_json::Value>>(file_content) {
130-
if !array.is_empty() {
131-
// Check if the first element has the expected Resource structure
132-
if let Some(first) = array.first() {
133-
if let Some(obj) = first.as_object() {
134-
if obj.contains_key("metadata") && obj.contains_key("entries") {
135-
return Some(CustomFormat::LangcodecResourceArray);
136-
}
137-
}
138-
}
129+
if let Ok(array) = serde_json::from_str::<Vec<serde_json::Value>>(file_content)
130+
&& !array.is_empty()
131+
{
132+
// Check if the first element has the expected Resource structure
133+
if let Some(first) = array.first()
134+
&& let Some(obj) = first.as_object()
135+
&& obj.contains_key("metadata")
136+
&& obj.contains_key("entries")
137+
{
138+
return Some(CustomFormat::LangcodecResourceArray);
139139
}
140140
}
141141
}
@@ -147,10 +147,9 @@ pub fn detect_custom_format(file_path: &str, file_content: &str) -> Option<Custo
147147
if let Ok(obj) = serde_json::from_str::<
148148
std::collections::HashMap<String, serde_json::Value>,
149149
>(file_content)
150+
&& !obj.is_empty()
150151
{
151-
if !obj.is_empty() {
152-
return Some(CustomFormat::JSONLanguageMap);
153-
}
152+
return Some(CustomFormat::JSONLanguageMap);
154153
}
155154
// Check if it's an array (JSONArrayLanguageMap)
156155
if serde_json::from_str::<Vec<serde_json::Value>>(file_content).is_ok() {

langcodec-cli/src/merge.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ pub fn run_merge_command(
3737
}
3838

3939
// If standard format fails, try custom format for JSON/YAML files
40-
if input.ends_with(".json") || input.ends_with(".yaml") || input.ends_with(".yml") {
41-
if let Ok(()) = try_custom_format_merge(input, lang.clone(), &mut codec) {
42-
continue;
43-
}
40+
if (input.ends_with(".json") || input.ends_with(".yaml") || input.ends_with(".yml"))
41+
&& let Ok(()) = try_custom_format_merge(input, lang.clone(), &mut codec)
42+
{
43+
continue;
4444
}
4545

4646
// If both fail, show error
@@ -111,12 +111,12 @@ pub fn run_merge_command(
111111
None => {
112112
if codec.resources.len() == 1 {
113113
println!("Writing single resource to output file");
114-
if let Some(resource) = codec.resources.first() {
115-
if let Err(e) = Codec::write_resource_to_file(resource, &output) {
116-
println!("❌ Error writing output file");
117-
eprintln!("Error writing to {}: {}", output, e);
118-
std::process::exit(1);
119-
}
114+
if let Some(resource) = codec.resources.first()
115+
&& let Err(e) = Codec::write_resource_to_file(resource, &output)
116+
{
117+
println!("Error writing output file");
118+
eprintln!("Error writing to {}: {}", output, e);
119+
std::process::exit(1);
120120
}
121121
} else {
122122
println!("❌ Error writing output file");

langcodec-cli/src/validation.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ pub fn validate_file_path(path: &str) -> Result<(), String> {
7777
pub fn validate_output_path(path: &str) -> Result<(), String> {
7878
let path_obj = Path::new(path);
7979

80-
if let Some(parent) = path_obj.parent() {
81-
if !parent.exists() {
82-
// Try to create the directory
83-
if let Err(e) = std::fs::create_dir_all(parent) {
84-
return Err(format!("Cannot create output directory: {}", e));
85-
}
80+
if let Some(parent) = path_obj.parent()
81+
&& !parent.exists()
82+
{
83+
// Try to create the directory
84+
if let Err(e) = std::fs::create_dir_all(parent) {
85+
return Err(format!("Cannot create output directory: {}", e));
8686
}
8787
}
8888

langcodec-cli/tests/format_edge_cases.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,10 @@ fn test_json_with_null_values() {
6969
CustomFormat::JSONLanguageMap,
7070
);
7171

72-
// Should handle null values gracefully
73-
if result.is_ok() {
74-
let resources = result.unwrap();
75-
let fr_resource = resources.iter().find(|r| r.metadata.language == "fr");
76-
assert!(fr_resource.is_none() || fr_resource.unwrap().entries.is_empty());
77-
}
72+
// Should fail because null values are not supported
73+
assert!(result.is_err());
74+
let error = result.unwrap_err();
75+
assert!(error.contains("Error parsing JSON") || error.contains("Invalid format"));
7876
}
7977

8078
#[test]
@@ -96,8 +94,7 @@ fn test_json_with_boolean_values() {
9694
);
9795

9896
// Should convert booleans to strings
99-
if result.is_ok() {
100-
let resources = result.unwrap();
97+
if let Ok(resources) = result {
10198
let en_resource = resources
10299
.iter()
103100
.find(|r| r.metadata.language == "en")
@@ -128,8 +125,7 @@ fn test_json_with_numbers() {
128125
);
129126

130127
// Should convert numbers to strings
131-
if result.is_ok() {
132-
let resources = result.unwrap();
128+
if let Ok(resources) = result {
133129
let en_resource = resources
134130
.iter()
135131
.find(|r| r.metadata.language == "en")
@@ -206,8 +202,7 @@ fn test_json_array_with_missing_keys() {
206202
);
207203

208204
// Should handle missing keys gracefully
209-
if result.is_ok() {
210-
let resources = result.unwrap();
205+
if let Ok(resources) = result {
211206
assert_eq!(resources.len(), 2); // en and fr
212207
}
213208
}
@@ -238,8 +233,7 @@ fn test_json_array_with_duplicate_keys() {
238233
);
239234

240235
// Should handle duplicate keys (last one wins)
241-
if result.is_ok() {
242-
let resources = result.unwrap();
236+
if let Ok(resources) = result {
243237
let en_resource = resources
244238
.iter()
245239
.find(|r| r.metadata.language == "en")

langcodec/src/codec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ impl Codec {
6868
}
6969

7070
/// Returns an iterator over all resources.
71-
pub fn iter(&self) -> std::slice::Iter<Resource> {
71+
pub fn iter(&self) -> std::slice::Iter<'_, Resource> {
7272
self.resources.iter()
7373
}
7474

7575
/// Returns a mutable iterator over all resources.
76-
pub fn iter_mut(&mut self) -> std::slice::IterMut<Resource> {
76+
pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, Resource> {
7777
self.resources.iter_mut()
7878
}
7979

langcodec/src/converter.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,21 +372,19 @@ pub fn infer_language_from_path<P: AsRef<Path>>(
372372
if let Some(captures) = regex::Regex::new(r"([a-z]{2}(?:-[A-Z]{2})?)\.lproj")
373373
.map_err(|_| Error::InvalidResource("Invalid regex pattern".to_string()))?
374374
.captures(&path_str)
375+
&& let Some(lang) = captures.get(1)
375376
{
376-
if let Some(lang) = captures.get(1) {
377-
return Ok(Some(lang.as_str().to_string()));
378-
}
377+
return Ok(Some(lang.as_str().to_string()));
379378
}
380379
}
381380
FormatType::AndroidStrings(_) => {
382381
// Android strings.xml files: values-es/strings.xml
383382
if let Some(captures) = regex::Regex::new(r"values-([a-z]{2}(?:-[A-Z]{2})?)")
384383
.map_err(|_| Error::InvalidResource("Invalid regex pattern".to_string()))?
385384
.captures(&path_str)
385+
&& let Some(lang) = captures.get(1)
386386
{
387-
if let Some(lang) = captures.get(1) {
388-
return Ok(Some(lang.as_str().to_string()));
389-
}
387+
return Ok(Some(lang.as_str().to_string()));
390388
}
391389
}
392390
_ => {}

0 commit comments

Comments
 (0)