Skip to content

Commit 20b056c

Browse files
committed
refactor(codec): streamline entry lookup methods
- Replaced manual entry search loops in `find_entries`, `find_entry`, and `find_entry_mut` with calls to new `find_entry` and `find_entry_mut` methods in the `Resource` struct. - This change enhances code readability and maintainability by encapsulating entry lookup logic within the `Resource` struct.
1 parent bae444e commit 20b056c

3 files changed

Lines changed: 35 additions & 12 deletions

File tree

langcodec/src/codec.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,8 @@ impl Codec {
154154
pub fn find_entries(&self, key: &str) -> Vec<(&Resource, &Entry)> {
155155
let mut results = Vec::new();
156156
for resource in &self.resources {
157-
for entry in &resource.entries {
158-
if entry.id == key {
159-
results.push((resource, entry));
160-
}
157+
if let Some(entry) = resource.find_entry(key) {
158+
results.push((resource, entry));
161159
}
162160
}
163161
results
@@ -187,10 +185,7 @@ impl Codec {
187185
/// }
188186
/// ```
189187
pub fn find_entry(&self, key: &str, language: &str) -> Option<&Entry> {
190-
self.get_by_language(language)?
191-
.entries
192-
.iter()
193-
.find(|entry| entry.id == key)
188+
self.get_by_language(language)?.find_entry(key)
194189
}
195190

196191
/// Finds a mutable entry by its key in a specific language.
@@ -219,10 +214,7 @@ impl Codec {
219214
/// }
220215
/// ```
221216
pub fn find_entry_mut(&mut self, key: &str, language: &str) -> Option<&mut Entry> {
222-
self.get_mut_by_language(language)?
223-
.entries
224-
.iter_mut()
225-
.find(|entry| entry.id == key)
217+
self.get_mut_by_language(language)?.find_entry_mut(key)
226218
}
227219

228220
/// Updates a translation for a specific key and language.

langcodec/src/formats/android_strings.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,29 @@ World
454454
assert_eq!(some_non_ascii.name, "some_non_ascii");
455455
assert_eq!(some_non_ascii.value, "你好");
456456
assert_eq!(some_non_ascii.translatable, None);
457+
458+
let resource = Resource::from(format);
459+
assert_eq!(resource.entries.len(), 5);
460+
let entry = &resource.entries[0];
461+
assert_eq!(entry.id, "hello");
462+
assert_eq!(entry.value, Translation::Singular("Hello".to_string()));
463+
assert_eq!(entry.status, EntryStatus::Translated);
464+
assert_eq!(entry.comment, None);
465+
466+
let entry = resource.find_entry("hello").unwrap();
467+
assert_eq!(entry.value, Translation::Singular("Hello".to_string()));
468+
assert_eq!(entry.status, EntryStatus::Translated);
469+
assert_eq!(entry.comment, None);
470+
471+
let entry = resource.find_entry("multiple_lines").unwrap();
472+
assert_eq!(entry.value, Translation::Singular("Hello\\n\\n\\nWorld\\n ".to_string()));
473+
assert_eq!(entry.status, EntryStatus::Translated);
474+
assert_eq!(entry.comment, None);
475+
476+
let entry = resource.find_entry("some_non_ascii").unwrap();
477+
assert_eq!(entry.value, Translation::Singular("你好".to_string()));
478+
assert_eq!(entry.status, EntryStatus::Translated);
479+
assert_eq!(entry.comment, None);
457480
}
458481

459482
#[test]

langcodec/src/types.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ impl Resource {
5050
self.entries.push(entry);
5151
}
5252

53+
pub fn find_entry(&self, id: &str) -> Option<&Entry> {
54+
self.entries.iter().find(|e| e.id == id)
55+
}
56+
57+
pub fn find_entry_mut(&mut self, id: &str) -> Option<&mut Entry> {
58+
self.entries.iter_mut().find(|e| e.id == id)
59+
}
60+
5361
pub fn parse_language_identifier(&self) -> Option<LanguageIdentifier> {
5462
self.metadata.language.parse().ok()
5563
}

0 commit comments

Comments
 (0)