mirror of
https://seed.radicle.dev/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5.git
synced 2026-11-21 10:30:41 +08:00
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:
@@ -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())),
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
|
||||
@@ -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())),
|
||||
}
|
||||
|
||||
@@ -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())),
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user