|
|
@ -12,9 +12,9 @@ use std::fmt; |
|
|
|
use std::cmp::{PartialEq, Ordering}; |
|
|
|
use std::cmp::{PartialEq, Ordering}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static STORAGE_FOLDER: String = "storage"; |
|
|
|
static STORAGE_FOLDER: Lazy<String> = Lazy::new(|| "storage".to_string() ); |
|
|
|
static STORAGE_PATH: Lazy<String> = Lazy::new(|| {
|
|
|
|
static STORAGE_PATH: Lazy<String> = Lazy::new(|| {
|
|
|
|
format!("{}/db.mps", STORAGE_FOLDER) |
|
|
|
format!("{}/db.mps", &*STORAGE_FOLDER) |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -93,16 +93,15 @@ impl Encoder { |
|
|
|
|
|
|
|
|
|
|
|
pub struct DB { |
|
|
|
pub struct DB { |
|
|
|
pub items: HashSet::<Item>, |
|
|
|
pub items: HashSet::<Item>, |
|
|
|
path: String, |
|
|
|
|
|
|
|
encoder: Encoder |
|
|
|
encoder: Encoder |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl DB { |
|
|
|
impl DB { |
|
|
|
// TODO: make path as String too
|
|
|
|
// TODO: make path as String too
|
|
|
|
pub fn new(path: &str, password: String) -> io::Result<DB> { |
|
|
|
pub fn new(password: String) -> io::Result<DB> { |
|
|
|
let encoder = Encoder::from(password); |
|
|
|
let encoder = Encoder::from(password); |
|
|
|
// TODO: throw error is password is incorrect
|
|
|
|
// TODO: throw error is password is incorrect
|
|
|
|
let file = fs::File::open(path)?; |
|
|
|
let file = fs::File::open(&*STORAGE_PATH)?; |
|
|
|
let reader = io::BufReader::new(file); |
|
|
|
let reader = io::BufReader::new(file); |
|
|
|
let mut items = HashSet::<Item>::new(); |
|
|
|
let mut items = HashSet::<Item>::new(); |
|
|
|
let mut id: Option<String> = None; |
|
|
|
let mut id: Option<String> = None; |
|
|
@ -124,14 +123,14 @@ impl DB { |
|
|
|
} |
|
|
|
} |
|
|
|
let result = DB { |
|
|
|
let result = DB { |
|
|
|
items: items, |
|
|
|
items: items, |
|
|
|
path: String::from(path), |
|
|
|
|
|
|
|
encoder: encoder |
|
|
|
encoder: encoder |
|
|
|
}; |
|
|
|
}; |
|
|
|
Ok(result) |
|
|
|
Ok(result) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn init(passphrase: String) -> io::Result<()> { |
|
|
|
pub fn init(_passphrase: String) -> io::Result<()> { |
|
|
|
fs::create_dir(STORAGE_FOLDER)?; |
|
|
|
// TODO: use pasphrase
|
|
|
|
|
|
|
|
fs::create_dir(&*STORAGE_FOLDER)?; |
|
|
|
println!("Storage folder created"); |
|
|
|
println!("Storage folder created"); |
|
|
|
//let mut db = DB::init(&*STORAGE_PATH, pass)?;
|
|
|
|
//let mut db = DB::init(&*STORAGE_PATH, pass)?;
|
|
|
|
|
|
|
|
|
|
|
@ -139,13 +138,20 @@ impl DB { |
|
|
|
println!("Storage db created."); |
|
|
|
println!("Storage db created."); |
|
|
|
println!("Initialization complete."); |
|
|
|
println!("Initialization complete."); |
|
|
|
println!(""); |
|
|
|
println!(""); |
|
|
|
println!("Now it's required to add folder `{}` under git manually.", STORAGE_FOLDER); |
|
|
|
println!("Now it's required to add folder `{}` under git manually.", &*STORAGE_FOLDER); |
|
|
|
println!("Don't worry it's going to be encrypted."); |
|
|
|
println!("Don't worry it's going to be encrypted."); |
|
|
|
|
|
|
|
Ok(()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn print_init_hint() { |
|
|
|
|
|
|
|
println!("mps can work only when storage inited."); |
|
|
|
|
|
|
|
println!("Hint: you can restore your storage if you have it already:"); |
|
|
|
|
|
|
|
println!(" git clone <your_storage_git_url> {}", &*STORAGE_FOLDER); |
|
|
|
|
|
|
|
println!("to init manually your storage and config") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn is_inited() -> bool { |
|
|
|
pub fn is_inited() -> bool { |
|
|
|
let path = Path::new(STORAGE_FOLDER); |
|
|
|
let path = Path::new(&*STORAGE_FOLDER); |
|
|
|
return path.exists(); |
|
|
|
return path.exists(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -158,7 +164,7 @@ impl DB { |
|
|
|
let mut file = fs::OpenOptions::new() |
|
|
|
let mut file = fs::OpenOptions::new() |
|
|
|
.write(true) |
|
|
|
.write(true) |
|
|
|
.append(false) |
|
|
|
.append(false) |
|
|
|
.open(&self.path)?; |
|
|
|
.open(&*STORAGE_PATH)?; |
|
|
|
|
|
|
|
|
|
|
|
for item in self.items.iter() { |
|
|
|
for item in self.items.iter() { |
|
|
|
writeln!(file, "{}", item.id)?; |
|
|
|
writeln!(file, "{}", item.id)?; |
|
|
|