use Type;
use SupportedType;
use failure::Error;
use std::collections::HashMap;
use serde::ser::Serialize;
use dirs;
use std::env;
pub type SettingsRaw = HashMap<String,Type>;
pub trait Format {
fn filename(&self) -> String;
fn folder(&self) -> String;
fn to_string<T>(&self,object:&T) -> Result<String,Error> where T : SupportedType + Serialize;
fn from_str<T>(&self,buffer:&str) -> Result<SettingsRaw,Error> where T : Format + Clone;
fn extension(&self) -> Option<String> {
None
}
fn local_filename(&self) -> Option<String> {
None
}
fn local_extension(&self) -> Option<String> {
None
}
fn get_path(&self) -> String {
match dirs::home_dir() {
None => "".to_string(),
Some(mut dir) => {
dir.push(format!("{}",self.folder()));
dir.display().to_string()
}
}
}
fn get_path_and_file(&self) -> String {
match dirs::home_dir() {
None => "".to_string(),
Some(mut dir) => {
dir.push(format!("{}/{}",self.folder(),self.get_filename()));
dir.display().to_string()
}
}
}
fn get_filename(&self) -> String {
if let Some(ext) = self.extension() {
return format!("{}.{}",self.filename(),ext);
} else {
return self.filename();
}
}
fn get_local_filename(&self) -> Option<String> {
match self.local_filename() {
None => None,
Some(localname) => {
if let Some(ext) = self.local_extension() {
Some(format!("{}.{}",localname,ext))
} else {
Some(localname)
}
}
}
}
fn get_local_path_and_filename(&self) -> String {
match env::current_dir() {
Err(_) => { panic!(); },
Ok(mut path) => {
if let Some(local_filename) = self.get_local_filename() {
path.push(local_filename);
} else {
path.push(self.get_filename());
}
return path.display().to_string();
}
}
}
}