-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.rs
More file actions
219 lines (195 loc) · 5.28 KB
/
Copy pathconfig.rs
File metadata and controls
219 lines (195 loc) · 5.28 KB
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
use std::{collections::HashMap, str::FromStr};
use cba::{bird::transform::camelcase_normalized, define_collection_wrapper};
use mime_guess::Mime;
use crate::{
cli::paths::{BINARY_SHORT, current_exe},
lessfilter::{RulesConfig, file_rule::ParseFileRuleError},
};
use fist_types::{FileCategory, When};
#[derive(
Default,
Debug,
Hash,
PartialEq,
Eq,
Clone,
Copy,
serde::Serialize,
serde::Deserialize,
clap::ValueEnum,
strum::Display,
strum::EnumString,
)]
#[strum(serialize_all = "lowercase")]
pub enum Preset {
#[clap(alias = "p")]
/// For the f:ist preview pane.
///
/// see [`matchmaker::preview`]
Preview,
#[default]
#[clap(alias = "d")]
/// For terminal display.
Display,
#[clap(alias = "x")]
/// For terminal interaction/verbose display.
Extended,
#[clap(alias = "i")]
/// Metadata/raw info.
Info,
#[clap(alias = "o")]
/// System open.
///
/// (By deferring to fs :open)
Open,
/// Alternate (custom) open
Alternate,
/// Alternate (custom) open
Alternate2,
#[clap(alias = "e")]
// For [`crate::run::FsAction::Advance`]
Edit,
#[clap(skip)]
/// Default preset for configuration only
Default,
}
impl Preset {
pub fn to_command_string(
self,
header: When,
) -> String {
let header = match header {
When::Always => "--header=true",
When::Never => "--header=false",
When::Auto => "",
};
format!(
"'{}' :tool lessfilter {header} {self} {{}}",
current_exe().to_str().unwrap_or(BINARY_SHORT),
)
}
}
#[derive(Debug, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LessfilterConfig {
#[serde(flatten, default)]
pub settings: LessfilterSettings,
#[serde(default)]
pub rules: RulesConfig,
#[serde(default)]
pub actions: CustomActions,
#[serde(default)]
pub categories: Categories,
}
impl Default for LessfilterConfig {
fn default() -> Self {
let ret = toml::from_str(include_str!("../../assets/config/lessfilter.toml"));
ret.unwrap()
}
}
#[derive(Debug, serde::Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct LessfilterSettings {
#[serde(deserialize_with = "camelcase_normalized")]
pub infer: InferMode,
/// This has to do with how a single action can sometimes be multiple command-line programs. This stops execution when any fail -- do not set.
#[serde(skip)]
pub early_exit: bool,
pub tracked_presets: Vec<Preset>,
pub run: RunSettings,
}
#[derive(Default, Debug, serde::Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct RunSettings {
pub image_viewer: Vec<String>,
}
#[derive(Debug, Default, Copy, Clone, serde::Deserialize)]
pub enum InferMode {
Guess,
Infer,
#[default]
FileFormat,
}
impl Default for LessfilterSettings {
fn default() -> Self {
Self {
infer: Default::default(),
early_exit: false,
tracked_presets: vec![Preset::Edit, Preset::Alternate, Preset::Extended],
run: Default::default(),
}
}
}
define_collection_wrapper!(
/// Name => Shell Script
///
/// # Notes
/// Name is case insensitive
///
#[derive(Debug, serde::Serialize, serde::Deserialize)]
CustomActions: HashMap<String, String>
);
define_collection_wrapper!(
#[derive(Debug)]
Categories: HashMap<String, Vec<MimeString>>
);
// --------------------- BOILERPLATE ---------------------
#[derive(Default, Debug, serde::Deserialize, Clone)]
#[serde(default, transparent)]
pub struct MimeString(String);
impl FromStr for MimeString {
type Err = ParseFileRuleError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.matches('/').count() != 1 {
Err(ParseFileRuleError::InvalidMime)
} else {
Ok(MimeString(s.to_string()))
}
}
}
impl MimeString {
pub fn equal(
&self,
mime: &Mime,
) -> bool {
self.0 == mime.to_string()
}
pub fn matches_type(
&self,
r#type: &str,
) -> bool {
let (type_, _subtype) = self.0.split_once('/').unwrap();
type_.is_empty() || type_ == "*" || r#type == type_
}
pub fn matches_subtype(
&self,
subtype: &str,
) -> bool {
let (_type_, subtype_) = self.0.split_once('/').unwrap();
subtype_.is_empty() || subtype_ == "*" || subtype == subtype_
}
pub fn matches_any(&self) -> bool {
let (type_, subtype) = self.0.split_once('/').unwrap();
type_ == "*" && (subtype == "*" || subtype.is_empty())
}
}
// ---------------------
use serde::de::Error as DeError;
use serde::{Deserialize, Deserializer};
impl<'de> Deserialize<'de> for Categories {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let map = HashMap::<String, Vec<MimeString>>::deserialize(deserializer)?;
for key in map.keys() {
if FileCategory::from_str(key).is_ok() {
return Err(D::Error::custom(format!(
"key '{}' must not be a valid FileCategory",
key
)));
}
}
Ok(Categories(map))
}
}