From a75db6a695a0404dbcc2afa83d0ea6326176d737 Mon Sep 17 00:00:00 2001 From: Fintan Halpenny Date: Wed, 29 Oct 2025 13:01:44 +0000 Subject: [PATCH] cli/terminal: remove argument helpers These helper functions were for parsing arguments passed via lexopt. Since `clap` is now the parser for these types, these functions are no longer needed. --- crates/radicle-cli/src/terminal.rs | 2 +- crates/radicle-cli/src/terminal/args.rs | 169 ------------------------ 2 files changed, 1 insertion(+), 170 deletions(-) diff --git a/crates/radicle-cli/src/terminal.rs b/crates/radicle-cli/src/terminal.rs index f0d78d7c..c43b0464 100644 --- a/crates/radicle-cli/src/terminal.rs +++ b/crates/radicle-cli/src/terminal.rs @@ -1,6 +1,6 @@ pub mod args; -pub use args::{Args, Error}; +pub use args::Error; pub mod format; pub mod io; pub use io::signer; diff --git a/crates/radicle-cli/src/terminal/args.rs b/crates/radicle-cli/src/terminal/args.rs index a33f0fe3..e486582d 100644 --- a/crates/radicle-cli/src/terminal/args.rs +++ b/crates/radicle-cli/src/terminal/args.rs @@ -1,21 +1,9 @@ -use std::ffi::OsString; -use std::net::SocketAddr; -use std::str::FromStr; -use std::time; - -use anyhow::anyhow; use clap::builder::TypedValueParser; use thiserror::Error; -use radicle::cob::{self, issue, patch}; -use radicle::crypto; -use radicle::git::{fmt::RefString, Oid}; use radicle::node::policy::Scope; -use radicle::node::{Address, Alias}; use radicle::prelude::{Did, NodeId, RepoId}; -use crate::git::Rev; - #[derive(thiserror::Error, Debug)] pub enum Error { /// If this error is returned from argument parsing, help is displayed. @@ -35,163 +23,6 @@ pub enum Error { }, } -pub trait Args: Sized { - fn from_env() -> anyhow::Result { - let args: Vec<_> = std::env::args_os().skip(1).collect(); - - match Self::from_args(args) { - Ok((opts, unparsed)) => { - self::finish(unparsed)?; - - Ok(opts) - } - Err(err) => Err(err), - } - } - - fn from_args(args: Vec) -> anyhow::Result<(Self, Vec)>; -} - -pub fn parse_value(flag: &str, value: OsString) -> anyhow::Result -where - ::Err: std::error::Error, -{ - value - .into_string() - .map_err(|_| anyhow!("the value specified for '--{}' is not valid UTF-8", flag))? - .parse() - .map_err(|e| anyhow!("invalid value specified for '--{}' ({})", flag, e)) -} - -pub fn format(arg: lexopt::Arg) -> OsString { - match arg { - lexopt::Arg::Long(flag) => format!("--{flag}").into(), - lexopt::Arg::Short(flag) => format!("-{flag}").into(), - lexopt::Arg::Value(val) => val, - } -} - -pub fn finish(unparsed: Vec) -> anyhow::Result<()> { - if let Some(arg) = unparsed.first() { - anyhow::bail!("unexpected argument `{}`", arg.to_string_lossy()) - } - Ok(()) -} - -pub fn refstring(flag: &str, value: OsString) -> anyhow::Result { - RefString::try_from( - value - .into_string() - .map_err(|_| anyhow!("the value specified for '--{}' is not valid UTF-8", flag))?, - ) - .map_err(|_| { - anyhow!( - "the value specified for '--{}' is not a valid ref string", - flag - ) - }) -} - -pub fn did(val: &OsString) -> anyhow::Result { - let val = val.to_string_lossy(); - let Ok(peer) = Did::from_str(&val) else { - if crypto::PublicKey::from_str(&val).is_ok() { - return Err(anyhow!("expected DID, did you mean 'did:key:{val}'?")); - } else { - return Err(anyhow!("invalid DID '{}', expected 'did:key'", val)); - } - }; - Ok(peer) -} - -pub fn nid(val: &OsString) -> anyhow::Result { - let val = val.to_string_lossy(); - NodeId::from_str(&val).map_err(|_| anyhow!("invalid Node ID '{}'", val)) -} - -pub fn rid(val: &OsString) -> anyhow::Result { - let val = val.to_string_lossy(); - RepoId::from_str(&val).map_err(|_| anyhow!("invalid Repository ID '{}'", val)) -} - -pub fn pubkey(val: &OsString) -> anyhow::Result { - let Ok(did) = did(val) else { - let nid = nid(val)?; - return Ok(nid); - }; - Ok(did.as_key().to_owned()) -} - -pub fn socket_addr(val: &OsString) -> anyhow::Result { - let val = val.to_string_lossy(); - SocketAddr::from_str(&val).map_err(|_| anyhow!("invalid socket address '{}'", val)) -} - -pub fn addr(val: &OsString) -> anyhow::Result
{ - let val = val.to_string_lossy(); - Address::from_str(&val).map_err(|_| anyhow!("invalid address '{}'", val)) -} - -pub fn number(val: &OsString) -> anyhow::Result { - let val = val.to_string_lossy(); - usize::from_str(&val).map_err(|_| anyhow!("invalid number '{}'", val)) -} - -pub fn seconds(val: &OsString) -> anyhow::Result { - let val = val.to_string_lossy(); - let secs = u64::from_str(&val).map_err(|_| anyhow!("invalid number of seconds '{}'", val))?; - - Ok(time::Duration::from_secs(secs)) -} - -pub fn milliseconds(val: &OsString) -> anyhow::Result { - let val = val.to_string_lossy(); - let secs = - u64::from_str(&val).map_err(|_| anyhow!("invalid number of milliseconds '{}'", val))?; - - Ok(time::Duration::from_millis(secs)) -} - -pub fn string(val: &OsString) -> String { - val.to_string_lossy().to_string() -} - -pub fn rev(val: &OsString) -> anyhow::Result { - let s = val.to_str().ok_or(anyhow!("invalid git rev {val:?}"))?; - Ok(Rev::from(s.to_owned())) -} - -pub fn oid(val: &OsString) -> anyhow::Result { - let s = string(val); - let o = radicle::git::Oid::from_str(&s).map_err(|_| anyhow!("invalid git oid '{s}'"))?; - - Ok(o) -} - -pub fn alias(val: &OsString) -> anyhow::Result { - let val = val.as_os_str(); - let val = val - .to_str() - .ok_or_else(|| anyhow!("alias must be valid UTF-8"))?; - - Alias::from_str(val).map_err(|e| e.into()) -} - -pub fn issue(val: &OsString) -> anyhow::Result { - let val = val.to_string_lossy(); - issue::IssueId::from_str(&val).map_err(|_| anyhow!("invalid Issue ID '{}'", val)) -} - -pub fn patch(val: &OsString) -> anyhow::Result { - let val = val.to_string_lossy(); - patch::PatchId::from_str(&val).map_err(|_| anyhow!("invalid Patch ID '{}'", val)) -} - -pub fn cob(val: &OsString) -> anyhow::Result { - let val = val.to_string_lossy(); - cob::ObjectId::from_str(&val).map_err(|_| anyhow!("invalid Object ID '{}'", val)) -} - /// Targets used in the `block` and `unblock` commands #[derive(Clone, Debug)] pub(crate) enum BlockTarget {