1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
extern crate crypto; use crypto::digest::Digest; use crypto::sha2::Sha512; use std::error::Error; use std::fs; use std::io; use std::io::prelude::*; use std::path::PathBuf; pub mod validation; pub trait Writeable { fn name(&self) -> &String; fn path(&self) -> &String; fn save(&self) -> Result<(), String> { let mut filename = Sha512::new(); filename.input_str(self.name().as_str()); let mut path = PathBuf::from(".tight"); path.push(self.path()); path.push(filename.result_str()); let mut file = match fs::OpenOptions::new() .write(true) .create_new(true) .open(path.as_path()) { Ok(file) => file, Err(e) => return Err(String::from(e.description())), }; match file.write(self.name().as_bytes()) { Ok(_) => Ok(()), Err(e) => Err(String::from(e.description())), } } } pub fn init() -> Result<(), io::Error> { fs::create_dir_all(".tight/expense")?; fs::create_dir_all(".tight/income")?; fs::create_dir_all(".tight/merchant")?; fs::create_dir_all(".tight/source")?; fs::create_dir_all(".tight/category")?; fs::create_dir_all(".tight/transfermethod")?; Ok(()) } #[derive(Debug)] pub struct Merchant { pub name: String, path: String, } impl Merchant { pub fn new(name: &str) -> Merchant { Merchant { name: String::from(name), path: String::from("merchant"), } } } impl Writeable for Merchant { fn name(&self) -> &String { &self.name } fn path(&self) -> &String { &self.path } }