2018-08-19 13:12:48 +03:00
|
|
|
/*
|
|
|
|
* meli - configuration module.
|
|
|
|
*
|
|
|
|
* Copyright 2017 Manos Pitsidianakis
|
|
|
|
*
|
|
|
|
* This file is part of meli.
|
|
|
|
*
|
|
|
|
* meli is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* meli is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with meli. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-03-14 12:19:25 +02:00
|
|
|
extern crate bincode;
|
2018-08-19 13:12:48 +03:00
|
|
|
extern crate serde;
|
2019-08-25 14:14:54 +03:00
|
|
|
extern crate toml;
|
2018-08-19 13:12:48 +03:00
|
|
|
extern crate xdg;
|
2019-02-15 09:06:42 +02:00
|
|
|
|
2019-09-27 12:48:48 +03:00
|
|
|
pub mod composing;
|
2019-03-02 21:40:57 +02:00
|
|
|
pub mod notifications;
|
2019-03-14 12:19:25 +02:00
|
|
|
pub mod pager;
|
2019-09-26 12:10:36 +03:00
|
|
|
pub mod pgp;
|
2019-03-03 14:24:15 +02:00
|
|
|
pub mod shortcuts;
|
2018-08-19 13:12:48 +03:00
|
|
|
|
2019-02-10 18:26:49 +02:00
|
|
|
pub mod accounts;
|
|
|
|
pub use self::accounts::Account;
|
2019-09-27 12:48:48 +03:00
|
|
|
pub use self::composing::*;
|
2019-09-26 12:10:36 +03:00
|
|
|
pub use self::pgp::*;
|
2019-03-14 12:19:25 +02:00
|
|
|
pub use self::shortcuts::*;
|
2019-02-10 18:26:49 +02:00
|
|
|
|
2019-04-06 00:30:06 +03:00
|
|
|
use self::default_vals::*;
|
2019-03-14 12:19:25 +02:00
|
|
|
use self::notifications::NotificationsSettings;
|
2019-06-18 21:13:58 +03:00
|
|
|
use crate::pager::PagerSettings;
|
2019-08-23 21:58:41 +03:00
|
|
|
use melib::backends::SpecialUseMailbox;
|
2018-08-19 13:12:48 +03:00
|
|
|
use melib::conf::AccountSettings;
|
2018-08-30 15:54:30 +03:00
|
|
|
use melib::error::*;
|
2018-08-19 13:12:48 +03:00
|
|
|
|
2019-08-25 21:44:19 +03:00
|
|
|
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
|
|
|
|
|
2018-08-19 13:12:48 +03:00
|
|
|
use std::collections::HashMap;
|
2018-09-05 08:22:10 +03:00
|
|
|
use std::env;
|
2019-08-25 14:14:54 +03:00
|
|
|
use std::fs::{File, OpenOptions};
|
|
|
|
use std::io::{self, BufRead, Read, Write};
|
2018-09-05 08:22:10 +03:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2019-04-06 00:30:06 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! split_command {
|
|
|
|
($cmd:expr) => {{
|
|
|
|
$cmd.split_whitespace().collect::<Vec<&str>>()
|
|
|
|
}};
|
2018-09-05 08:22:10 +03:00
|
|
|
}
|
|
|
|
|
2019-05-13 21:08:18 +03:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum ToggleFlag {
|
|
|
|
Unset,
|
|
|
|
InternalVal(bool),
|
|
|
|
False,
|
|
|
|
True,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ToggleFlag {
|
|
|
|
fn default() -> Self {
|
|
|
|
ToggleFlag::Unset
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToggleFlag {
|
|
|
|
pub fn is_unset(&self) -> bool {
|
|
|
|
ToggleFlag::Unset == *self
|
|
|
|
}
|
|
|
|
pub fn is_internal(&self) -> bool {
|
|
|
|
if let ToggleFlag::InternalVal(_) = *self {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn is_false(&self) -> bool {
|
|
|
|
ToggleFlag::False == *self || ToggleFlag::InternalVal(false) == *self
|
|
|
|
}
|
|
|
|
pub fn is_true(&self) -> bool {
|
|
|
|
ToggleFlag::True == *self || ToggleFlag::InternalVal(true) == *self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 21:38:37 +03:00
|
|
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct MailUIConf {
|
|
|
|
pub pager: Option<PagerSettings>,
|
|
|
|
pub notifications: Option<NotificationsSettings>,
|
|
|
|
pub shortcuts: Option<Shortcuts>,
|
2019-09-27 12:48:48 +03:00
|
|
|
pub composing: Option<ComposingSettings>,
|
2019-09-09 21:38:37 +03:00
|
|
|
pub identity: Option<String>,
|
2019-09-16 14:09:08 +03:00
|
|
|
pub index_style: Option<IndexStyle>,
|
2019-09-09 21:38:37 +03:00
|
|
|
}
|
|
|
|
|
2019-08-25 21:44:19 +03:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2018-09-05 08:22:10 +03:00
|
|
|
pub struct FolderConf {
|
2019-09-09 21:38:37 +03:00
|
|
|
pub rename: Option<String>,
|
2018-09-05 08:22:10 +03:00
|
|
|
#[serde(default = "true_val")]
|
2019-09-09 21:38:37 +03:00
|
|
|
pub autoload: bool,
|
2019-05-13 21:08:18 +03:00
|
|
|
#[serde(deserialize_with = "toggleflag_de", default)]
|
2019-09-09 21:38:37 +03:00
|
|
|
pub subscribe: ToggleFlag,
|
2019-08-23 21:32:32 +03:00
|
|
|
#[serde(deserialize_with = "toggleflag_de", default)]
|
2019-09-09 21:38:37 +03:00
|
|
|
pub ignore: ToggleFlag,
|
2019-08-23 21:58:41 +03:00
|
|
|
#[serde(default = "none")]
|
2019-09-09 21:38:37 +03:00
|
|
|
pub usage: Option<SpecialUseMailbox>,
|
|
|
|
#[serde(flatten)]
|
|
|
|
pub conf_override: MailUIConf,
|
2019-05-13 21:08:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for FolderConf {
|
|
|
|
fn default() -> Self {
|
|
|
|
FolderConf {
|
|
|
|
rename: None,
|
|
|
|
autoload: true,
|
2019-07-18 20:26:49 +03:00
|
|
|
subscribe: ToggleFlag::Unset,
|
2019-09-09 21:38:37 +03:00
|
|
|
ignore: ToggleFlag::Unset,
|
2019-08-23 21:58:41 +03:00
|
|
|
usage: None,
|
2019-09-09 21:38:37 +03:00
|
|
|
conf_override: MailUIConf::default(),
|
2019-05-13 21:08:18 +03:00
|
|
|
}
|
|
|
|
}
|
2018-09-05 08:22:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FolderConf {
|
|
|
|
pub fn rename(&self) -> Option<&str> {
|
2019-05-13 22:05:00 +03:00
|
|
|
self.rename.as_ref().map(String::as_str)
|
2018-09-05 08:22:10 +03:00
|
|
|
}
|
|
|
|
}
|
2018-08-19 13:12:48 +03:00
|
|
|
|
2019-08-25 21:44:19 +03:00
|
|
|
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
2018-08-19 13:12:48 +03:00
|
|
|
pub struct FileAccount {
|
|
|
|
root_folder: String,
|
|
|
|
format: String,
|
2018-08-30 15:54:30 +03:00
|
|
|
identity: String,
|
2019-06-28 19:34:40 +03:00
|
|
|
#[serde(flatten)]
|
|
|
|
pub extra: HashMap<String, String>,
|
2019-04-06 00:30:06 +03:00
|
|
|
|
|
|
|
#[serde(default = "none")]
|
2018-08-30 15:54:30 +03:00
|
|
|
display_name: Option<String>,
|
2019-09-16 14:09:08 +03:00
|
|
|
index_style: IndexStyle,
|
2019-04-06 00:30:06 +03:00
|
|
|
|
|
|
|
/// A command to pipe html output before displaying it in a pager
|
|
|
|
/// Default: None
|
|
|
|
#[serde(default = "none", deserialize_with = "non_empty_string")]
|
|
|
|
html_filter: Option<String>,
|
|
|
|
|
2019-07-18 20:16:51 +03:00
|
|
|
#[serde(default = "false_val")]
|
|
|
|
read_only: bool,
|
2019-08-23 21:32:32 +03:00
|
|
|
subscribed_folders: Vec<String>,
|
2018-09-05 08:22:10 +03:00
|
|
|
folders: Option<HashMap<String, FolderConf>>,
|
2018-08-19 13:12:48 +03:00
|
|
|
}
|
|
|
|
|
2018-08-30 15:54:30 +03:00
|
|
|
impl From<FileAccount> for AccountConf {
|
|
|
|
fn from(x: FileAccount) -> Self {
|
|
|
|
let format = x.format.to_lowercase();
|
|
|
|
let root_folder = x.root_folder.clone();
|
|
|
|
let identity = x.identity.clone();
|
|
|
|
let display_name = x.display_name.clone();
|
|
|
|
|
2019-08-23 21:32:32 +03:00
|
|
|
let mut acc = AccountSettings {
|
2018-08-30 15:54:30 +03:00
|
|
|
name: String::new(),
|
|
|
|
root_folder,
|
|
|
|
format,
|
|
|
|
identity,
|
2019-07-18 20:16:51 +03:00
|
|
|
read_only: x.read_only,
|
2018-08-30 15:54:30 +03:00
|
|
|
display_name,
|
2019-08-23 21:32:32 +03:00
|
|
|
subscribed_folders: x.subscribed_folders.clone(),
|
2019-06-28 19:34:40 +03:00
|
|
|
extra: x.extra.clone(),
|
2018-08-30 15:54:30 +03:00
|
|
|
};
|
|
|
|
|
2019-08-23 21:32:32 +03:00
|
|
|
let root_path = PathBuf::from(acc.root_folder.as_str());
|
|
|
|
let root_tmp = root_path
|
|
|
|
.components()
|
|
|
|
.last()
|
|
|
|
.unwrap()
|
|
|
|
.as_os_str()
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.to_string();
|
|
|
|
if !acc.subscribed_folders.contains(&root_tmp) {
|
|
|
|
acc.subscribed_folders.push(root_tmp);
|
|
|
|
}
|
|
|
|
let mut folder_confs = x.folders.clone().unwrap_or_else(Default::default);
|
|
|
|
for s in &x.subscribed_folders {
|
|
|
|
if !folder_confs.contains_key(s) {
|
|
|
|
folder_confs.insert(
|
|
|
|
s.to_string(),
|
|
|
|
FolderConf {
|
|
|
|
subscribe: ToggleFlag::True,
|
|
|
|
..FolderConf::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
if !folder_confs[s].subscribe.is_unset() {
|
|
|
|
eprintln!("Configuration error: folder `{}` cannot both have `subscribe` flag set and be in the `subscribed_folders` array", s);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
folder_confs.get_mut(s).unwrap().subscribe = ToggleFlag::True;
|
|
|
|
}
|
2019-08-23 21:58:41 +03:00
|
|
|
|
|
|
|
if folder_confs[s].usage.is_none() {
|
2019-09-09 21:38:37 +03:00
|
|
|
let name = s
|
|
|
|
.split(if s.contains('/') { '/' } else { '.' })
|
|
|
|
.last()
|
|
|
|
.unwrap_or("");
|
2019-08-23 21:58:41 +03:00
|
|
|
folder_confs.get_mut(s).unwrap().usage = if name.eq_ignore_ascii_case("inbox") {
|
|
|
|
Some(SpecialUseMailbox::Inbox)
|
|
|
|
} else if name.eq_ignore_ascii_case("archive") {
|
|
|
|
Some(SpecialUseMailbox::Archive)
|
|
|
|
} else if name.eq_ignore_ascii_case("drafts") {
|
|
|
|
Some(SpecialUseMailbox::Drafts)
|
|
|
|
} else if name.eq_ignore_ascii_case("junk") {
|
|
|
|
Some(SpecialUseMailbox::Junk)
|
|
|
|
} else if name.eq_ignore_ascii_case("spam") {
|
|
|
|
Some(SpecialUseMailbox::Junk)
|
|
|
|
} else if name.eq_ignore_ascii_case("sent") {
|
|
|
|
Some(SpecialUseMailbox::Sent)
|
|
|
|
} else if name.eq_ignore_ascii_case("trash") {
|
|
|
|
Some(SpecialUseMailbox::Trash)
|
|
|
|
} else {
|
|
|
|
Some(SpecialUseMailbox::Normal)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if folder_confs[s].ignore.is_unset() {
|
|
|
|
use SpecialUseMailbox::*;
|
|
|
|
if [Junk, Sent, Trash].contains(&folder_confs[s].usage.as_ref().unwrap()) {
|
|
|
|
folder_confs.get_mut(s).unwrap().ignore = ToggleFlag::InternalVal(true);
|
|
|
|
}
|
|
|
|
}
|
2019-08-23 21:32:32 +03:00
|
|
|
}
|
2019-05-13 21:08:18 +03:00
|
|
|
|
2018-08-30 15:54:30 +03:00
|
|
|
AccountConf {
|
|
|
|
account: acc,
|
|
|
|
conf: x,
|
2019-05-13 21:08:18 +03:00
|
|
|
folder_confs,
|
2018-08-30 15:54:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-19 13:12:48 +03:00
|
|
|
impl FileAccount {
|
2018-09-05 08:22:10 +03:00
|
|
|
pub fn folders(&self) -> Option<&HashMap<String, FolderConf>> {
|
2018-08-19 14:54:32 +03:00
|
|
|
self.folders.as_ref()
|
|
|
|
}
|
2019-09-16 14:09:08 +03:00
|
|
|
|
2018-08-19 13:12:48 +03:00
|
|
|
pub fn folder(&self) -> &str {
|
|
|
|
&self.root_folder
|
|
|
|
}
|
2019-09-16 14:09:08 +03:00
|
|
|
|
|
|
|
pub fn index_style(&self) -> IndexStyle {
|
|
|
|
self.index_style
|
2019-04-10 18:57:09 +03:00
|
|
|
}
|
2019-09-16 14:09:08 +03:00
|
|
|
|
2019-04-06 00:30:06 +03:00
|
|
|
pub fn html_filter(&self) -> Option<&str> {
|
2019-05-13 22:05:00 +03:00
|
|
|
self.html_filter.as_ref().map(String::as_str)
|
2019-04-06 00:30:06 +03:00
|
|
|
}
|
2018-08-19 13:12:48 +03:00
|
|
|
}
|
|
|
|
|
2019-08-25 21:44:19 +03:00
|
|
|
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
2018-08-19 13:12:48 +03:00
|
|
|
struct FileSettings {
|
|
|
|
accounts: HashMap<String, FileAccount>,
|
2019-06-10 18:29:49 +03:00
|
|
|
#[serde(default)]
|
2018-08-19 13:12:48 +03:00
|
|
|
pager: PagerSettings,
|
2019-06-10 18:29:49 +03:00
|
|
|
#[serde(default)]
|
2019-03-02 21:40:57 +02:00
|
|
|
notifications: NotificationsSettings,
|
2019-06-10 18:29:49 +03:00
|
|
|
#[serde(default)]
|
2019-03-04 10:15:11 +02:00
|
|
|
shortcuts: Shortcuts,
|
2019-09-27 12:48:48 +03:00
|
|
|
composing: ComposingSettings,
|
2019-09-26 12:10:36 +03:00
|
|
|
#[serde(default)]
|
|
|
|
pgp: PGPSettings,
|
2018-08-19 13:12:48 +03:00
|
|
|
}
|
|
|
|
|
2018-08-19 13:30:43 +03:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2018-08-19 14:08:20 +03:00
|
|
|
pub struct AccountConf {
|
2019-09-09 21:38:37 +03:00
|
|
|
pub(crate) account: AccountSettings,
|
|
|
|
pub(crate) conf: FileAccount,
|
|
|
|
pub(crate) folder_confs: HashMap<String, FolderConf>,
|
2018-08-19 13:30:43 +03:00
|
|
|
}
|
|
|
|
|
2018-08-19 14:08:20 +03:00
|
|
|
impl AccountConf {
|
2018-08-19 13:30:43 +03:00
|
|
|
pub fn account(&self) -> &AccountSettings {
|
|
|
|
&self.account
|
|
|
|
}
|
|
|
|
pub fn conf(&self) -> &FileAccount {
|
|
|
|
&self.conf
|
|
|
|
}
|
2018-08-19 14:08:20 +03:00
|
|
|
pub fn conf_mut(&mut self) -> &mut FileAccount {
|
|
|
|
&mut self.conf
|
|
|
|
}
|
2018-08-19 13:30:43 +03:00
|
|
|
}
|
|
|
|
|
2018-08-19 13:12:48 +03:00
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
pub struct Settings {
|
2018-08-19 14:08:20 +03:00
|
|
|
pub accounts: HashMap<String, AccountConf>,
|
2018-08-19 13:12:48 +03:00
|
|
|
pub pager: PagerSettings,
|
2019-03-02 21:40:57 +02:00
|
|
|
pub notifications: NotificationsSettings,
|
2019-03-04 10:15:11 +02:00
|
|
|
pub shortcuts: Shortcuts,
|
2019-09-27 12:48:48 +03:00
|
|
|
pub composing: ComposingSettings,
|
2019-09-26 12:10:36 +03:00
|
|
|
pub pgp: PGPSettings,
|
2018-08-19 13:12:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FileSettings {
|
2018-08-30 15:54:30 +03:00
|
|
|
pub fn new() -> Result<FileSettings> {
|
2018-09-05 08:22:10 +03:00
|
|
|
let config_path = match env::var("MELI_CONFIG") {
|
|
|
|
Ok(path) => PathBuf::from(path),
|
|
|
|
Err(_) => {
|
|
|
|
let xdg_dirs = xdg::BaseDirectories::with_prefix("meli").unwrap();
|
|
|
|
xdg_dirs
|
|
|
|
.place_config_file("config")
|
|
|
|
.expect("cannot create configuration directory")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if !config_path.exists() {
|
2019-06-10 18:29:49 +03:00
|
|
|
println!(
|
|
|
|
"No configuration found. Would you like to generate one in {}? [Y/n]",
|
2018-09-05 08:22:10 +03:00
|
|
|
config_path.display()
|
|
|
|
);
|
2019-06-10 18:29:49 +03:00
|
|
|
let mut buffer = String::new();
|
|
|
|
let stdin = io::stdin();
|
|
|
|
let mut handle = stdin.lock();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
buffer.clear();
|
|
|
|
handle
|
|
|
|
.read_line(&mut buffer)
|
|
|
|
.expect("Could not read from stdin.");
|
|
|
|
|
|
|
|
match buffer.trim() {
|
|
|
|
"Y" | "y" | "yes" | "YES" | "Yes" => {
|
|
|
|
let mut file = OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.create_new(true)
|
|
|
|
.open(config_path.as_path())
|
|
|
|
.expect("Could not create config file.");
|
2019-06-18 21:13:58 +03:00
|
|
|
file.write_all(include_bytes!("../../sample-config"))
|
2019-06-10 18:29:49 +03:00
|
|
|
.expect("Could not write to config file.");
|
|
|
|
println!("Written config to {}", config_path.display());
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
"n" | "N" | "no" | "No" | "NO" => {
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
println!(
|
|
|
|
"No configuration found. Would you like to generate one in {}? [Y/n]",
|
|
|
|
config_path.display()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-05 08:22:10 +03:00
|
|
|
}
|
2019-08-25 14:14:54 +03:00
|
|
|
|
|
|
|
let mut file = File::open(config_path.to_str().unwrap())?;
|
|
|
|
let mut contents = String::new();
|
|
|
|
file.read_to_string(&mut contents)?;
|
|
|
|
let s = toml::from_str(&contents);
|
|
|
|
if let Err(e) = s {
|
|
|
|
eprintln!("Config file contains errors: {}", e.to_string());
|
2019-06-10 18:29:49 +03:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2018-08-19 13:12:48 +03:00
|
|
|
|
2019-08-25 14:14:54 +03:00
|
|
|
Ok(s.unwrap())
|
2018-08-19 13:12:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Settings {
|
|
|
|
pub fn new() -> Settings {
|
2019-06-10 18:29:49 +03:00
|
|
|
let fs = FileSettings::new().unwrap_or_else(|e| {
|
2019-07-17 20:41:43 +03:00
|
|
|
eprintln!("Configuration error: {}", e);
|
2019-06-10 18:29:49 +03:00
|
|
|
std::process::exit(1);
|
|
|
|
});
|
2018-08-19 14:08:20 +03:00
|
|
|
let mut s: HashMap<String, AccountConf> = HashMap::new();
|
2018-08-19 13:12:48 +03:00
|
|
|
|
|
|
|
for (id, x) in fs.accounts {
|
2018-08-30 15:54:30 +03:00
|
|
|
let mut ac = AccountConf::from(x);
|
|
|
|
ac.account.set_name(id.clone());
|
|
|
|
|
|
|
|
s.insert(id, ac);
|
2018-08-19 13:12:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Settings {
|
|
|
|
accounts: s,
|
|
|
|
pager: fs.pager,
|
2019-03-02 21:40:57 +02:00
|
|
|
notifications: fs.notifications,
|
2019-03-03 14:24:15 +02:00
|
|
|
shortcuts: fs.shortcuts,
|
2019-09-27 12:48:48 +03:00
|
|
|
composing: fs.composing,
|
2019-09-26 12:10:36 +03:00
|
|
|
pgp: fs.pgp,
|
2018-08-19 13:12:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-18 23:14:06 +02:00
|
|
|
|
2019-09-09 21:38:37 +03:00
|
|
|
#[derive(Copy, Debug, Clone, Hash, PartialEq)]
|
2019-02-18 23:14:06 +02:00
|
|
|
pub enum IndexStyle {
|
|
|
|
Plain,
|
|
|
|
Threaded,
|
|
|
|
Compact,
|
2019-09-14 12:43:19 +03:00
|
|
|
Conversations,
|
2019-02-18 23:14:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for IndexStyle {
|
|
|
|
fn default() -> Self {
|
|
|
|
IndexStyle::Compact
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-06 00:30:06 +03:00
|
|
|
fn non_empty_string<'de, D>(deserializer: D) -> std::result::Result<Option<String>, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let s = <String>::deserialize(deserializer)?;
|
|
|
|
if s.is_empty() {
|
|
|
|
Ok(None)
|
|
|
|
} else {
|
|
|
|
Ok(Some(s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-13 21:08:18 +03:00
|
|
|
fn toggleflag_de<'de, D>(deserializer: D) -> std::result::Result<ToggleFlag, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let s = <bool>::deserialize(deserializer);
|
|
|
|
Ok(match s {
|
|
|
|
Err(_) => ToggleFlag::Unset,
|
|
|
|
Ok(true) => ToggleFlag::True,
|
|
|
|
Ok(false) => ToggleFlag::False,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-25 21:44:19 +03:00
|
|
|
impl Serialize for ToggleFlag {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
match self {
|
|
|
|
ToggleFlag::Unset | ToggleFlag::InternalVal(_) => serializer.serialize_none(),
|
|
|
|
ToggleFlag::False => serializer.serialize_bool(false),
|
|
|
|
ToggleFlag::True => serializer.serialize_bool(true),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-06 00:30:06 +03:00
|
|
|
/*
|
|
|
|
* Deserialize default functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
mod default_vals {
|
2019-06-18 21:13:58 +03:00
|
|
|
pub(in crate::conf) fn false_val() -> bool {
|
2019-07-18 20:16:51 +03:00
|
|
|
false
|
2019-04-06 00:30:06 +03:00
|
|
|
}
|
|
|
|
|
2019-06-18 21:13:58 +03:00
|
|
|
pub(in crate::conf) fn true_val() -> bool {
|
2019-04-06 00:30:06 +03:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2019-06-18 21:13:58 +03:00
|
|
|
pub(in crate::conf) fn zero_val() -> usize {
|
2019-04-06 00:30:06 +03:00
|
|
|
0
|
|
|
|
}
|
|
|
|
|
2019-06-18 21:13:58 +03:00
|
|
|
pub(in crate::conf) fn eighty_percent() -> usize {
|
2019-04-06 00:30:06 +03:00
|
|
|
80
|
|
|
|
}
|
|
|
|
|
2019-08-23 21:32:32 +03:00
|
|
|
pub(in crate::conf) fn none<T>() -> Option<T> {
|
2019-04-06 00:30:06 +03:00
|
|
|
None
|
|
|
|
}
|
2019-09-15 23:36:30 +03:00
|
|
|
|
|
|
|
pub(in crate::conf) fn internal_value_false() -> super::ToggleFlag {
|
|
|
|
super::ToggleFlag::InternalVal(false)
|
|
|
|
}
|
2019-04-06 00:30:06 +03:00
|
|
|
}
|
2019-09-09 21:38:37 +03:00
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for IndexStyle {
|
|
|
|
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let s = <String>::deserialize(deserializer)?;
|
|
|
|
match s.as_str() {
|
|
|
|
"Plain" | "plain" => Ok(IndexStyle::Plain),
|
|
|
|
"Threaded" | "threaded" => Ok(IndexStyle::Threaded),
|
|
|
|
"Compact" | "compact" => Ok(IndexStyle::Compact),
|
2019-09-14 12:43:19 +03:00
|
|
|
"Conversations" | "conversations" => Ok(IndexStyle::Conversations),
|
|
|
|
_ => Err(de::Error::custom("invalid `index_style` value")),
|
2019-09-09 21:38:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for IndexStyle {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
match self {
|
|
|
|
IndexStyle::Plain => serializer.serialize_str("plain"),
|
|
|
|
IndexStyle::Threaded => serializer.serialize_str("threaded"),
|
|
|
|
IndexStyle::Compact => serializer.serialize_str("compact"),
|
2019-09-14 12:43:19 +03:00
|
|
|
IndexStyle::Conversations => serializer.serialize_str("conversations"),
|
2019-09-09 21:38:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|