mirror of
https://github.com/chmln/handlr.git
synced 2024-11-14 21:49:27 +01:00
desktop entry - clean up
This commit is contained in:
parent
9d726cb6af
commit
1c461f5b57
1 changed files with 44 additions and 50 deletions
|
@ -1,9 +1,10 @@
|
|||
use crate::{Error, Result};
|
||||
use mime::Mime;
|
||||
use pest::Parser;
|
||||
use std::{
|
||||
convert::TryFrom,
|
||||
ffi::OsString,
|
||||
path::PathBuf,
|
||||
path::{Path, PathBuf},
|
||||
process::{Command, Stdio},
|
||||
str::FromStr,
|
||||
};
|
||||
|
@ -69,39 +70,29 @@ impl DesktopEntry {
|
|||
}
|
||||
}
|
||||
|
||||
impl TryFrom<PathBuf> for DesktopEntry {
|
||||
type Error = Error;
|
||||
fn try_from(p: PathBuf) -> Result<DesktopEntry> {
|
||||
use pest::Parser;
|
||||
let raw = std::fs::read_to_string(&p)?;
|
||||
let file = Self::parse(Rule::file, &raw)?.next().unwrap();
|
||||
fn parse_file(path: &Path) -> Option<DesktopEntry> {
|
||||
let raw = std::fs::read_to_string(&path).ok()?;
|
||||
let file = DesktopEntry::parse(Rule::file, &raw).ok()?.next()?;
|
||||
|
||||
let mut entry = DesktopEntry::default();
|
||||
entry.file_name = path.file_name()?.to_owned();
|
||||
|
||||
let mut entry = Self::default();
|
||||
entry.file_name = p.file_name().unwrap().to_owned();
|
||||
let mut section = "";
|
||||
|
||||
for line in file.into_inner() {
|
||||
match line.as_rule() {
|
||||
Rule::section => {
|
||||
section = line.into_inner().as_str();
|
||||
}
|
||||
Rule::section => section = line.into_inner().as_str(),
|
||||
Rule::property if section == "Desktop Entry" => {
|
||||
let mut inner_rules = line.into_inner(); // { name ~ "=" ~ value }
|
||||
let mut inner = line.into_inner(); // { name ~ "=" ~ value }
|
||||
|
||||
let name = inner_rules.next().unwrap().as_str();
|
||||
match name {
|
||||
"Name" if entry.name.is_empty() => {
|
||||
entry.name =
|
||||
inner_rules.next().unwrap().as_str().into();
|
||||
}
|
||||
"Exec" => {
|
||||
entry.exec =
|
||||
inner_rules.next().unwrap().as_str().into();
|
||||
match inner.next()?.as_str() {
|
||||
"Name" if entry.name == "" => {
|
||||
entry.name = inner.next()?.as_str().into()
|
||||
}
|
||||
"Exec" => entry.exec = inner.next()?.as_str().into(),
|
||||
"MimeType" => {
|
||||
let mut mimes = inner_rules
|
||||
.next()
|
||||
.unwrap()
|
||||
let mut mimes = inner
|
||||
.next()?
|
||||
.as_str()
|
||||
.split(";")
|
||||
.filter_map(|m| Mime::from_str(m).ok())
|
||||
|
@ -109,10 +100,7 @@ impl TryFrom<PathBuf> for DesktopEntry {
|
|||
mimes.pop();
|
||||
entry.mimes = mimes;
|
||||
}
|
||||
"Terminal" => {
|
||||
entry.term =
|
||||
inner_rules.next().unwrap().as_str() == "true"
|
||||
}
|
||||
"Terminal" => entry.term = inner.next()?.as_str() == "true",
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
@ -121,9 +109,15 @@ impl TryFrom<PathBuf> for DesktopEntry {
|
|||
}
|
||||
|
||||
if !entry.name.is_empty() && !entry.exec.is_empty() {
|
||||
Ok(entry)
|
||||
Some(entry)
|
||||
} else {
|
||||
Err(Error::BadEntry(p.clone()))
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<PathBuf> for DesktopEntry {
|
||||
type Error = Error;
|
||||
fn try_from(path: PathBuf) -> Result<DesktopEntry> {
|
||||
parse_file(&path).ok_or(Error::BadEntry(path))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue