2017-09-01 15:24:32 +03:00
|
|
|
/*
|
|
|
|
* meli - configuration module.
|
|
|
|
*
|
|
|
|
* Copyright 2017 Manos Pitsidianakis
|
2017-09-07 23:00:08 +03:00
|
|
|
*
|
2017-09-01 15:24:32 +03:00
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern crate config;
|
2018-07-11 18:58:57 +03:00
|
|
|
extern crate serde;
|
2018-07-27 21:37:56 +03:00
|
|
|
extern crate xdg;
|
2018-07-13 18:38:57 +03:00
|
|
|
pub mod pager;
|
|
|
|
|
|
|
|
use pager::PagerSettings;
|
|
|
|
|
2018-07-27 21:37:56 +03:00
|
|
|
use std::collections::HashMap;
|
2018-07-11 23:12:25 +03:00
|
|
|
|
2017-09-01 15:24:32 +03:00
|
|
|
#[derive(Debug, Deserialize)]
|
2018-08-11 18:00:21 +03:00
|
|
|
pub struct FileAccount {
|
|
|
|
root_folder: String,
|
2017-09-01 15:24:32 +03:00
|
|
|
format: String,
|
|
|
|
sent_folder: String,
|
2017-09-16 15:05:28 +03:00
|
|
|
threaded: bool,
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
|
|
|
|
2018-08-11 18:00:21 +03:00
|
|
|
impl FileAccount {
|
|
|
|
pub fn folder(&self) -> &str {
|
|
|
|
&self.root_folder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-14 18:03:43 +03:00
|
|
|
#[derive(Debug, Deserialize)]
|
2017-09-01 15:24:32 +03:00
|
|
|
struct FileSettings {
|
|
|
|
accounts: HashMap<String, FileAccount>,
|
2018-07-13 18:38:57 +03:00
|
|
|
pager: PagerSettings,
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
|
|
|
|
2017-09-16 15:05:28 +03:00
|
|
|
#[derive(Debug, Clone)]
|
2017-09-14 18:08:14 +03:00
|
|
|
pub struct AccountSettings {
|
2018-07-11 23:12:25 +03:00
|
|
|
name: String,
|
2018-08-11 18:00:21 +03:00
|
|
|
root_folder: String,
|
2017-09-30 20:02:22 +03:00
|
|
|
format: String,
|
2017-09-01 15:24:32 +03:00
|
|
|
pub sent_folder: String,
|
2018-07-17 17:16:16 +03:00
|
|
|
pub threaded: bool,
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
2017-09-30 20:02:22 +03:00
|
|
|
|
|
|
|
impl AccountSettings {
|
2018-07-20 12:44:04 +03:00
|
|
|
pub fn format(&self) -> &str {
|
2017-09-30 20:02:22 +03:00
|
|
|
&self.format
|
|
|
|
}
|
2018-07-20 12:44:04 +03:00
|
|
|
pub fn name(&self) -> &str {
|
2018-07-11 23:12:25 +03:00
|
|
|
&self.name
|
|
|
|
}
|
2018-08-11 18:00:21 +03:00
|
|
|
pub fn root_folder(&self) -> &str {
|
|
|
|
&self.root_folder
|
|
|
|
}
|
2017-09-30 20:02:22 +03:00
|
|
|
}
|
|
|
|
|
2018-07-22 14:05:12 +03:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2017-09-01 15:24:32 +03:00
|
|
|
pub struct Settings {
|
2017-09-16 15:05:28 +03:00
|
|
|
pub accounts: HashMap<String, AccountSettings>,
|
2018-07-13 18:38:57 +03:00
|
|
|
pub pager: PagerSettings,
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
use self::config::{Config, File, FileFormat};
|
|
|
|
impl FileSettings {
|
|
|
|
pub fn new() -> FileSettings {
|
|
|
|
let xdg_dirs = xdg::BaseDirectories::with_prefix("meli").unwrap();
|
2017-09-16 15:05:28 +03:00
|
|
|
let config_path = xdg_dirs
|
|
|
|
.place_config_file("config")
|
|
|
|
.expect("cannot create configuration directory");
|
2017-09-01 15:24:32 +03:00
|
|
|
//let setts = Config::default().merge(File::new(config_path.to_str().unwrap_or_default(), config::FileFormat::Toml)).unwrap();
|
|
|
|
let mut s = Config::new();
|
|
|
|
let s = s.merge(File::new(config_path.to_str().unwrap(), FileFormat::Toml));
|
|
|
|
|
2018-08-03 13:46:08 +03:00
|
|
|
// No point in returning without a config file.
|
|
|
|
// TODO: Error and exit instead of panic.
|
2018-07-27 21:37:56 +03:00
|
|
|
s.unwrap().deserialize().unwrap()
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Settings {
|
|
|
|
pub fn new() -> Settings {
|
|
|
|
let fs = FileSettings::new();
|
2017-09-14 18:08:14 +03:00
|
|
|
let mut s: HashMap<String, AccountSettings> = HashMap::new();
|
2017-09-07 23:00:08 +03:00
|
|
|
|
2017-09-01 15:24:32 +03:00
|
|
|
for (id, x) in fs.accounts {
|
2018-08-11 18:00:21 +03:00
|
|
|
let format = x.format.to_lowercase();
|
|
|
|
let sent_folder = x.sent_folder;
|
|
|
|
let threaded = x.threaded;
|
|
|
|
let root_folder = x.root_folder;
|
|
|
|
|
|
|
|
let acc = AccountSettings {
|
|
|
|
name: id.clone(),
|
|
|
|
root_folder,
|
|
|
|
format,
|
|
|
|
sent_folder,
|
|
|
|
threaded,
|
2017-09-01 15:24:32 +03:00
|
|
|
};
|
2018-08-11 18:00:21 +03:00
|
|
|
|
|
|
|
s.insert(id, acc);
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:37:56 +03:00
|
|
|
Settings {
|
|
|
|
accounts: s,
|
|
|
|
pager: fs.pager,
|
|
|
|
}
|
2017-09-01 15:24:32 +03:00
|
|
|
}
|
|
|
|
}
|