| 1 |
#[macro_use] extern crate failure;
|
| 2 |
#[macro_use] extern crate lazy_static;
|
| 3 |
extern crate regex;
|
| 4 |
|
| 5 |
use failure::Error;
|
| 6 |
|
| 7 |
use std::collections::HashMap;
|
| 8 |
use std::io::{Read};
|
| 9 |
|
| 10 |
#[derive(Debug)]
|
| 11 |
pub struct DesktopEntry {
|
| 12 |
pub info: DesktopEntryInfo,
|
| 13 |
pub typ: EntryType,
|
| 14 |
}
|
| 15 |
|
| 16 |
#[derive(Debug)]
|
| 17 |
pub enum EntryType {
|
| 18 |
Application(ApplicationInfo),
|
| 19 |
Link(String),
|
| 20 |
Directory,
|
| 21 |
}
|
| 22 |
|
| 23 |
#[derive(Debug)]
|
| 24 |
pub struct ApplicationInfo {
|
| 25 |
pub dbus_activatable: bool,
|
| 26 |
pub try_exec: Option<String>,
|
| 27 |
pub exec: Option<String>,
|
| 28 |
pub path: Option<String>,
|
| 29 |
pub terminal: bool,
|
| 30 |
pub actions: Vec<Action>,
|
| 31 |
pub mime_type: Vec<String>,
|
| 32 |
pub categories: Vec<String>,
|
| 33 |
pub implements: Vec<String>,
|
| 34 |
pub keywords: Vec<String>,
|
| 35 |
pub startup_notify: bool,
|
| 36 |
pub startup_wm_class: Option<String>,
|
| 37 |
}
|
| 38 |
|
| 39 |
#[derive(Debug)]
|
| 40 |
pub struct Action {
|
| 41 |
pub name: String,
|
| 42 |
pub icon: Option<String>,
|
| 43 |
pub exec: Option<String>,
|
| 44 |
}
|
| 45 |
|
| 46 |
impl Action {
|
| 47 |
fn from_hashmap(bag: &mut HashMap<String, String>) -> Result<Action, Error> {
|
| 48 |
let name = bag.remove("Name")
|
| 49 |
.ok_or(format_err!("No Name!"))?;
|
| 50 |
let icon = bag.remove("Icon");
|
| 51 |
let exec = bag.remove("Exec");
|
| 52 |
|
| 53 |
Ok(Action { name, icon, exec })
|
| 54 |
}
|
| 55 |
}
|
| 56 |
|
| 57 |
impl ApplicationInfo {
|
| 58 |
fn from_hashmap(
|
| 59 |
bag: &mut HashMap<String, String>,
|
| 60 |
rest: &mut parser::Bag,
|
| 61 |
) ->
|
| 62 |
Result<ApplicationInfo, Error>
|
| 63 |
{
|
| 64 |
let exec = bag.remove("Exec");
|
| 65 |
let path = bag.remove("Path");
|
| 66 |
let try_exec = bag.remove("TryExec");
|
| 67 |
|
| 68 |
|
| 69 |
let actions = bag.remove("Actions")
|
| 70 |
.map_or_else(|| Vec::new(), parser::list_of_strings);
|
| 71 |
let actions: Result<Vec<Action>, Error> =
|
| 72 |
actions.into_iter().map(|a: String| {
|
| 73 |
let key = format!("Desktop Action {}", a);
|
| 74 |
let mut b = rest.remove(&key)
|
| 75 |
.ok_or(format_err!("No action named {}", key))?;
|
| 76 |
let r: Result<Action, Error> = Action::from_hashmap(&mut b);
|
| 77 |
r
|
| 78 |
}).collect();
|
| 79 |
let actions = actions?;
|
| 80 |
|
| 81 |
let mime_type = bag.remove("MimeType")
|
| 82 |
.map_or_else(|| Vec::new(), parser::list_of_strings);
|
| 83 |
let categories = bag.remove("Categories")
|
| 84 |
.map_or_else(|| Vec::new(), parser::list_of_strings);
|
| 85 |
let implements = bag.remove("Implements")
|
| 86 |
.map_or_else(|| Vec::new(), parser::list_of_strings);
|
| 87 |
let keywords = bag.remove("Keywords")
|
| 88 |
.map_or_else(|| Vec::new(), parser::list_of_strings);
|
| 89 |
|
| 90 |
let terminal = bag.remove("Terminal")
|
| 91 |
.map_or_else(|| Ok(false), parser::to_bool)?;
|
| 92 |
let dbus_activatable = bag.remove("DbusActivatable")
|
| 93 |
.map_or_else(|| Ok(false), parser::to_bool)?;
|
| 94 |
|
| 95 |
let startup_notify = bag.remove("StartupNotify")
|
| 96 |
.map_or_else(|| Ok(false), parser::to_bool)?;
|
| 97 |
let startup_wm_class = None;
|
| 98 |
|
| 99 |
Ok(ApplicationInfo {
|
| 100 |
dbus_activatable,
|
| 101 |
try_exec,
|
| 102 |
exec,
|
| 103 |
path,
|
| 104 |
terminal,
|
| 105 |
actions,
|
| 106 |
mime_type,
|
| 107 |
categories,
|
| 108 |
implements,
|
| 109 |
keywords,
|
| 110 |
startup_notify,
|
| 111 |
startup_wm_class,
|
| 112 |
})
|
| 113 |
}
|
| 114 |
}
|
| 115 |
|
| 116 |
#[derive(Debug)]
|
| 117 |
pub struct DesktopEntryInfo {
|
| 118 |
pub version: Option<String>,
|
| 119 |
pub name: String,
|
| 120 |
pub generic_name: Option<String>,
|
| 121 |
pub no_display: bool,
|
| 122 |
pub comment: Option<String>,
|
| 123 |
pub icon: Option<String>,
|
| 124 |
pub hidden: bool,
|
| 125 |
pub only_show_in: Vec<String>,
|
| 126 |
pub not_show_in: Vec<String>,
|
| 127 |
}
|
| 128 |
|
| 129 |
|
| 130 |
mod parser {
|
| 131 |
use std::collections::HashMap;
|
| 132 |
use std::io::{BufRead, BufReader, Read};
|
| 133 |
use std::mem;
|
| 134 |
|
| 135 |
use failure::Error;
|
| 136 |
use regex::Regex;
|
| 137 |
|
| 138 |
pub type Bag = HashMap<String, HashMap<String, String>>;
|
| 139 |
|
| 140 |
pub fn bags_from_file<R: Read>(reader: &mut R) -> Result<Bag, Error> {
|
| 141 |
lazy_static! {
|
| 142 |
static ref SECTION: Regex =
|
| 143 |
Regex::new(r"\[([A-Za-z0-9 -]*)\]").unwrap();
|
| 144 |
static ref KV: Regex =
|
| 145 |
Regex::new(r"([A-Za-z0-9-]*(\[[A-Za-z@_]*\])?)=(.*)").unwrap();
|
| 146 |
}
|
| 147 |
|
| 148 |
let reader = BufReader::new(reader);
|
| 149 |
let mut bags = HashMap::new();
|
| 150 |
let mut current_section = None;
|
| 151 |
let mut current_bag = HashMap::new();
|
| 152 |
|
| 153 |
for ln in reader.lines() {
|
| 154 |
let ln = ln?;
|
| 155 |
|
| 156 |
if let Some(kv) = KV.captures(&ln) {
|
| 157 |
current_bag.insert(kv[1].to_owned(), kv[3].to_owned());
|
| 158 |
} else if let Some(s) = SECTION.captures(&ln) {
|
| 159 |
if let Some(name) = current_section.take() {
|
| 160 |
let bag = mem::replace(&mut current_bag, HashMap::new());
|
| 161 |
bags.insert(name, bag);
|
| 162 |
}
|
| 163 |
current_section = Some(s[1].to_owned());
|
| 164 |
}
|
| 165 |
}
|
| 166 |
|
| 167 |
if let Some(name) = current_section.take() {
|
| 168 |
bags.insert(name, current_bag);
|
| 169 |
}
|
| 170 |
|
| 171 |
Ok(bags)
|
| 172 |
}
|
| 173 |
|
| 174 |
pub fn list_of_strings(str: String) -> Vec<String> {
|
| 175 |
str.split(";").filter(|s| s.len() > 0).map(|s| s.to_owned()).collect()
|
| 176 |
}
|
| 177 |
|
| 178 |
pub fn to_bool(str: String) -> Result<bool, Error> {
|
| 179 |
match str.as_ref() {
|
| 180 |
"true" => Ok(true),
|
| 181 |
"false" => Ok(false),
|
| 182 |
_ => Err(format_err!("Invalid value for boolean field: {}", str)),
|
| 183 |
}
|
| 184 |
}
|
| 185 |
}
|
| 186 |
|
| 187 |
impl DesktopEntryInfo {
|
| 188 |
pub fn from_bag(bags: &mut parser::Bag) -> Result<DesktopEntryInfo, Error> {
|
| 189 |
let bag = bags.get_mut("Desktop Entry")
|
| 190 |
.ok_or(format_err!("No Desktop Entry"))?;
|
| 191 |
|
| 192 |
let version = bag.remove("Version");
|
| 193 |
|
| 194 |
let name = bag.remove("Name")
|
| 195 |
.ok_or(format_err!("No name"))?
|
| 196 |
.to_owned();
|
| 197 |
let generic_name = bag.remove("Generic Name");
|
| 198 |
let comment = bag.remove("Comment");
|
| 199 |
let icon = bag.remove("Icon");
|
| 200 |
|
| 201 |
let no_display = bag.remove("No Display")
|
| 202 |
.map_or_else(|| Ok(false), parser::to_bool)?;
|
| 203 |
let hidden = bag.remove("Hidden")
|
| 204 |
.map_or_else(|| Ok(false), parser::to_bool)?;
|
| 205 |
|
| 206 |
let only_show_in = bag.remove("OnlyShowIn")
|
| 207 |
.map_or_else(|| Vec::new(), parser::list_of_strings);
|
| 208 |
let not_show_in = bag.remove("NotShowIn")
|
| 209 |
.map_or_else(|| Vec::new(), parser::list_of_strings);
|
| 210 |
|
| 211 |
let info = DesktopEntryInfo {
|
| 212 |
version,
|
| 213 |
name,
|
| 214 |
generic_name,
|
| 215 |
no_display,
|
| 216 |
comment,
|
| 217 |
icon,
|
| 218 |
hidden,
|
| 219 |
only_show_in,
|
| 220 |
not_show_in,
|
| 221 |
};
|
| 222 |
|
| 223 |
Ok(info)
|
| 224 |
}
|
| 225 |
}
|
| 226 |
|
| 227 |
impl DesktopEntry {
|
| 228 |
pub fn from_file<R: Read>(reader: &mut R) -> Result<DesktopEntry, Error> {
|
| 229 |
let mut bags = parser::bags_from_file(reader)?;
|
| 230 |
DesktopEntry::from_bags(&mut bags)
|
| 231 |
}
|
| 232 |
|
| 233 |
fn from_bags(bags: &mut parser::Bag) -> Result<DesktopEntry, Error> {
|
| 234 |
let info = DesktopEntryInfo::from_bag(bags)?;
|
| 235 |
let mut bag = bags.remove("Desktop Entry")
|
| 236 |
.ok_or(format_err!("No Desktop Entry"))?;
|
| 237 |
let typ = bag.remove("Type")
|
| 238 |
.ok_or(format_err!("No Type"))?;
|
| 239 |
let typ = match typ.as_ref() {
|
| 240 |
"Application" => {
|
| 241 |
let app = ApplicationInfo::from_hashmap(&mut bag, bags)?;
|
| 242 |
EntryType::Application(app)
|
| 243 |
}
|
| 244 |
"Link" => {
|
| 245 |
let url = bag.remove("URL")
|
| 246 |
.ok_or(format_err!("No URL"))?;
|
| 247 |
EntryType::Link(url)
|
| 248 |
}
|
| 249 |
"Directory" => EntryType::Directory,
|
| 250 |
r => Err(format_err!("Bad type: {}", r))?,
|
| 251 |
};
|
| 252 |
Ok(DesktopEntry { info, typ })
|
| 253 |
}
|
| 254 |
}
|
| 255 |
|
| 256 |
|
| 257 |
#[cfg(test)]
|
| 258 |
mod tests {
|
| 259 |
#[test]
|
| 260 |
fn firefox_example() {
|
| 261 |
let firefox_example = include_str!("../test_cases/firefox.desktop");
|
| 262 |
let mut f = ::std::io::Cursor::new(firefox_example);
|
| 263 |
let mut entry = ::parser::bags_from_file(&mut f).unwrap();
|
| 264 |
assert_eq!(
|
| 265 |
entry["Desktop Entry"]["Name"],
|
| 266 |
"Firefox".to_owned(),
|
| 267 |
);
|
| 268 |
|
| 269 |
let info = ::DesktopEntryInfo::from_bag(&mut entry).unwrap();
|
| 270 |
println!("Got: {:#?}", info);
|
| 271 |
assert_eq!(
|
| 272 |
info.name,
|
| 273 |
"Firefox".to_owned(),
|
| 274 |
);
|
| 275 |
}
|
| 276 |
|
| 277 |
#[test]
|
| 278 |
fn steam_example() {
|
| 279 |
let steam_example = include_str!("../test_cases/steam.desktop");
|
| 280 |
let mut f = ::std::io::Cursor::new(steam_example);
|
| 281 |
let entry = ::DesktopEntry::from_file(&mut f).unwrap();
|
| 282 |
println!("got {:#?}", entry);
|
| 283 |
assert_eq!(
|
| 284 |
entry.info.name,
|
| 285 |
"Steam".to_owned(),
|
| 286 |
);
|
| 287 |
}
|
| 288 |
}
|