foundry_cli/opts/
chain.rsuse clap::builder::{PossibleValuesParser, TypedValueParser};
use eyre::Result;
use foundry_config::{Chain, NamedChain};
use std::ffi::OsStr;
use strum::VariantNames;
#[derive(Clone, Debug)]
pub struct ChainValueParser {
pub inner: PossibleValuesParser,
}
impl Default for ChainValueParser {
fn default() -> Self {
Self { inner: PossibleValuesParser::from(NamedChain::VARIANTS) }
}
}
impl TypedValueParser for ChainValueParser {
type Value = Chain;
fn parse_ref(
&self,
cmd: &clap::Command,
arg: Option<&clap::Arg>,
value: &OsStr,
) -> Result<Self::Value, clap::Error> {
let s =
value.to_str().ok_or_else(|| clap::Error::new(clap::error::ErrorKind::InvalidUtf8))?;
if let Ok(id) = s.parse() {
Ok(Chain::from_id(id))
} else {
s.parse()
.map_err(|_| self.inner.parse_ref(cmd, arg, value).unwrap_err())
.map(Chain::from_named)
}
}
}