|
|
|
@ -3,6 +3,8 @@ mod db;
|
|
|
|
|
|
|
|
|
|
use db::{DB, Item}; |
|
|
|
|
|
|
|
|
|
use once_cell::sync::Lazy; |
|
|
|
|
|
|
|
|
|
use clap::{Parser, Subcommand}; |
|
|
|
|
|
|
|
|
|
use std::fs; |
|
|
|
@ -10,8 +12,10 @@ use std::io::{self, Write};
|
|
|
|
|
use std::path::Path; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const STORAGE_FOLDER: &str = "storage"; |
|
|
|
|
const STORAGE_PATH: &str = "storage/db.mps"; // TODO: concat from STORAGE_FOLDER
|
|
|
|
|
static STORAGE_FOLDER: &str = "storage"; |
|
|
|
|
static STORAGE_PATH: Lazy<String> = Lazy::new(|| {
|
|
|
|
|
format!("{}/db.mps", STORAGE_FOLDER) |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Parser)] |
|
|
|
@ -51,7 +55,7 @@ fn init() -> io::Result<()> {
|
|
|
|
|
fs::create_dir(STORAGE_FOLDER)?; |
|
|
|
|
println!("Storage folder created"); |
|
|
|
|
|
|
|
|
|
fs::File::create(STORAGE_PATH)?; |
|
|
|
|
fs::File::create(&*STORAGE_PATH)?; |
|
|
|
|
println!("Storage db created."); |
|
|
|
|
println!("Initialization complete."); |
|
|
|
|
println!(""); |
|
|
|
@ -61,7 +65,7 @@ fn init() -> io::Result<()> {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn add(id: &String) -> io::Result<()> { |
|
|
|
|
let mut db = DB::new(STORAGE_PATH)?; |
|
|
|
|
let mut db = DB::new(&*STORAGE_PATH)?; |
|
|
|
|
if db.contains(id) { |
|
|
|
|
// TODO: ask to edit existing in outer function which invoked this one
|
|
|
|
|
return Err(io::Error::new( |
|
|
|
@ -71,13 +75,13 @@ fn add(id: &String) -> io::Result<()> {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
db.items.insert(Item::from(id.clone())); |
|
|
|
|
db.dump(STORAGE_PATH)?; |
|
|
|
|
db.dump(&*STORAGE_PATH)?; |
|
|
|
|
|
|
|
|
|
Ok(()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn list() -> io:: Result<()> { |
|
|
|
|
let db = DB::new(STORAGE_PATH)?; |
|
|
|
|
let db = DB::new(&STORAGE_PATH)?; |
|
|
|
|
let mut vec: Vec<_> = db.items.iter().collect(); |
|
|
|
|
vec.sort(); |
|
|
|
|
for item in &vec { |
|
|
|
|