freedesktop-desktop-entry 0.8.1

Freedesktop Desktop Entry Specification
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// Copyright 2021 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0

use std::{
    fs::{self},
    path::{Path, PathBuf},
};

use crate::{DesktopEntry, Group};
use crate::{Groups, LocaleMap};
use bstr::ByteSlice;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum DecodeError {
    #[error("path does not contain a valid app ID")]
    AppID,
    #[error(transparent)]
    Io(#[from] std::io::Error),
    #[error("MultipleGroupWithSameName")]
    MultipleGroupWithSameName,
    #[error("KeyValueWithoutAGroup")]
    KeyValueWithoutAGroup,
    #[error("InvalidKey. Accepted: A-Za-z0-9")]
    InvalidKey,
    #[error("KeyDoesNotExist, this can happen when a localized key has no default value")]
    KeyDoesNotExist,
    #[error("InvalidValue")]
    InvalidValue,
    #[error("InvalidGroup")]
    InvalidGroup,
    #[error("InvalidEntry")]
    InvalidEntry,
}

#[derive(Debug, Eq, PartialEq)]
pub enum Line<'a> {
    Group(&'a str),
    Entry(&'a str, &'a str),
    Comment(&'a str),
}

#[inline]
pub fn parse_line<'a>(line: &'a str) -> Result<Line<'a>, DecodeError> {
    if line.trim().is_empty() || line.starts_with('#') {
        return Ok(Line::Comment(&line));
    }

    let line_bytes = line.as_bytes();

    // if group
    if line_bytes[0] == b'[' {
        if let Some(end) = memchr::memrchr(b']', &line_bytes[1..]) {
            let group_name = &line[1..end + 1];
            return Ok(Line::Group(group_name));
        } else {
            return Err(DecodeError::InvalidGroup);
        }
    }
    // else, if entry
    else if let Some(delimiter) = memchr::memchr(b'=', line_bytes) {
        let key = &line[..delimiter];
        let value = &line[delimiter + 1..];

        if key.is_empty() {
            return Err(DecodeError::InvalidKey);
        }
        return Ok(Line::Entry(key, value));
    } else {
        return Err(DecodeError::InvalidEntry);
    }
}

fn group_entry_from_str(
    input: &str,
    group: &str,
    entry: &str,
) -> Result<Option<String>, DecodeError> {
    let mut in_group = false;

    for line in input.lines() {
        match parse_line(line)? {
            Line::Group(parsed_group) => {
                in_group = parsed_group == group;
            }
            Line::Entry(key, value) => {
                if in_group && key == entry {
                    return Ok(Some(format_value(value)?));
                }
            }
            _ => (),
        }
    }

    Ok(None)
}

/// Return a single entry from a specified group
pub fn group_entry_from_path(
    path: impl Into<PathBuf>,
    group: &str,
    entry: &str,
) -> Result<Option<String>, DecodeError> {
    let path: PathBuf = path.into();
    let input = fs::read_to_string(&path)?;

    group_entry_from_str(&input, group, entry)
}

/// Return a single desktop entry
pub fn desktop_entry_from_path(
    path: impl Into<PathBuf>,
    entry: &str,
) -> Result<Option<String>, DecodeError> {
    let path: PathBuf = path.into();
    let input = fs::read_to_string(&path)?;

    group_entry_from_str(&input, "Desktop Entry", entry)
}

struct UnknownKey<'a> {
    key: &'a str,
    locale: String,
    value: String,
}

