From c3c5a9c11a7418db3c9e416403ecf23164ef020a Mon Sep 17 00:00:00 2001 From: Coin de Gamma Date: Sat, 28 Sep 2024 08:50:02 +0000 Subject: [PATCH] resolve item_id --- src/main.rs | 90 +++++++++++++++++++++++------------------------------ 1 file changed, 39 insertions(+), 51 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1f073eb..1f8f1e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -171,27 +171,15 @@ impl MPS { Ok(()) } + /// Show item. Id must exists fn show(&mut self, id: &String) -> io::Result<()> { self.login()?; let st = self.storage.as_ref().unwrap(); - if !st.contains(id) { - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - format!("Can`t find id: {}", id) - )); - } let item = st.get(id); editor::open_to_show(&item.content)?; Ok(()) } - fn show_by_number(&mut self, number: u32) -> io::Result<()> { - self.login()?; - let st = self.storage.as_ref().unwrap(); - let id = st.get_id_by_number(number)?; - self.show(&id) - } - fn add(&mut self, id: &String) -> io::Result<()> { self.login()?; let st = self.storage.as_mut().unwrap(); @@ -212,6 +200,7 @@ impl MPS { Ok(()) } + /// Edit item, id need not to exist fn edit(&mut self, id: &String) -> io::Result<()> { self.login()?; let st = self.storage.as_mut().unwrap(); @@ -233,36 +222,47 @@ impl MPS { Ok(()) } - fn edit_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.edit(&id) - } - + // Delete item by id, is must exist fn delete(&mut self, id: &String) -> io::Result<()> { self.login()?; let st = self.storage.as_mut().unwrap(); - if !st.contains(id) { - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - "No such item" - )); - } st.remove(id); st.dump()?; Ok(()) } - fn delete_by_number(&mut self, number: u32) -> io::Result<()> { + /// Resolve id by ItemArgs. + /// # Arguments + /// * `args` - arguments to parse + /// * `check` - check that id existing + fn item_id_by_item_id_args(&mut self, args: &ItemIdArgs, check: bool) -> io::Result { self.login()?; let st = self.storage.as_mut().unwrap(); - let id = st.get_id_by_number(number)?; - self.delete(&id) + let mut item_id: String = "NOT_INITIALIZED".to_string(); + if let Some(id) = &args.id { + item_id = id.clone(); + if check && !st.contains(&id) { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("No such item {}", id) + )); + } + } + if let Some(number) = &args.number { + item_id = st.get_id_by_number(*number)?; + // we can guarantee that id exists because we take it by id + } + if args.id.is_none() && args.number.is_none() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("Bag arguments") + )); + } + Ok(item_id.clone()) } - } + fn run_command() -> io::Result<()> { let cli = Cli::parse(); match &cli.command { @@ -273,36 +273,24 @@ fn run_command() -> io::Result<()> { let mut mps = MPS::new(); mps.list()?; } - Some(Commands::Show(item)) => { + Some(Commands::Show(args)) => { 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)?; - } + let id = mps.item_id_by_item_id_args(args, true)?; + mps.show(&id)?; } Some(Commands::Add { id }) => { let mut mps = MPS::new(); mps.add(id)?; } - Some(Commands::Edit(item)) => { + Some(Commands::Edit(args)) => { 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)?; - } + let id = mps.item_id_by_item_id_args(args, false)?; + mps.edit(&id)?; } - Some(Commands::Delete(item)) => { + Some(Commands::Delete(args)) => { let mut mps = MPS::new(); - if let Some(id) = &item.id { - mps.delete(id)? - } - if let Some(number) = &item.number { - mps.delete_by_number(*number)?; - } + let id = mps.item_id_by_item_id_args(args, true)?; + mps.delete(&id)?; } None => { match Storage::check_installed() {