Browse Source

basic show

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

43
src/main.rs

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

5
src/storage.rs

@ -179,6 +179,11 @@ impl Storage {
self.items.contains(&item) 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<()> { pub fn dump(&self) -> io::Result<()> {
let mut file = fs::OpenOptions::new() let mut file = fs::OpenOptions::new()
.write(true) .write(true)

Loading…
Cancel
Save