|
| 1 | +use std::{ |
| 2 | + borrow::Cow, |
| 3 | + collections::BTreeMap, |
| 4 | + fs::File, |
| 5 | + io::{self, BufRead}, |
| 6 | + path::{Path, PathBuf}, |
| 7 | +}; |
| 8 | + |
| 9 | +use crate::DesktopEntry; |
| 10 | +use crate::{Groups, LocaleMap}; |
| 11 | +use thiserror::Error; |
| 12 | + |
| 13 | +#[derive(Debug, Error)] |
| 14 | +pub enum DecodeError { |
| 15 | + #[error("path does not contain a valid app ID")] |
| 16 | + AppID, |
| 17 | + #[error(transparent)] |
| 18 | + Io(#[from] std::io::Error), |
| 19 | +} |
| 20 | + |
| 21 | +impl<'a> DesktopEntry<'a> { |
| 22 | + pub fn decode_from_str<L>( |
| 23 | + path: &'a Path, |
| 24 | + input: &'a str, |
| 25 | + locales: &[L], |
| 26 | + ) -> Result<DesktopEntry<'a>, DecodeError> |
| 27 | + where |
| 28 | + L: AsRef<str>, |
| 29 | + { |
| 30 | + let appid = get_app_id(path)?; |
| 31 | + |
| 32 | + let mut groups = Groups::new(); |
| 33 | + let mut active_group = Cow::Borrowed(""); |
| 34 | + let mut ubuntu_gettext_domain = None; |
| 35 | + |
| 36 | + for line in input.lines() { |
| 37 | + process_line( |
| 38 | + line, |
| 39 | + &mut groups, |
| 40 | + &mut active_group, |
| 41 | + &mut ubuntu_gettext_domain, |
| 42 | + locales, |
| 43 | + Cow::Borrowed, |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + Ok(DesktopEntry { |
| 48 | + appid: Cow::Borrowed(appid), |
| 49 | + groups, |
| 50 | + path: Cow::Borrowed(path), |
| 51 | + ubuntu_gettext_domain, |
| 52 | + }) |
| 53 | + } |
| 54 | + |
| 55 | + pub fn decode_from_paths<'i, 'l: 'i, L>( |
| 56 | + paths: impl Iterator<Item = PathBuf> + 'i, |
| 57 | + locales: &'l [L], |
| 58 | + ) -> impl Iterator<Item = Result<DesktopEntry<'static>, DecodeError>> + 'i |
| 59 | + where |
| 60 | + L: AsRef<str>, |
| 61 | + { |
| 62 | + let mut buf = String::new(); |
| 63 | + |
| 64 | + paths.map(move |path| decode_from_path_with_buf(path, locales, &mut buf)) |
| 65 | + } |
| 66 | + |
| 67 | + /// Return an owned [`DesktopEntry`] |
| 68 | + pub fn decode_from_path<L>( |
| 69 | + path: PathBuf, |
| 70 | + locales: &[L], |
| 71 | + ) -> Result<DesktopEntry<'static>, DecodeError> |
| 72 | + where |
| 73 | + L: AsRef<str>, |
| 74 | + { |
| 75 | + let mut buf = String::new(); |
| 76 | + decode_from_path_with_buf(path, locales, &mut buf) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +fn get_app_id<P: AsRef<Path> + ?Sized>(path: &P) -> Result<&str, DecodeError> { |
| 81 | + let appid = path |
| 82 | + .as_ref() |
| 83 | + .file_stem() |
| 84 | + .ok_or(DecodeError::AppID)? |
| 85 | + .to_str() |
| 86 | + .ok_or(DecodeError::AppID)?; |
| 87 | + Ok(appid) |
| 88 | +} |
| 89 | + |
| 90 | +#[inline] |
| 91 | +fn process_line<'buf, 'local_ref, 'res: 'local_ref + 'buf, F, L>( |
| 92 | + line: &'buf str, |
| 93 | + groups: &'local_ref mut Groups<'res>, |
| 94 | + active_group: &'local_ref mut Cow<'res, str>, |
| 95 | + ubuntu_gettext_domain: &'local_ref mut Option<Cow<'res, str>>, |
| 96 | + locales_filter: &[L], |
| 97 | + convert_to_cow: F, |
| 98 | +) where |
| 99 | + F: Fn(&'buf str) -> Cow<'res, str>, |
| 100 | + L: AsRef<str>, |
| 101 | +{ |
| 102 | + let line = line.trim(); |
| 103 | + if line.is_empty() || line.starts_with('#') { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + let line_bytes = line.as_bytes(); |
| 108 | + |
| 109 | + if line_bytes[0] == b'[' { |
| 110 | + if let Some(end) = memchr::memrchr(b']', &line_bytes[1..]) { |
| 111 | + *active_group = convert_to_cow(&line[1..end + 1]); |
| 112 | + } |
| 113 | + } else if let Some(delimiter) = memchr::memchr(b'=', line_bytes) { |
| 114 | + let key = &line[..delimiter]; |
| 115 | + let value = &line[delimiter + 1..]; |
| 116 | + |
| 117 | + // if locale |
| 118 | + if key.as_bytes()[key.len() - 1] == b']' { |
| 119 | + if let Some(start) = memchr::memchr(b'[', key.as_bytes()) { |
| 120 | + let key_name = &key[..start]; |
| 121 | + let locale = &key[start + 1..key.len() - 1]; |
| 122 | + |
| 123 | + if !locales_filter.iter().any(|l| l.as_ref() == locale) { |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + groups |
| 128 | + .entry(active_group.clone()) |
| 129 | + .or_default() |
| 130 | + .entry(convert_to_cow(key_name)) |
| 131 | + .or_insert_with(|| (Cow::Borrowed(""), LocaleMap::new())) |
| 132 | + .1 |
| 133 | + .insert(convert_to_cow(locale), convert_to_cow(value)); |
| 134 | + |
| 135 | + return; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + if key == "X-Ubuntu-Gettext-Domain" { |
| 140 | + *ubuntu_gettext_domain = Some(convert_to_cow(value)); |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + groups |
| 145 | + .entry(active_group.clone()) |
| 146 | + .or_default() |
| 147 | + .entry(convert_to_cow(key)) |
| 148 | + .or_insert_with(|| (Cow::Borrowed(""), BTreeMap::new())) |
| 149 | + .0 = convert_to_cow(value); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +#[inline] |
| 154 | +fn decode_from_path_with_buf<L>( |
| 155 | + path: PathBuf, |
| 156 | + locales: &[L], |
| 157 | + buf: &mut String, |
| 158 | +) -> Result<DesktopEntry<'static>, DecodeError> |
| 159 | +where |
| 160 | + L: AsRef<str>, |
| 161 | +{ |
| 162 | + let file = File::open(&path)?; |
| 163 | + |
| 164 | + let appid = get_app_id(&path)?; |
| 165 | + |
| 166 | + let mut groups = Groups::new(); |
| 167 | + let mut active_group = Cow::Borrowed(""); |
| 168 | + let mut ubuntu_gettext_domain = None; |
| 169 | + |
| 170 | + let mut reader = io::BufReader::new(file); |
| 171 | + |
| 172 | + while reader.read_line(buf)? != 0 { |
| 173 | + process_line( |
| 174 | + buf, |
| 175 | + &mut groups, |
| 176 | + &mut active_group, |
| 177 | + &mut ubuntu_gettext_domain, |
| 178 | + locales, |
| 179 | + |s| Cow::Owned(s.to_owned()), |
| 180 | + ); |
| 181 | + buf.clear(); |
| 182 | + } |
| 183 | + |
| 184 | + Ok(DesktopEntry { |
| 185 | + appid: Cow::Owned(appid.to_owned()), |
| 186 | + groups, |
| 187 | + path: Cow::Owned(path), |
| 188 | + ubuntu_gettext_domain, |
| 189 | + }) |
| 190 | +} |
0 commit comments