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
|
use serde_derive::Deserialize;
use std::env;
use std::fmt;
#[derive(Clone, Debug, Deserialize)]
pub struct AppList {
#[serde(default, rename = "inherit")]
_inherit: String,
#[serde(default, rename = "app_list")]
_app_list: Vec<AppMimetypeEntry>,
}
impl AppList {
pub fn new(_inherit: String, _app_list: Vec<AppMimetypeEntry>) -> Self {
Self {
_inherit,
_app_list,
}
}
pub fn parent(&self) -> &str {
self._inherit.as_str()
}
pub fn app_list(&self) -> &[AppMimetypeEntry] {
self._app_list.as_slice()
}
}
#[derive(Clone, Debug, Deserialize)]
pub struct AppMimetypeEntry {
#[serde(rename = "command")]
_command: String,
#[serde(default, rename = "args")]
_args: Vec<String>,
#[serde(default, rename = "fork")]
_fork: bool,
#[serde(default, rename = "silent")]
_silent: bool,
#[serde(default, rename = "confirm_exit")]
_confirm_exit: bool,
}
impl AppMimetypeEntry {
pub fn new(command: String) -> Self {
Self {
_command: command,
_args: Vec::new(),
_fork: false,
_silent: false,
_confirm_exit: false,
}
}
#[allow(dead_code)]
pub fn arg<S: std::convert::Into<String>>(&mut self, arg: S) -> &mut Self {
self._args.push(arg.into());
self
}
pub fn args<I, S>(&mut self, args: I) -> &mut Self
where
I: Iterator<Item = S>,
S: std::convert::Into<String>,
{
args.for_each(|arg| self._args.push(arg.into()));
self
}
#[allow(dead_code)]
pub fn fork(&mut self, fork: bool) -> &mut Self {
self._fork = fork;
self
}
#[allow(dead_code)]
pub fn silent(&mut self, silent: bool) -> &mut Self {
self._silent = silent;
self
}
#[allow(dead_code)]
pub fn confirm_exit(&mut self, confirm_exit: bool) -> &mut Self {
self._confirm_exit = confirm_exit;
self
}
pub fn get_command(&self) -> &str {
self._command.as_str()
}
pub fn get_args(&self) -> &[String] {
&self._args
}
pub fn get_fork(&self) -> bool {
self._fork
}
pub fn get_silent(&self) -> bool {
self._silent
}
pub fn get_confirm_exit(&self) -> bool {
self._confirm_exit
}
// TODO: Windows support
pub fn program_exists(&self) -> bool {
let program = self.get_command();
env::var_os("PATH")
.map(|path| env::split_paths(&path).any(|dir| dir.join(program).is_file()))
.unwrap_or(false)
}
}
impl std::default::Default for AppMimetypeEntry {
fn default() -> Self {
Self {
_command: "".to_string(),
_args: Vec::new(),
_fork: false,
_silent: false,
_confirm_exit: false,
}
}
}
impl std::fmt::Display for AppMimetypeEntry {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.get_command()).unwrap();
self.get_args()
.iter()
.for_each(|arg| write!(f, " {}", arg).unwrap());
f.write_str(" ").unwrap();
if self.get_fork() {
f.write_str("[fork]").unwrap();
}
if self.get_silent() {
f.write_str("[silent]").unwrap();
}
if self.get_confirm_exit() {
f.write_str("[confirm-exit]").unwrap();
}
f.write_str("")
}
}
|