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
|
use serde_derive::Deserialize;
use std::collections::HashMap;
use crate::config::{parse_to_config_file, TomlConfigFile};
use super::{AppList, AppMimetypeEntry};
pub type MimetypeRegistry = HashMap<String, AppList>;
#[derive(Debug, Deserialize)]
pub struct AppMimetypeRegistryCrude {
#[serde(default, rename = "class")]
pub _class: HashMap<String, Vec<AppMimetypeEntry>>,
#[serde(default, rename = "extension")]
pub _extension: MimetypeRegistry,
}
#[derive(Debug, Default)]
pub struct AppMimetypeRegistry {
// pub _class: HashMap<String, Vec<AppMimetypeEntry>>,
pub _extension: MimetypeRegistry,
}
pub const EMPTY_ARR: [AppMimetypeEntry; 0] = [];
impl AppMimetypeRegistry {
pub fn app_list_for_ext(&self, extension: &str) -> &[AppMimetypeEntry] {
match self._extension.get(extension) {
Some(s) => s.app_list(),
None => &EMPTY_ARR,
}
}
}
impl From<AppMimetypeRegistryCrude> for AppMimetypeRegistry {
fn from(crude: AppMimetypeRegistryCrude) -> Self {
let mut registry = MimetypeRegistry::new();
for (ext, app_list) in crude._extension {
let class = app_list.parent();
let mut combined_app_list: Vec<AppMimetypeEntry> = crude
._class
.get(class)
.map(|v| (*v).clone())
.unwrap_or_default();
combined_app_list.extend_from_slice(app_list.app_list());
let combined_app_list = AppList::new(class.to_string(), combined_app_list);
registry.insert(ext, combined_app_list);
}
Self {
_extension: registry,
}
}
}
impl TomlConfigFile for AppMimetypeRegistry {
fn get_config(file_name: &str) -> Self {
parse_to_config_file::<AppMimetypeRegistryCrude, AppMimetypeRegistry>(file_name)
.unwrap_or_default()
}
}
|