General code for load/store settings

This commit is contained in:
daa84 2017-10-19 17:04:58 +03:00
parent fbe25e1a1c
commit a286d39b48
5 changed files with 142 additions and 84 deletions

30
src/dirs.rs Normal file
View File

@ -0,0 +1,30 @@
use std;
use std::path::PathBuf;
pub fn get_app_config_dir_create() -> Result<PathBuf, String> {
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<PathBuf, String> {
let mut config_dir = get_xdg_config_dir()?;
config_dir.push("nvim-gtk");
Ok(config_dir)
}
fn get_xdg_config_dir() -> Result<PathBuf, String> {
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)
}

View File

@ -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;

View File

@ -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<PlugInfo>,
}
impl SettingsLoader for Settings {
const SETTINGS_FILE: &'static str = "plugs.toml";
fn empty() -> Self {
Settings { plugs: vec![] }
}
fn from_str(s: &str) -> Result<Self, String> {
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 }
}
}

View File

@ -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<ProjectEntrySettings>,
@ -564,87 +559,21 @@ impl ProjectEntrySettings {
}
}
impl ProjectSettings {
fn new(projects: Vec<ProjectEntrySettings>) -> 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<ProjectSettings, String> {
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<ProjectSettings, String> {
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<Self, String> {
toml::from_str(&s).map_err(|e| format!("{}", e))
}
}
fn get_app_config_dir_create() -> Result<PathBuf, String> {
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<PathBuf, String> {
let mut config_dir = get_xdg_config_dir()?;
config_dir.push("nvim-gtk");
Ok(config_dir)
}
fn get_xdg_config_dir() -> Result<PathBuf, String> {
if let Ok(config_path) = std::env::var("XDG_CONFIG_HOME") {
return Ok(PathBuf::from(config_path));
impl ProjectSettings {
fn new(projects: Vec<ProjectEntrySettings>) -> 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)
}

View File

@ -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<Self, String>;
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<T: SettingsLoader>(path: &Path) -> Result<T, String> {
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<T: SettingsLoader>() -> Result<T, String> {
let mut toml_path = dirs::get_app_config_dir_create()?;
toml_path.push(T::SETTINGS_FILE);
load_from_file(&toml_path)
}
fn save_err<T: SettingsLoader>(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::<T>(sl).map_err(|e| format!("{}", e))?;
file.write_all(&contents).map_err(|e| format!("{}", e))?;
Ok(())
}