extern crate dirs;
extern crate serde;
extern crate toml;
use error;
use git;
use std::fs;
use std::fs::File;
use std::io::{Read, Write};
use std::path;
#[derive(Serialize, Deserialize, Debug)]
pub struct SnippetLocation {
pub local: String,
pub ext: String,
pub git: Option<bool>,
}
impl SnippetLocation {
pub fn default(home: &String) -> SnippetLocation {
return SnippetLocation {
local: String::from(home.to_owned() + "/.snippets"),
ext: "md".to_string(),
git: None,
};
}
pub fn create_if_not_exists(&self) -> Result<(), error::Error> {
let path = path::Path::new(&self.local);
if !path.exists() {
fs::create_dir_all(&path)?;
}
Ok(())
}
}
#[derive(Serialize, Deserialize)]
pub struct Project {
pub locations: Vec<SnippetLocation>,
}
pub enum ProjectOperation {
Exist(Project),
NotExist(Project),
}
impl Project {
pub fn write(&self, folder: &path::Path) -> Result<(), error::Error> {
let to_write = toml::to_string(self).expect("Cannot serialize project");
let full_path = path::Path::new(&folder).join(".x.toml");
let mut f = File::create(&full_path).expect("Cannot create project file");
f.write_all(to_write.as_bytes())
.expect("Cannot write to project file");
Ok(())
}
pub fn default_project() -> Result<ProjectOperation, error::Error> {
let home = String::from(
dirs::home_dir()
.expect("Cannot find the home dir")
.to_str()
.unwrap(),
);
let format = format!("{}/.x.toml", &home);
let path = path::Path::new(&format);
let mut project_operation = if path.exists() {
let mut f = File::open(path).expect("Found project file but can't read it.");
let mut buffer = String::new();
f.read_to_string(&mut buffer)?;
let project: Project =
toml::from_str(&buffer).expect("Cannot deserialize project file");
ProjectOperation::Exist(project)
} else {
ProjectOperation::NotExist(Project {
locations: vec![SnippetLocation::default(&home)],
})
};
if let ProjectOperation::Exist(ref mut project) = project_operation {
git::determine_git_status(project);
}
Ok(project_operation)
}
}