diff --git a/src/dirs.rs b/src/dirs.rs new file mode 100644 index 0000000..10ef164 --- /dev/null +++ b/src/dirs.rs @@ -0,0 +1,30 @@ +use std; +use std::path::PathBuf; + +pub fn get_app_config_dir_create() -> Result { + let config_dir = get_app_config_dir()?; + + std::fs::create_dir_all(&config_dir) + .map_err(|e| format!("{}", e))?; + + Ok(config_dir) +} + +fn get_app_config_dir() -> Result { + let mut config_dir = get_xdg_config_dir()?; + + config_dir.push("nvim-gtk"); + + Ok(config_dir) +} + +fn get_xdg_config_dir() -> Result { + if let Ok(config_path) = std::env::var("XDG_CONFIG_HOME") { + return Ok(PathBuf::from(config_path)); + } + + let mut home_dir = std::env::home_dir() + .ok_or("Impossible to get your home dir!")?; + home_dir.push(".config"); + Ok(home_dir) +} diff --git a/src/main.rs b/src/main.rs index b120a03..c48713e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,12 +18,14 @@ extern crate log; extern crate env_logger; extern crate htmlescape; +extern crate serde; #[macro_use] extern crate serde_derive; extern crate toml; mod sys; +mod dirs; mod color; mod value; mod mode; diff --git a/src/plug_manager/store.rs b/src/plug_manager/store.rs index 56d7d81..8f56edc 100644 --- a/src/plug_manager/store.rs +++ b/src/plug_manager/store.rs @@ -1,9 +1,40 @@ -pub struct Store { - -} +use toml; + +use settings::SettingsLoader; + +pub struct Store {} impl Store { pub fn new() -> Self { - Store { } + Store {} + } +} + +#[derive(Serialize, Deserialize)] +struct Settings { + plugs: Vec, +} + +impl SettingsLoader for Settings { + const SETTINGS_FILE: &'static str = "plugs.toml"; + + fn empty() -> Self { + Settings { plugs: vec![] } + } + + fn from_str(s: &str) -> Result { + toml::from_str(&s).map_err(|e| format!("{}", e)) + } +} + +#[derive(Serialize, Deserialize)] +pub struct PlugInfo { + name: String, + url: String, +} + +impl PlugInfo { + pub fn new(name: String, url: String) -> Self { + PlugInfo { name, url } } } diff --git a/src/project.rs b/src/project.rs index 60fe38b..7bb838d 100644 --- a/src/project.rs +++ b/src/project.rs @@ -532,14 +532,9 @@ impl Entry { // ----- Store / Load settings // -use std::path::PathBuf; -use std::fs::File; -use std::io::prelude::*; -use std; +use settings::SettingsLoader; use toml; -const PROJECTS_SETTINGS_FILE: &str = "projects.toml"; - #[derive(Serialize, Deserialize)] struct ProjectSettings { projects: Vec, @@ -564,87 +559,21 @@ impl ProjectEntrySettings { } } -impl ProjectSettings { - fn new(projects: Vec) -> ProjectSettings { - ProjectSettings { projects } - } +impl SettingsLoader for ProjectSettings { + const SETTINGS_FILE: &'static str = "projects.toml"; fn empty() -> ProjectSettings { ProjectSettings { projects: vec![] } } - fn load_from_file(path: &Path) -> Result { - if path.exists() { - let mut file = File::open(path).map_err(|e| format!("{}", e))?; - let mut contents = String::new(); - file.read_to_string(&mut contents) - .map_err(|e| format!("{}", e))?; - toml::from_str(&contents).map_err(|e| format!("{}", e)) - } else { - Ok(ProjectSettings::empty()) - } - } - - fn load_err() -> Result { - let mut toml_path = get_app_config_dir_create()?; - toml_path.push(PROJECTS_SETTINGS_FILE); - ProjectSettings::load_from_file(&toml_path) - } - - fn load() -> ProjectSettings { - match ProjectSettings::load_err() { - Ok(settings) => settings, - Err(e) => { - println!("{}", e); - ProjectSettings::empty() - } - } - } - - fn save_err(&self) -> Result<(), String> { - let mut toml_path = get_app_config_dir_create()?; - toml_path.push(PROJECTS_SETTINGS_FILE); - let mut file = File::create(toml_path).map_err(|e| format!("{}", e))?; - - let contents = toml::to_vec(self).map_err(|e| format!("{}", e))?; - - file.write_all(&contents).map_err(|e| format!("{}", e))?; - - Ok(()) - } - - pub fn save(&self) { - match self.save_err() { - Ok(()) => (), - Err(e) => println!("{}", e), - } + fn from_str(s: &str) -> Result { + toml::from_str(&s).map_err(|e| format!("{}", e)) } } -fn get_app_config_dir_create() -> Result { - let config_dir = get_app_config_dir()?; - - std::fs::create_dir_all(&config_dir) - .map_err(|e| format!("{}", e))?; - - Ok(config_dir) -} - -fn get_app_config_dir() -> Result { - let mut config_dir = get_xdg_config_dir()?; - - config_dir.push("nvim-gtk"); - - Ok(config_dir) -} - -fn get_xdg_config_dir() -> Result { - if let Ok(config_path) = std::env::var("XDG_CONFIG_HOME") { - return Ok(PathBuf::from(config_path)); +impl ProjectSettings { + fn new(projects: Vec) -> ProjectSettings { + ProjectSettings { projects } } - - let mut home_dir = std::env::home_dir() - .ok_or("Impossible to get your home dir!")?; - home_dir.push(".config"); - Ok(home_dir) } + diff --git a/src/settings.rs b/src/settings.rs index 04d36df..edc0319 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -101,3 +101,69 @@ fn monospace_font_changed(mut shell: &mut Shell, state: &mut State) { state.update_font(&mut shell); } } + +use std::path::Path; +use std::fs::File; +use std::io::prelude::*; + +use toml; +use serde; + +use dirs; + +pub trait SettingsLoader: Sized + serde::Serialize { + const SETTINGS_FILE: &'static str; + + fn empty() -> Self; + + fn from_str(s: &str) -> Result; + + fn load() -> Self { + match load_err() { + Ok(settings) => settings, + Err(e) => { + println!("{}", e); + Self::empty() + } + } + } + + fn save(&self) { + match save_err(self) { + Ok(()) => (), + Err(e) => println!("{}", e), + } + } +} + +fn load_from_file(path: &Path) -> Result { + if path.exists() { + let mut file = File::open(path).map_err(|e| format!("{}", e))?; + let mut contents = String::new(); + file.read_to_string(&mut contents).map_err( + |e| format!("{}", e), + )?; + T::from_str(&contents) + } else { + Ok(T::empty()) + } +} + +fn load_err() -> Result { + let mut toml_path = dirs::get_app_config_dir_create()?; + toml_path.push(T::SETTINGS_FILE); + load_from_file(&toml_path) +} + + +fn save_err(sl: &T) -> Result<(), String> { + let mut toml_path = dirs::get_app_config_dir_create()?; + toml_path.push(T::SETTINGS_FILE); + let mut file = File::create(toml_path).map_err(|e| format!("{}", e))?; + + let contents = toml::to_vec::(sl).map_err(|e| format!("{}", e))?; + + file.write_all(&contents).map_err(|e| format!("{}", e))?; + + Ok(()) +}