Browse Source

basic show

fix-typo
Coin de Gamma 3 months ago
parent
commit
1e738bb907
  1. 49
      src/main.rs
  2. 5
      src/storage.rs

49
src/main.rs

@ -20,20 +20,25 @@ struct Cli {
#[derive(Subcommand)]
enum Commands {
/// Initialization of storage and config, use this in first time of usage of mps
Init,
/// Lists all ids stored in db
List,
/// Adds new item with unique id to the db
Add {
#[arg(value_name="item_id")]
id: String
},
/// Lists all ids stored in db
List
// TODO: show
/// Show content of item
Show {
#[arg(value_name="item_id")]
id: String
}
// TODO: edit
}
@ -94,6 +99,17 @@ fn init() -> io::Result<()> {
Ok(())
}
fn list() -> io:: Result<()> {
let passphrase = login()?;
let st = Storage::from_db(passphrase)?;
let mut vec: Vec<_> = st.items.iter().collect();
vec.sort();
for item in &vec {
println!("{}", item.id);
}
Ok(())
}
fn add(id: &String) -> io::Result<()> {
let passphrase = login()?;
let mut st = Storage::from_db(passphrase)?;
@ -102,6 +118,7 @@ fn add(id: &String) -> io::Result<()> {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("Dublicate item id: {}. Item id's must be unique.", id)
// TODO: ask to edit [Y/n]
))
}
@ -112,14 +129,19 @@ fn add(id: &String) -> io::Result<()> {
Ok(())
}
fn list() -> io:: Result<()> {
let passphrase = login()?;
let st = Storage::from_db(passphrase)?;
let mut vec: Vec<_> = st.items.iter().collect();
vec.sort();
for item in &vec {
println!("{}", item.id);
fn show(id: &String) -> io::Result<()> {
let passphrase = login()?;
let mut st = Storage::from_db(passphrase)?;
if !st.contains(id) {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("Can`t find id: {}", id)
));
}
let item = st.get(id);
println!("content:");
println!("{}", item.content);
Ok(())
}
@ -132,6 +154,9 @@ fn run_command() -> io::Result<()> {
Some(Commands::Add{ id }) => {
add(id)?;
}
Some(Commands::Show{ id }) => {
show(id)?;
}
Some(Commands::List) => {
list()?;
}

5
src/storage.rs

@ -179,6 +179,11 @@ impl Storage {
self.items.contains(&item)
}
pub fn get(&self, id: &String) -> &Item {
let item = Item::from_empty(id.clone());
self.items.get(&item).unwrap()
}
pub fn dump(&self) -> io::Result<()> {
let mut file = fs::OpenOptions::new()
.write(true)

Loading…
Cancel
Save