Skip to content

Commit f3437eb

Browse files
authored
feat!: optionally filter locales
1 parent ab047fe commit f3437eb

File tree

6 files changed

+22
-21
lines changed

6 files changed

+22
-21
lines changed

examples/all_files.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn main() {
1313
for path in Iter::new(default_paths()) {
1414
let path_src = PathSource::guess_from(&path);
1515
if let Ok(bytes) = fs::read_to_string(&path) {
16-
if let Ok(entry) = DesktopEntry::from_str(&path, &bytes, &locales) {
16+
if let Ok(entry) = DesktopEntry::from_str(&path, &bytes, Some(&locales)) {
1717
println!("{:?}: {}\n---\n{}", path_src, path.display(), entry);
1818
}
1919
}

examples/bench.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn bench_borrowed(it: u32) {
2626

2727
for path in paths {
2828
if let Ok(bytes) = fs::read_to_string(&path) {
29-
if let Ok(_entry) = DesktopEntry::from_str(&path, &bytes, &locale) {}
29+
if let Ok(_entry) = DesktopEntry::from_str(&path, &bytes, Some(&locale)) {}
3030
}
3131
}
3232

@@ -46,7 +46,7 @@ fn bench_owned(it: u32) {
4646
let now = Instant::now();
4747

4848
for path in paths {
49-
if let Ok(_entry) = DesktopEntry::from_path(path, &locale) {}
49+
if let Ok(_entry) = DesktopEntry::from_path(path, Some(&locale)) {}
5050
}
5151

5252
total_time += now.elapsed();
@@ -64,7 +64,7 @@ fn bench_owned_optimized(it: u32) {
6464

6565
let now = Instant::now();
6666

67-
let _ = DesktopEntry::from_paths(paths, &locale)
67+
let _ = DesktopEntry::from_paths(paths, Some(&locale))
6868
.filter_map(|e| e.ok())
6969
.collect::<Vec<_>>();
7070

examples/specific_file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn main() {
1212
// }
1313
// }
1414

15-
if let Ok(entry) = DesktopEntry::from_path(path.to_path_buf(), locales) {
15+
if let Ok(entry) = DesktopEntry::from_path(path.to_path_buf(), Some(locales)) {
1616
println!("{}\n---\n{}", path.display(), entry);
1717
}
1818
}

src/decoder.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl<'a> DesktopEntry<'a> {
2525
pub fn from_str<L>(
2626
path: &'a Path,
2727
input: &'a str,
28-
locales: &[L],
28+
locales_filter: Option<&[L]>,
2929
) -> Result<DesktopEntry<'a>, DecodeError>
3030
where
3131
L: AsRef<str>,
@@ -36,15 +36,15 @@ impl<'a> DesktopEntry<'a> {
3636
let mut active_group = Cow::Borrowed("");
3737
let mut ubuntu_gettext_domain = None;
3838

39-
let locales = add_generic_locales(locales);
39+
let locales_filter = locales_filter.map(add_generic_locales);
4040

4141
for line in input.lines() {
4242
process_line(
4343
line,
4444
&mut groups,
4545
&mut active_group,
4646
&mut ubuntu_gettext_domain,
47-
&locales,
47+
locales_filter.as_deref(),
4848
Cow::Borrowed,
4949
)
5050
}
@@ -59,25 +59,25 @@ impl<'a> DesktopEntry<'a> {
5959

6060
pub fn from_paths<'i, 'l: 'i, L>(
6161
paths: impl Iterator<Item = PathBuf> + 'i,
62-
locales: &'l [L],
62+
locales_filter: Option<&'l [L]>,
6363
) -> impl Iterator<Item = Result<DesktopEntry<'static>, DecodeError>> + 'i
6464
where
6565
L: AsRef<str>,
6666
{
6767
let mut buf = String::new();
68-
let locales = add_generic_locales(locales);
68+
let locales_filter = locales_filter.map(add_generic_locales);
6969

70-
paths.map(move |path| decode_from_path_with_buf(path, &locales, &mut buf))
70+
paths.map(move |path| decode_from_path_with_buf(path, locales_filter.as_deref(), &mut buf))
7171
}
7272

7373
/// Return an owned [`DesktopEntry`]
74-
pub fn from_path<L>(path: PathBuf, locales: &[L]) -> Result<DesktopEntry<'static>, DecodeError>
74+
pub fn from_path<L>(path: PathBuf, locales_filter: Option<&[L]>) -> Result<DesktopEntry<'static>, DecodeError>
7575
where
7676
L: AsRef<str>,
7777
{
7878
let mut buf = String::new();
79-
let locales = add_generic_locales(locales);
80-
decode_from_path_with_buf(path, &locales, &mut buf)
79+
let locales_filter = locales_filter.map(add_generic_locales);
80+
decode_from_path_with_buf(path, locales_filter.as_deref(), &mut buf)
8181
}
8282
}
8383

@@ -97,7 +97,7 @@ fn process_line<'buf, 'local_ref, 'res: 'local_ref + 'buf, F, L>(
9797
groups: &'local_ref mut Groups<'res>,
9898
active_group: &'local_ref mut Cow<'res, str>,
9999
ubuntu_gettext_domain: &'local_ref mut Option<Cow<'res, str>>,
100-
locales_filter: &[L],
100+
locales_filter: Option<&[L]>,
101101
convert_to_cow: F,
102102
) where
103103
F: Fn(&'buf str) -> Cow<'res, str>,
@@ -124,8 +124,9 @@ fn process_line<'buf, 'local_ref, 'res: 'local_ref + 'buf, F, L>(
124124
let key_name = &key[..start];
125125
let locale = &key[start + 1..key.len() - 1];
126126

127-
if !locales_filter.iter().any(|l| l.as_ref() == locale) {
128-
return;
127+
match locales_filter {
128+
Some(locales_filter) if !locales_filter.iter().any(|l| l.as_ref() == locale) => return,
129+
_ => (),
129130
}
130131

131132
groups
@@ -157,7 +158,7 @@ fn process_line<'buf, 'local_ref, 'res: 'local_ref + 'buf, F, L>(
157158
#[inline]
158159
fn decode_from_path_with_buf<L>(
159160
path: PathBuf,
160-
locales: &[L],
161+
locales_filter: Option<&[L]>,
161162
buf: &mut String,
162163
) -> Result<DesktopEntry<'static>, DecodeError>
163164
where
@@ -179,7 +180,7 @@ where
179180
&mut groups,
180181
&mut active_group,
181182
&mut ubuntu_gettext_domain,
182-
locales,
183+
locales_filter,
183184
|s| Cow::Owned(s.to_owned()),
184185
);
185186
buf.clear();

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ fn add_field() {
476476
fn env_with_locale() {
477477
let locales = &["fr_FR"];
478478

479-
let de = DesktopEntry::from_path(PathBuf::from("tests/org.mozilla.firefox.desktop"), locales)
479+
let de = DesktopEntry::from_path(PathBuf::from("tests/org.mozilla.firefox.desktop"), Some(locales))
480480
.unwrap();
481481

482482
assert_eq!(de.generic_name(locales).unwrap(), "Navigateur Web");

src/matching.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ mod test {
242242
#[test]
243243
fn find_de() {
244244
let entries =
245-
DesktopEntry::from_paths(Iter::new(default_paths()), &get_languages_from_env())
245+
DesktopEntry::from_paths(Iter::new(default_paths()), Some(&get_languages_from_env()))
246246
.filter_map(|e| e.ok())
247247
.collect::<Vec<_>>();
248248

0 commit comments

Comments
 (0)