item id arggroup #28

Merged
glitch4347 merged 1 commits from item-number-refactoring into main 3 months ago
  1. 109
      src/main.rs
  2. 2
      src/storage.rs

109
src/main.rs

@ -7,7 +7,7 @@ mod git;
use storage::{Storage, Item}; use storage::{Storage, Item};
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand, ArgGroup};
use rpassword; use rpassword;
use std::io::{self, Write}; use std::io::{self, Write};
@ -34,15 +34,7 @@ enum Commands {
List, List,
/// Show content of an item /// Show content of an item
Show { Show(ItemIdArgs),
// positional
#[arg(value_name="item_id", required=false)]
id: Option<String>,
// named
#[arg(short, long, value_name="item_number", required=false)]
number: Option<u32>
},
/// Adds new item with unique id to the storage /// Adds new item with unique id to the storage
Add { Add {
@ -51,21 +43,26 @@ enum Commands {
}, },
/// Edit item content /// Edit item content
Edit { Edit(ItemIdArgs),
// positional
#[arg(value_name="item_id", required=false)]
id: Option<String>,
// named /// Delete item
#[arg(short, long, value_name="item_number", required=false)] Delete(ItemIdArgs)
number: Option<u32>
},
Delete { }
#[arg(value_name="item_id")]
id: String
}
#[derive(Parser)]
#[command(group(
ArgGroup::new("item")
.required(true)
.args(&["id", "number"])
))]
struct ItemIdArgs {
/// Item id
#[arg(required=false)]
id: Option<String>,
#[arg(short='n', long, value_name="number")]
number: Option<u32>
} }
enum PROMPT { enum PROMPT {
@ -257,6 +254,13 @@ impl MPS {
Ok(()) Ok(())
} }
fn delete_by_number(&mut self, number: u32) -> io::Result<()> {
self.login()?;
let st = self.storage.as_mut().unwrap();
let id = st.get_id_by_number(number)?;
self.delete(&id)
}
} }
fn run_command() -> io::Result<()> { fn run_command() -> io::Result<()> {
@ -269,62 +273,37 @@ fn run_command() -> io::Result<()> {
let mut mps = MPS::new(); let mut mps = MPS::new();
mps.list()?; mps.list()?;
} }
Some(Commands::Show { id, number }) => { Some(Commands::Show(item)) => {
match (id, number) {
(None, None) => {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Must provide <item_id> or -n <item_number>"
));
}
(Some(item_id), None) => {
let mut mps = MPS::new(); let mut mps = MPS::new();
mps.show(item_id)?; if let Some(id) = &item.id {
}, mps.show(id)?;
(None, Some(item_number)) => { }
let mut mps = MPS::new(); if let Some(number) = &item.number {
mps.show_by_number(*item_number)?; mps.show_by_number(*number)?;
},
_ => {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Must provide <item_id> or -n <item_number> but not both"
))
} }
};
} }
Some(Commands::Add { id }) => { Some(Commands::Add { id }) => {
let mut mps = MPS::new(); let mut mps = MPS::new();
mps.add(id)?; mps.add(id)?;
} }
Some(Commands::Edit { id, number }) => { Some(Commands::Edit(item)) => {
match (id, number) {
(None, None) => {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Must provide <item_id> or -n <item_number>"
));
}
(Some(item_id), None) => {
let mut mps = MPS::new(); let mut mps = MPS::new();
mps.edit(item_id)?; if let Some(id) = &item.id {
}, mps.edit(id)?;
(None, Some(item_number)) => { }
let mut mps = MPS::new(); if let Some(number) = &item.number {
mps.edit_by_number(*item_number)?; mps.edit_by_number(*number)?;
},
_ => {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Must provide <item_id> or -n <item_number> but not both"
))
} }
};
} }
Some(Commands::Delete { id }) => { Some(Commands::Delete(item)) => {
let mut mps = MPS::new(); let mut mps = MPS::new();
if let Some(id) = &item.id {
mps.delete(id)? mps.delete(id)?
} }
if let Some(number) = &item.number {
mps.delete_by_number(*number)?;
}
}
None => { None => {
match Storage::check_installed() { match Storage::check_installed() {
Ok(()) => (), Ok(()) => (),

2
src/storage.rs

@ -197,7 +197,7 @@ impl Storage {
)); ));
} }
let ids = self.ids(); let ids = self.ids();
if number >= ids.len() { if number > ids.len() {
return Err(io::Error::new( return Err(io::Error::new(
io::ErrorKind::InvalidInput, io::ErrorKind::InvalidInput,
format!("There are only {} items, but asked with number {}", ids.len(), number) format!("There are only {} items, but asked with number {}", ids.len(), number)

Loading…
Cancel
Save