impl DesktopEntry {
    pub fn from_str<L>(
        path: impl Into<PathBuf>,
        input: &str,
        locales_filter: Option<&[L]>,
    ) -> Result<DesktopEntry, DecodeError>
    where
        L: AsRef<str>,
    {
        #[inline(never)]
        fn inner<'a>(
            path: PathBuf,
            input: &'a str,
            locales_filter: Option<Vec<&str>>,
        ) -> Result<DesktopEntry, DecodeError> {
            let path: PathBuf = path.into();

            let appid = get_app_id(&path)?;

            let mut groups = Groups::default();
            let mut active_group: Option<ActiveGroup> = None;
            let mut active_keys: Option<ActiveKeys> = None;
            let mut ubuntu_gettext_domain = None;

            let mut unknown_keys: Vec<UnknownKey> = Vec::new();

            for line in input.lines() {
                process_line(
                    line,
                    &mut groups,
                    &mut active_group,
                    &mut active_keys,
                    &mut ubuntu_gettext_domain,
                    locales_filter.as_deref(),
                    &mut unknown_keys,
                )?;
            }
            
            if let Some(active_keys) = active_keys.take() {
                match &mut active_group {
                    Some(active_group) => {
                        active_group.group.0.insert(
                            active_keys.key_name,
                            (active_keys.default_value, active_keys.locales),
                        );
                    }
                    None => return Err(DecodeError::KeyValueWithoutAGroup),
                }
            }

            // insert keys which have no group
            for unknown_key in unknown_keys.drain(..) {
                match &mut active_group {
                    Some(active_group) => match active_group.group.0.get_mut(unknown_key.key) {
                        Some((_, locale_map)) => {
                            locale_map.insert(unknown_key.locale, unknown_key.value);
                        }
                        None => return Err(DecodeError::KeyDoesNotExist),
                    },
                    None => return Err(DecodeError::KeyDoesNotExist),
                }
            }

            if let Some(mut group) = active_group.take() {
                groups
                    .0
                    .entry(group.group_name)
                    .or_insert_with(|| Group::default())
                    .0
                    .append(&mut group.group.0);
            }

            Ok(DesktopEntry {
                appid,
                groups,
                path,
                ubuntu_gettext_domain,
            })
        }

        inner(path.into(), input, locales_filter.map(add_generic_locales))
    }

    /// Return an owned [`DesktopEntry`]
    #[inline]
    pub fn from_path<L>(
        path: impl Into<PathBuf>,
        locales_filter: Option<&[L]>,
    ) -> Result<DesktopEntry, DecodeError>
    where
        L: AsRef<str>,
    {
        let path: PathBuf = path.into();
        let input = fs::read_to_string(&path)?;
        Self::from_str(path, &input, locales_filter)
    }
}

#[inline]
fn get_app_id<P: AsRef<Path> + ?Sized>(path: &P) -> Result<String, DecodeError> {
    let path_as_bytes = path
        .as_ref()
        .as_os_str()
        .as_encoded_bytes()
        .strip_suffix(b".desktop")
        .ok_or(DecodeError::AppID)?;

    Ok(
        if let Some((_prefix, entry)) = path_as_bytes.rsplit_once_str("/applications/") {
            String::from_utf8(entry.replace(b"/", b"-"))
                .ok()
                .ok_or(DecodeError::AppID)?
        } else {
            path.as_ref()
                .file_stem()
                .ok_or(DecodeError::AppID)?
                .to_str()
                .ok_or(DecodeError::AppID)?
                .to_owned()
        },
    )
}

#[derive(Debug)]
struct ActiveGroup {
    group_name: String,
    group: Group,
}

#[derive(Debug)]
struct ActiveKeys {
    key_name: String,
    default_value: String,
    locales: LocaleMap,
}

