diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2022-09-11 09:50:09 -0400 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2022-09-11 09:51:01 -0400 |
commit | f117ddaccb996942774d9efee9c5807fe7272ded (patch) | |
tree | 21a0958d9502a149064ee99a2827c49d3d7294e2 /src/config | |
parent | 1908ff94b7e5f981a4254ca697056a299b2e6b51 (diff) |
add mimetype support via file command
Diffstat (limited to 'src/config')
-rw-r--r-- | src/config/mimetype/entry.rs | 33 | ||||
-rw-r--r-- | src/config/mimetype/list.rs | 66 | ||||
-rw-r--r-- | src/config/mimetype/mod.rs | 6 | ||||
-rw-r--r-- | src/config/mimetype/registry.rs | 80 |
4 files changed, 128 insertions, 57 deletions
diff --git a/src/config/mimetype/entry.rs b/src/config/mimetype/entry.rs index c6c0307..8a40d47 100644 --- a/src/config/mimetype/entry.rs +++ b/src/config/mimetype/entry.rs @@ -3,32 +3,7 @@ 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 { +pub struct ProgramEntry { #[serde(rename = "command")] _command: String, #[serde(default, rename = "args")] @@ -41,7 +16,7 @@ pub struct AppMimetypeEntry { _confirm_exit: bool, } -impl AppMimetypeEntry { +impl ProgramEntry { pub fn new(command: String) -> Self { Self { _command: command, @@ -114,7 +89,7 @@ impl AppMimetypeEntry { } } -impl std::default::Default for AppMimetypeEntry { +impl std::default::Default for ProgramEntry { fn default() -> Self { Self { _command: "".to_string(), @@ -126,7 +101,7 @@ impl std::default::Default for AppMimetypeEntry { } } -impl std::fmt::Display for AppMimetypeEntry { +impl std::fmt::Display for ProgramEntry { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str(self.get_command()).unwrap(); self.get_args() diff --git a/src/config/mimetype/list.rs b/src/config/mimetype/list.rs new file mode 100644 index 0000000..67373e9 --- /dev/null +++ b/src/config/mimetype/list.rs @@ -0,0 +1,66 @@ +use serde_derive::Deserialize; +use std::collections::HashMap; + +use super::ProgramEntry; + +#[derive(Clone, Debug, Deserialize)] +pub struct ExtensionAppListRaw { + #[serde(default, rename = "inherit")] + pub inherit_class: String, + #[serde(default)] + pub app_list: Vec<ProgramEntry>, +} + +impl ExtensionAppListRaw { + pub fn parent(&self) -> &str { + self.inherit_class.as_str() + } + + pub fn app_list(&self) -> &[ProgramEntry] { + self.app_list.as_slice() + } +} + +pub type ExtensionAppList = Vec<ProgramEntry>; + +#[derive(Clone, Debug, Deserialize)] +pub struct MimetypeAppListRaw { + #[serde(default, rename = "inherit")] + pub inherit_class: String, + #[serde(default)] + pub app_list: Vec<ProgramEntry>, + #[serde(default)] + pub subtype: Option<HashMap<String, ExtensionAppListRaw>>, +} + +impl MimetypeAppListRaw { + pub fn parent(&self) -> &str { + self.inherit_class.as_str() + } + + pub fn app_list(&self) -> &[ProgramEntry] { + self.app_list.as_slice() + } +} + +#[derive(Clone, Debug)] +pub struct MimetypeAppList { + _app_list: Vec<ProgramEntry>, + _subtypes: HashMap<String, ExtensionAppList>, +} + +impl MimetypeAppList { + pub fn new(_app_list: Vec<ProgramEntry>, _subtypes: HashMap<String, ExtensionAppList>) -> Self { + Self { + _app_list, + _subtypes, + } + } + pub fn app_list(&self) -> &[ProgramEntry] { + self._app_list.as_slice() + } + + pub fn subtypes(&self) -> &HashMap<String, ExtensionAppList> { + &self._subtypes + } +} diff --git a/src/config/mimetype/mod.rs b/src/config/mimetype/mod.rs index 3ab8beb..ced4807 100644 --- a/src/config/mimetype/mod.rs +++ b/src/config/mimetype/mod.rs @@ -1,5 +1,7 @@ mod entry; +mod list; mod registry; -pub use self::entry::{AppList, AppMimetypeEntry}; -pub use self::registry::AppMimetypeRegistry; +pub use self::entry::*; +pub use self::list::*; +pub use self::registry::*; diff --git a/src/config/mimetype/registry.rs b/src/config/mimetype/registry.rs index 73c20ac..452bb93 100644 --- a/src/config/mimetype/registry.rs +++ b/src/config/mimetype/registry.rs @@ -3,60 +3,88 @@ use std::collections::HashMap; use crate::config::{parse_to_config_file, TomlConfigFile}; -use super::{AppList, AppMimetypeEntry}; +use super::{ + ExtensionAppList, ExtensionAppListRaw, MimetypeAppList, MimetypeAppListRaw, ProgramEntry, +}; -pub type MimetypeRegistry = HashMap<String, AppList>; +pub type ExtensionRegistryRaw = HashMap<String, ExtensionAppListRaw>; +pub type MimetypeRegistryRaw = HashMap<String, MimetypeAppListRaw>; + +pub type ExtensionRegistry = HashMap<String, ExtensionAppList>; +pub type MimetypeRegistry = HashMap<String, MimetypeAppList>; #[derive(Debug, Deserialize)] -pub struct AppMimetypeRegistryCrude { +pub struct AppProgramRegistryRaw { #[serde(default, rename = "class")] - pub _class: HashMap<String, Vec<AppMimetypeEntry>>, + pub _class: HashMap<String, Vec<ProgramEntry>>, #[serde(default, rename = "extension")] - pub _extension: MimetypeRegistry, + pub _extension: ExtensionRegistryRaw, + #[serde(default, rename = "mimetype")] + pub _mimetype: MimetypeRegistryRaw, } #[derive(Debug, Default)] -pub struct AppMimetypeRegistry { - // pub _class: HashMap<String, Vec<AppMimetypeEntry>>, - pub _extension: MimetypeRegistry, +pub struct AppProgramRegistry { + // pub _class: HashMap<String, Vec<ProgramEntry>>, + pub _extension: ExtensionRegistry, + pub _mimetype: MimetypeRegistry, } -pub const EMPTY_ARR: [AppMimetypeEntry; 0] = []; +impl AppProgramRegistry { + pub fn app_list_for_ext(&self, extension: &str) -> Option<&ExtensionAppList> { + self._extension.get(extension) + } -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, - } + pub fn app_list_for_mimetype(&self, mimetype: &str) -> Option<&MimetypeAppList> { + self._mimetype.get(mimetype) } } -impl From<AppMimetypeRegistryCrude> for AppMimetypeRegistry { - fn from(crude: AppMimetypeRegistryCrude) -> Self { - let mut registry = MimetypeRegistry::new(); - - for (ext, app_list) in crude._extension { +impl From<AppProgramRegistryRaw> for AppProgramRegistry { + fn from(raw: AppProgramRegistryRaw) -> Self { + let mut extension = ExtensionRegistry::new(); + for (ext, app_list) in raw._extension { let class = app_list.parent(); - let mut combined_app_list: Vec<AppMimetypeEntry> = crude + let mut combined_app_list: ExtensionAppList = raw ._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); + + extension.insert(ext, combined_app_list); + } + + let mut mimetype = MimetypeRegistry::new(); + for (ttype, data) in raw._mimetype { + let class = data.parent(); + let mut combined_app_list: ExtensionAppList = raw + ._class + .get(class) + .map(|v| (*v).clone()) + .unwrap_or_default(); + combined_app_list.extend_from_slice(data.app_list()); + + let subtypes = data + .subtype + .unwrap_or_else(|| HashMap::new()) + .into_iter() + .map(|(k, v)| (k, v.app_list)) + .collect(); + let app_list = MimetypeAppList::new(combined_app_list, subtypes); + mimetype.insert(ttype, app_list); } Self { - _extension: registry, + _extension: extension, + _mimetype: mimetype, } } } -impl TomlConfigFile for AppMimetypeRegistry { +impl TomlConfigFile for AppProgramRegistry { fn get_config(file_name: &str) -> Self { - parse_to_config_file::<AppMimetypeRegistryCrude, AppMimetypeRegistry>(file_name) + parse_to_config_file::<AppProgramRegistryRaw, AppProgramRegistry>(file_name) .unwrap_or_default() } } |