Browse Source

item id arggroup

item-number-refactoring
Coin de Gamma 3 months ago
parent
commit
c5f6cdd485
  1. 121
      src/main.rs
  2. 2
      src/storage.rs

121
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
#[arg(short, long, value_name="item_number", required=false)]
number: Option<u32>
},
Delete { /// Delete item
#[arg(value_name="item_id")] Delete(ItemIdArgs)
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,61 +273,36 @@ 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) { let mut mps = MPS::new();
(None, None) => { if let Some(id) = &item.id {
return Err(io::Error::new( mps.show(id)?;
io::ErrorKind::InvalidInput, }
"Must provide <item_id> or -n <item_number>" if let Some(number) = &item.number {
)); mps.show_by_number(*number)?;
} }
(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 <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) { let mut mps = MPS::new();
(None, None) => { if let Some(id) = &item.id {
return Err(io::Error::new( mps.edit(id)?;
io::ErrorKind::InvalidInput, }
"Must provide <item_id> or -n <item_number>" if let Some(number) = &item.number {
)); mps.edit_by_number(*number)?;
} }
(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 <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();
mps.delete(id)? if let Some(id) = &item.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() {

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