#[inline(never)]
fn process_line<'a>(
    line: &'a str,
    groups: &mut Groups,
    active_group: &mut Option<ActiveGroup>,
    active_keys: &mut Option<ActiveKeys>,
    ubuntu_gettext_domain: &mut Option<String>,
    locales_filter: Option<&[&str]>,
    unknown_keys: &mut Vec<UnknownKey<'a>>,
) -> Result<(), DecodeError> {
    match parse_line(line)? {
        Line::Group(group_name) => {
            // insert keys which have no group
            for unknown_key in unknown_keys.drain(..) {
                match active_group {
                    Some(active_group) => match active_group.group.0.get_mut(unknown_key.key) {
                        Some((_, locale_map)) => {
                            locale_map.insert(unknown_key.locale, unknown_key.value);
                        }
                        None => return Err(DecodeError::KeyDoesNotExist),
                    },
                    None => return Err(DecodeError::KeyDoesNotExist),
                }
            }

            if let Some(active_keys) = active_keys.take() {
                match active_group {
                    Some(active_group) => {
                        active_group.group.0.insert(
                            active_keys.key_name,
                            (active_keys.default_value, active_keys.locales),
                        );
                    }
                    None => return Err(DecodeError::KeyValueWithoutAGroup),
                }
            }

            if let Some(mut group) = active_group.take() {
                groups
                    .0
                    .entry(group.group_name)
                    .or_insert_with(|| Group::default())
                    .0
                    .append(&mut group.group.0);
            }

            active_group.replace(ActiveGroup {
                group_name: group_name.to_string(),
                group: Group::default(),
            });
        }
        Line::Entry(key, value) => {
            let value = format_value(value)?;

            // if locale
            if key.as_bytes()[key.len() - 1] == b']' {
                if let Some(start) = memchr::memchr(b'[', key.as_bytes()) {
                    let locale = &key[start + 1..key.len() - 1];

                    let key = &key[..start];

                    match locales_filter {
                        Some(locales_filter) if !locales_filter.iter().any(|l| *l == locale) => {
                            return Ok(());
                        }
                        _ => (),
                    }

                    // we verify that the name is the same of active key
                    // even tho this is forbidden by the spec, nautilus does this for example
                    if let Some(active_keys) = active_keys
                        .as_mut()
                        .filter(|active_keys| active_keys.key_name == key)
                    {
                        active_keys.locales.insert(locale.to_string(), value);
                    } else {
                        unknown_keys.push(UnknownKey {
                            key,
                            locale: locale.to_string(),
                            value,
                        });
                    }

                    return Ok(());
                }
            }

            if key == "X-Ubuntu-Gettext-Domain" {
                *ubuntu_gettext_domain = Some(value.to_string());
                return Ok(());
            }

            if let Some(active_keys) = active_keys.take() {
                match active_group {
                    Some(active_group) => {
                        active_group.group.0.insert(
                            active_keys.key_name,
                            (active_keys.default_value, active_keys.locales),
                        );
                    }
                    None => return Err(DecodeError::KeyValueWithoutAGroup),
                }
            }
            active_keys.replace(ActiveKeys {
                // todo: verify that the key only contains A-Za-z0-9 ?
                key_name: key.trim().to_string(),
                default_value: value,
                locales: LocaleMap::default(),
            });
        }
        _ => (),
    }
    Ok(())
}

// https://specifications.freedesktop.org/desktop-entry-spec/latest/value-types.html
#[inline]
pub(crate) fn format_value(input: &str) -> Result<String, DecodeError> {
    let input = if let Some(input) = input.strip_prefix(" ") {
        input
    } else {
        input
    };

    let mut res = String::with_capacity(input.len());

    let mut last: usize = 0;

    for i in memchr::memchr_iter(b'\\', input.as_bytes()) {
        // edge case for //
        if last > i {
            continue;
        }

        // when there is an \ at the end
        if input.len() <= i + 1 {
            return Err(DecodeError::InvalidValue);
        }

        if last < i {
            res.push_str(&input[last..i]);
        }

        last = i + 2;

        match input.as_bytes()[i + 1] {
            b's' => res.push(' '),
            b'n' => res.push('\n'),
            b't' => res.push('\t'),
            b'r' => res.push('\r'),
            b'\\' => res.push('\\'),
            _ => {
                return Err(DecodeError::InvalidValue);
            }
        }
    }

    if last < input.len() {
        res.push_str(&input[last..input.len()]);
    }

    Ok(res)
}

/// Ex: if a locale equal fr_FR, add fr
#[inline]
fn add_generic_locales<L: AsRef<str>>(locales: &[L]) -> Vec<&str> {
    let mut v = Vec::with_capacity(locales.len() + 1);

    for l in locales {
        let l = l.as_ref();

        v.push(l);

        if let Some(start) = memchr::memchr(b'_', l.as_bytes()) {
            v.push(l.split_at(start).0)
        }
    }

    v
}

#[cfg(test)]
mod test {
    use crate::{decoder::Line, parse_line};

    #[test]
    fn test_parse_empty_comment() {
        let line = parse_line("").unwrap();

        assert_eq!(line, Line::Comment(""));
    }

    #[test]
    fn test_parse_hash_comment() {
        let line = parse_line("# comment").unwrap();

        assert_eq!(line, Line::Comment("# comment"));
    }

    #[test]
    fn test_parse_group() {
        let line = parse_line("[Some Group]").unwrap();

        assert_eq!(line, Line::Group("Some Group"));
    }

    #[test]
    fn test_parse_entry() {
        let line = parse_line("key=value").unwrap();

        assert_eq!(line, Line::Entry("key", "value"));
    }

    #[test]
    fn test_group_error() {
        let result = parse_line("[Some Group");

        assert!(result.is_err());
    }

    #[test]
    fn test_entry_error() {
        let result = parse_line("invalid entry");

        assert!(result.is_err());
    }
}