cli: add rid function for Id parsing

Adds helper for parsing the Id type in CLI arguments.

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
Fintan Halpenny 2023-02-16 13:21:09 +00:00 committed by Alexis Sellier
parent 5477a3615f
commit 2b5552546c
No known key found for this signature in database
5 changed files with 10 additions and 29 deletions

View File

@ -60,10 +60,7 @@ impl Args for Options {
}
}
Value(val) if id.is_none() => {
let val = val.to_string_lossy();
let val = Id::from_str(&val).context(format!("invalid id '{val}'"))?;
id = Some(val);
id = Some(term::args::rid(&val)?);
}
_ => return Err(anyhow::anyhow!(arg.unexpected())),
}

View File

@ -1,5 +1,4 @@
use std::ffi::OsString;
use std::str::FromStr;
use anyhow::{anyhow, Context as _};
@ -86,13 +85,7 @@ impl Args for Options {
did = Some(term::args::did(&val)?);
}
Some(OperationName::List) => {
// TODO: create args::project_id function
let val = val.to_string_lossy();
if let Ok(val) = Id::from_str(&val) {
id = Some(val);
} else {
return Err(anyhow!("invalid Project ID '{}'", val));
}
id = Some(term::args::rid(&val)?);
}
None => continue,
},

View File

@ -1,5 +1,4 @@
use std::ffi::OsString;
use std::str::FromStr;
use anyhow::{anyhow, Context as _};
@ -45,13 +44,7 @@ impl Args for Options {
return Err(Error::Help.into());
}
Value(val) if id.is_none() => {
let val = val.to_string_lossy();
if let Ok(val) = Id::from_str(&val) {
id = Some(val);
} else {
return Err(anyhow!("invalid ID '{}'", val));
}
id = Some(term::args::rid(&val)?);
}
_ => return Err(anyhow::anyhow!(arg.unexpected())),
}

View File

@ -1,6 +1,5 @@
use std::ffi::OsString;
use std::fs;
use std::str::FromStr;
use anyhow::anyhow;
@ -52,13 +51,7 @@ impl Args for Options {
return Err(Error::Help.into());
}
Value(val) if id.is_none() => {
let val = val.to_string_lossy();
if let Ok(val) = Id::from_str(&val) {
id = Some(val);
} else {
return Err(anyhow!("invalid ID '{}'", val));
}
id = Some(term::args::rid(&val)?);
}
_ => return Err(anyhow::anyhow!(arg.unexpected())),
}

View File

@ -3,7 +3,7 @@ use std::str::FromStr;
use anyhow::anyhow;
use radicle::crypto;
use radicle::prelude::Did;
use radicle::prelude::{Did, Id};
#[derive(thiserror::Error, Debug)]
pub enum Error {
@ -85,3 +85,8 @@ pub fn did(val: &OsString) -> anyhow::Result<Did> {
};
Ok(peer)
}
pub fn rid(val: &OsString) -> anyhow::Result<Id> {
let val = val.to_string_lossy();
Id::from_str(&val).map_err(|_| anyhow!("invalid repository ID '{}'", val))
}