Skip to content

Commit 7ef2b72

Browse files
committed
Refactor CSV/TSV handling to support multiple resources
Updated CSV and TSV format handling to process all provided resources instead of only the first one. This change improves multi-language and multi-resource support for these formats across conversion and file writing functions.
1 parent 1da78fc commit 7ef2b72

1 file changed

Lines changed: 12 additions & 50 deletions

File tree

langcodec/src/converter.rs

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -89,32 +89,12 @@ pub fn convert_resources_to_format(
8989
.map_err(|e| {
9090
Error::conversion_error(format!("Error writing Xcstrings output: {}", e), None)
9191
}),
92-
FormatType::CSV => {
93-
if let Some(resource) = resources.first() {
94-
CSVFormat::try_from(vec![resource.clone()])
95-
.and_then(|f| f.write_to(Path::new(output_path)))
96-
.map_err(|e| {
97-
Error::conversion_error(format!("Error writing CSV output: {}", e), None)
98-
})
99-
} else {
100-
Err(Error::InvalidResource(
101-
"No resources to convert".to_string(),
102-
))
103-
}
104-
}
105-
FormatType::TSV => {
106-
if let Some(resource) = resources.first() {
107-
TSVFormat::try_from(vec![resource.clone()])
108-
.and_then(|f| f.write_to(Path::new(output_path)))
109-
.map_err(|e| {
110-
Error::conversion_error(format!("Error writing TSV output: {}", e), None)
111-
})
112-
} else {
113-
Err(Error::InvalidResource(
114-
"No resources to convert".to_string(),
115-
))
116-
}
117-
}
92+
FormatType::CSV => CSVFormat::try_from(resources)
93+
.and_then(|f| f.write_to(Path::new(output_path)))
94+
.map_err(|e| Error::conversion_error(format!("Error writing CSV output: {}", e), None)),
95+
FormatType::TSV => TSVFormat::try_from(resources)
96+
.and_then(|f| f.write_to(Path::new(output_path)))
97+
.map_err(|e| Error::conversion_error(format!("Error writing TSV output: {}", e), None)),
11898
}
11999
}
120100

@@ -201,26 +181,8 @@ pub fn convert<P: AsRef<Path>>(
201181
}
202182
}
203183
FormatType::Xcstrings => XcstringsFormat::try_from(resources)?.write_to(&output),
204-
FormatType::CSV => {
205-
let resource = pick_resource(None);
206-
if let Some(res) = resource {
207-
CSVFormat::try_from(vec![res])?.write_to(&output)
208-
} else {
209-
Err(Error::InvalidResource(
210-
"No matching resource for output language.".to_string(),
211-
))
212-
}
213-
}
214-
FormatType::TSV => {
215-
let resource = pick_resource(None);
216-
if let Some(res) = resource {
217-
TSVFormat::try_from(vec![res])?.write_to(&output)
218-
} else {
219-
Err(Error::InvalidResource(
220-
"No matching resource for output language.".to_string(),
221-
))
222-
}
223-
}
184+
FormatType::CSV => CSVFormat::try_from(resources)?.write_to(&output),
185+
FormatType::TSV => TSVFormat::try_from(resources)?.write_to(&output),
224186
}
225187
}
226188

@@ -339,8 +301,8 @@ pub fn infer_format_from_extension<P: AsRef<Path>>(path: P) -> Option<FormatType
339301
pub fn infer_format_from_path<P: AsRef<Path>>(path: P) -> Option<FormatType> {
340302
match infer_format_from_extension(&path) {
341303
Some(format) => match format {
342-
FormatType::Xcstrings => Some(format),
343-
FormatType::CSV | FormatType::TSV => Some(format), // Multi-language formats, no language inference needed
304+
// Multi-language formats, no language inference needed
305+
FormatType::Xcstrings | FormatType::CSV | FormatType::TSV => Some(format),
344306
FormatType::AndroidStrings(_) | FormatType::Strings(_) => {
345307
let lang = infer_language_from_path(&path, &format).ok().flatten();
346308
Some(format.with_language(lang))
@@ -453,8 +415,8 @@ pub fn write_resources_to_file(resources: &[Resource], file_path: &String) -> Re
453415
Some("AndroidStrings") => AndroidStringsFormat::from(first.clone()).write_to(path)?,
454416
Some("Strings") => StringsFormat::try_from(first.clone())?.write_to(path)?,
455417
Some("Xcstrings") => XcstringsFormat::try_from(resources.to_vec())?.write_to(path)?,
456-
Some("CSV") => CSVFormat::try_from(vec![first.clone()])?.write_to(path)?,
457-
Some("TSV") => TSVFormat::try_from(vec![first.clone()])?.write_to(path)?,
418+
Some("CSV") => CSVFormat::try_from(resources.to_vec())?.write_to(path)?,
419+
Some("TSV") => TSVFormat::try_from(resources.to_vec())?.write_to(path)?,
458420
_ => Err(Error::UnsupportedFormat(format!(
459421
"Unsupported format: {:?}",
460422
first.metadata.custom.get("format")

0 commit comments

Comments
 (0)