diff options
author | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2020-07-17 13:12:57 +0300 |
---|---|---|
committer | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2020-07-17 13:33:40 +0300 |
commit | 996abd323f5fa00c79f05c5e618ea79797b1644c (patch) | |
tree | 71af9b855c7a109509e36cecac84ecbf80c7a2d0 /src/conf.rs | |
parent | c6c2865a5416e6c5ea1e142e1a4140ab2e1f94e8 (diff) |
Add print setting action
Add experimental print setting action. The command is of the form:
print account_name listing.index_style
account_name is currently ignored.
The path, e.g. listing.index_style is split by "." and fed to
DotAddressable lookup trait method. The method checks the first segment
in the path if it matches any of the struct's fields, and then calls the
field's lookup method.
Diffstat (limited to 'src/conf.rs')
-rw-r--r-- | src/conf.rs | 85 |
1 files changed, 83 insertions, 2 deletions
diff --git a/src/conf.rs b/src/conf.rs index 8e149a99..21d316aa 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -249,7 +249,7 @@ pub struct FileSettings { pub log: LogSettings, } -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone, Default, Serialize)] pub struct AccountConf { pub(crate) account: AccountSettings, pub(crate) conf: FileAccount, @@ -272,7 +272,7 @@ impl AccountConf { } } -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone, Default, Serialize)] pub struct Settings { pub accounts: HashMap<String, AccountConf>, pub pager: PagerSettings, @@ -827,3 +827,84 @@ pub struct LogSettings { #[serde(default)] maximum_level: melib::LoggingLevel, } + +pub trait DotAddressable: Serialize { + fn lookup(&self, parent_field: &str, path: &[&str]) -> Result<String> { + if !path.is_empty() { + Err(MeliError::new(format!( + "{} has no fields, it is of type {}", + parent_field, + std::any::type_name::<Self>() + ))) + } else { + Ok(toml::to_string(self).map_err(|err| err.to_string())?) + } + } +} + +impl DotAddressable for bool {} + +impl DotAddressable for String {} +impl DotAddressable for IndexStyle {} +impl DotAddressable for u64 {} +impl DotAddressable for crate::terminal::Color {} +impl DotAddressable for crate::terminal::Attr {} +impl DotAddressable for usize {} +impl DotAddressable for Query {} +impl DotAddressable for melib::LoggingLevel {} +impl DotAddressable for PathBuf {} +impl DotAddressable for ToggleFlag {} +impl<T: DotAddressable> DotAddressable for Option<T> {} +impl<K: DotAddressable + std::cmp::Eq + std::hash::Hash, V: DotAddressable> DotAddressable + for HashMap<K, V> +{ +} +impl<K: DotAddressable + std::cmp::Eq + std::hash::Hash> DotAddressable for HashSet<K> {} + +impl DotAddressable for LogSettings { + fn lookup(&self, parent_field: &str, path: &[&str]) -> Result<String> { + match path.first() { + Some(field) => { + let tail = &path[1..]; + match *field { + "log_file" => self.log_file.lookup(field, tail), + "maximum_level" => self.maximum_level.lookup(field, tail), + + other => Err(MeliError::new(format!( + "{} has no field named {}", + parent_field, other + ))), + } + } + None => Ok(toml::to_string(self).map_err(|err| err.to_string())?), + } + } +} +impl DotAddressable for Settings { + fn lookup(&self, parent_field: &str, path: &[&str]) -> Result<String> { + match path.first() { + Some(field) => { + let tail = &path[1..]; + match *field { + "accounts" => Err(MeliError::new("unimplemented")), + "pager" => self.pager.lookup(field, tail), + "listing" => self.listing.lookup(field, tail), + "notifications" => Err(MeliError::new("unimplemented")), + "shortcuts" => Err(MeliError::new("unimplemented")), + "tags" => Err(MeliError::new("unimplemented")), + "composing" => Err(MeliError::new("unimplemented")), + "pgp" => Err(MeliError::new("unimplemented")), + "terminal" => self.terminal.lookup(field, tail), + "plugins" => Err(MeliError::new("unimplemented")), + "log" => self.log.lookup(field, tail), + + other => Err(MeliError::new(format!( + "{} has no field named {}", + parent_field, other + ))), + } + } + None => Ok(toml::to_string(self).map_err(|err| err.to_string())?), + } + } +} |