Skip to content

Commit 240f7fd

Browse files
committed
Refactor plural form iteration and path handling
Simplifies iteration over plural forms in codec.rs by using values() and values_mut() instead of iterating with keys. In converter.rs, removes unnecessary to_path_buf() conversions and passes input/output as references, and consistently removes redundant references in read_from and write_to calls. Also removes an unused function in placeholder.rs.
1 parent bf4ae2c commit 240f7fd

3 files changed

Lines changed: 26 additions & 36 deletions

File tree

langcodec/src/codec.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -655,11 +655,7 @@ impl Codec {
655655
let sigs: Vec<Vec<String>> = match &entry.value {
656656
Translation::Singular(v) => vec![signature(v)],
657657
Translation::Plural(p) => {
658-
let mut all = Vec::new();
659-
for (_cat, v) in &p.forms {
660-
all.push(signature(v));
661-
}
662-
all
658+
p.forms.values().map(|v| signature(v)).collect()
663659
}
664660
};
665661
map.entry(entry.id.clone())
@@ -727,11 +723,7 @@ impl Codec {
727723
for entry in &res.entries {
728724
let sigs: Vec<Vec<String>> = match &entry.value {
729725
Translation::Singular(v) => vec![signature(v)],
730-
Translation::Plural(p) => p
731-
.forms
732-
.iter()
733-
.map(|(_c, v)| signature(v))
734-
.collect(),
726+
Translation::Plural(p) => p.forms.values().map(|v| signature(v)).collect(),
735727
};
736728
map.entry(entry.id.clone())
737729
.or_default()
@@ -795,7 +787,7 @@ impl Codec {
795787
*v = nv;
796788
}
797789
Translation::Plural(p) => {
798-
for (_c, v) in p.forms.iter_mut() {
790+
for v in p.forms.values_mut() {
799791
let nv = normalize_placeholders(v);
800792
*v = nv;
801793
}

langcodec/src/converter.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ pub fn convert<P: AsRef<Path>>(
145145

146146
// Read input as resources
147147
let mut resources = match input_format {
148-
FormatType::AndroidStrings(_) => vec![AndroidStringsFormat::read_from(&input)?.into()],
149-
FormatType::Strings(_) => vec![StringsFormat::read_from(&input)?.into()],
150-
FormatType::Xcstrings => Vec::<Resource>::try_from(XcstringsFormat::read_from(&input)?)?,
151-
FormatType::CSV => Vec::<Resource>::try_from(CSVFormat::read_from(&input)?)?,
152-
FormatType::TSV => Vec::<Resource>::try_from(TSVFormat::read_from(&input)?)?,
148+
FormatType::AndroidStrings(_) => vec![AndroidStringsFormat::read_from(input)?.into()],
149+
FormatType::Strings(_) => vec![StringsFormat::read_from(input)?.into()],
150+
FormatType::Xcstrings => Vec::<Resource>::try_from(XcstringsFormat::read_from(input)?)?,
151+
FormatType::CSV => Vec::<Resource>::try_from(CSVFormat::read_from(input)?)?,
152+
FormatType::TSV => Vec::<Resource>::try_from(TSVFormat::read_from(input)?)?,
153153
};
154154

155155
// Ensure language is set for single-language inputs if provided on input_format
@@ -173,7 +173,7 @@ pub fn convert<P: AsRef<Path>>(
173173
FormatType::AndroidStrings(lang) => {
174174
let resource = pick_resource(lang);
175175
if let Some(res) = resource {
176-
AndroidStringsFormat::from(res).write_to(&output)
176+
AndroidStringsFormat::from(res).write_to(output)
177177
} else {
178178
Err(Error::InvalidResource(
179179
"No matching resource for output language.".to_string(),
@@ -183,16 +183,16 @@ pub fn convert<P: AsRef<Path>>(
183183
FormatType::Strings(lang) => {
184184
let resource = pick_resource(lang);
185185
if let Some(res) = resource {
186-
StringsFormat::try_from(res)?.write_to(&output)
186+
StringsFormat::try_from(res)?.write_to(output)
187187
} else {
188188
Err(Error::InvalidResource(
189189
"No matching resource for output language.".to_string(),
190190
))
191191
}
192192
}
193-
FormatType::Xcstrings => XcstringsFormat::try_from(resources)?.write_to(&output),
194-
FormatType::CSV => CSVFormat::try_from(resources)?.write_to(&output),
195-
FormatType::TSV => TSVFormat::try_from(resources)?.write_to(&output),
193+
FormatType::Xcstrings => XcstringsFormat::try_from(resources)?.write_to(output),
194+
FormatType::CSV => CSVFormat::try_from(resources)?.write_to(output),
195+
FormatType::TSV => TSVFormat::try_from(resources)?.write_to(output),
196196
}
197197
}
198198

@@ -222,8 +222,8 @@ pub fn convert_with_normalization<P: AsRef<Path>>(
222222
output_format: FormatType,
223223
normalize: bool,
224224
) -> Result<(), Error> {
225-
let mut input = input.as_ref().to_path_buf();
226-
let mut output = output.as_ref().to_path_buf();
225+
let input = input.as_ref();
226+
let output = output.as_ref();
227227

228228
// Carry language between single-language formats
229229
let output_format = if let Some(lang) = input_format.language() {
@@ -240,11 +240,11 @@ pub fn convert_with_normalization<P: AsRef<Path>>(
240240

241241
// Read input as resources
242242
let mut resources = match input_format {
243-
FormatType::AndroidStrings(_) => vec![AndroidStringsFormat::read_from(&input)?.into()],
244-
FormatType::Strings(_) => vec![StringsFormat::read_from(&input)?.into()],
245-
FormatType::Xcstrings => Vec::<Resource>::try_from(XcstringsFormat::read_from(&input)?)?,
246-
FormatType::CSV => Vec::<Resource>::try_from(CSVFormat::read_from(&input)?)?,
247-
FormatType::TSV => Vec::<Resource>::try_from(TSVFormat::read_from(&input)?)?,
243+
FormatType::AndroidStrings(_) => vec![AndroidStringsFormat::read_from(input)?.into()],
244+
FormatType::Strings(_) => vec![StringsFormat::read_from(input)?.into()],
245+
FormatType::Xcstrings => Vec::<Resource>::try_from(XcstringsFormat::read_from(input)?)?,
246+
FormatType::CSV => Vec::<Resource>::try_from(CSVFormat::read_from(input)?)?,
247+
FormatType::TSV => Vec::<Resource>::try_from(TSVFormat::read_from(input)?)?,
248248
};
249249

250250
// Ensure language is set for single-language inputs if provided on input_format
@@ -261,7 +261,7 @@ pub fn convert_with_normalization<P: AsRef<Path>>(
261261
for entry in &mut res.entries {
262262
match &mut entry.value {
263263
crate::types::Translation::Singular(v) => {
264-
*v = normalize_placeholders(v).into();
264+
*v = normalize_placeholders(v);
265265
}
266266
crate::types::Translation::Plural(p) => {
267267
for (_c, v) in p.forms.iter_mut() {
@@ -285,7 +285,7 @@ pub fn convert_with_normalization<P: AsRef<Path>>(
285285
FormatType::AndroidStrings(lang) => {
286286
let resource = pick_resource(lang);
287287
if let Some(res) = resource {
288-
AndroidStringsFormat::from(res).write_to(&output)
288+
AndroidStringsFormat::from(res).write_to(output)
289289
} else {
290290
Err(Error::InvalidResource(
291291
"No matching resource for output language.".to_string(),
@@ -295,16 +295,16 @@ pub fn convert_with_normalization<P: AsRef<Path>>(
295295
FormatType::Strings(lang) => {
296296
let resource = pick_resource(lang);
297297
if let Some(res) = resource {
298-
StringsFormat::try_from(res)?.write_to(&output)
298+
StringsFormat::try_from(res)?.write_to(output)
299299
} else {
300300
Err(Error::InvalidResource(
301301
"No matching resource for output language.".to_string(),
302302
))
303303
}
304304
}
305-
FormatType::Xcstrings => XcstringsFormat::try_from(resources)?.write_to(&output),
306-
FormatType::CSV => CSVFormat::try_from(resources)?.write_to(&output),
307-
FormatType::TSV => TSVFormat::try_from(resources)?.write_to(&output),
305+
FormatType::Xcstrings => XcstringsFormat::try_from(resources)?.write_to(output),
306+
FormatType::CSV => CSVFormat::try_from(resources)?.write_to(output),
307+
FormatType::TSV => TSVFormat::try_from(resources)?.write_to(output),
308308
}
309309
}
310310

langcodec/src/placeholder.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ pub fn signature(input: &str) -> Vec<String> {
114114
.collect()
115115
}
116116

117-
fn canonical_kind(raw: &str) -> char { canonical_kind_char(raw.chars().next().unwrap_or('s')) }
118-
119117
fn canonical_kind_char(ch: char) -> char {
120118
match ch {
121119
'@' => 's',

0 commit comments

Comments
 (0)