From c5f6cdd48505a3f0d4dd19c22fd855ff2beb368e Mon Sep 17 00:00:00 2001 From: Coin de Gamma Date: Sat, 28 Sep 2024 08:05:48 +0000 Subject: [PATCH] item id arggroup --- src/main.rs | 121 ++++++++++++++++++++----------------------------- src/storage.rs | 2 +- 2 files changed, 51 insertions(+), 72 deletions(-) diff --git a/src/main.rs b/src/main.rs index d01b97e..1f073eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ mod git; use storage::{Storage, Item}; -use clap::{Parser, Subcommand}; +use clap::{Parser, Subcommand, ArgGroup}; use rpassword; use std::io::{self, Write}; @@ -34,15 +34,7 @@ enum Commands { List, /// Show content of an item - Show { - // positional - #[arg(value_name="item_id", required=false)] - id: Option, - - // named - #[arg(short, long, value_name="item_number", required=false)] - number: Option - }, + Show(ItemIdArgs), /// Adds new item with unique id to the storage Add { @@ -51,23 +43,28 @@ enum Commands { }, /// Edit item content - Edit { - // positional - #[arg(value_name="item_id", required=false)] - id: Option, - - // named - #[arg(short, long, value_name="item_number", required=false)] - number: Option - }, + Edit(ItemIdArgs), - Delete { - #[arg(value_name="item_id")] - id: String - } + /// Delete item + Delete(ItemIdArgs) } +#[derive(Parser)] +#[command(group( + ArgGroup::new("item") + .required(true) + .args(&["id", "number"]) +))] +struct ItemIdArgs { + /// Item id + #[arg(required=false)] + id: Option, + + #[arg(short='n', long, value_name="number")] + number: Option +} + enum PROMPT { YES, NO @@ -257,6 +254,13 @@ impl MPS { 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<()> { @@ -269,61 +273,36 @@ fn run_command() -> io::Result<()> { let mut mps = MPS::new(); mps.list()?; } - Some(Commands::Show { id, number }) => { - match (id, number) { - (None, None) => { - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - "Must provide or -n " - )); - } - (Some(item_id), None) => { - let mut mps = MPS::new(); - mps.show(item_id)?; - }, - (None, Some(item_number)) => { - let mut mps = MPS::new(); - mps.show_by_number(*item_number)?; - }, - _ => { - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - "Must provide or -n but not both" - )) - } - }; + Some(Commands::Show(item)) => { + let mut mps = MPS::new(); + if let Some(id) = &item.id { + mps.show(id)?; + } + if let Some(number) = &item.number { + mps.show_by_number(*number)?; + } } Some(Commands::Add { id }) => { let mut mps = MPS::new(); mps.add(id)?; } - Some(Commands::Edit { id, number }) => { - match (id, number) { - (None, None) => { - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - "Must provide or -n " - )); - } - (Some(item_id), None) => { - let mut mps = MPS::new(); - mps.edit(item_id)?; - }, - (None, Some(item_number)) => { - let mut mps = MPS::new(); - mps.edit_by_number(*item_number)?; - }, - _ => { - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - "Must provide or -n but not both" - )) - } - }; + Some(Commands::Edit(item)) => { + let mut mps = MPS::new(); + if let Some(id) = &item.id { + mps.edit(id)?; + } + if let Some(number) = &item.number { + mps.edit_by_number(*number)?; + } } - Some(Commands::Delete { id }) => { + Some(Commands::Delete(item)) => { let mut mps = MPS::new(); - mps.delete(id)? + if let Some(id) = &item.id { + mps.delete(id)? + } + if let Some(number) = &item.number { + mps.delete_by_number(*number)?; + } } None => { match Storage::check_installed() { diff --git a/src/storage.rs b/src/storage.rs index db6e4b8..984a6a2 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -197,7 +197,7 @@ impl Storage { )); } let ids = self.ids(); - if number >= ids.len() { + if number > ids.len() { return Err(io::Error::new( io::ErrorKind::InvalidInput, format!("There are only {} items, but asked with number {}", ids.len(), number) -- 2.34.1