mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat: introduce ChainSpecParser generic in cli types (#10582)
This commit is contained in:
@ -1,8 +1,7 @@
|
||||
use alloy_genesis::Genesis;
|
||||
use clap::{builder::TypedValueParser, error::Result, Arg, Command};
|
||||
use reth_chainspec::{ChainSpec, DEV, HOLESKY, MAINNET, SEPOLIA};
|
||||
use reth_cli::chainspec::ChainSpecParser;
|
||||
use std::{ffi::OsStr, fs, path::PathBuf, sync::Arc};
|
||||
use std::{fs, path::PathBuf, sync::Arc};
|
||||
|
||||
/// Clap value parser for [`ChainSpec`]s.
|
||||
///
|
||||
@ -40,7 +39,9 @@ fn chain_value_parser(s: &str) -> eyre::Result<Arc<ChainSpec>, eyre::Error> {
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct EthChainSpecParser;
|
||||
|
||||
impl ChainSpecParser<ChainSpec> for EthChainSpecParser {
|
||||
impl ChainSpecParser for EthChainSpecParser {
|
||||
type ChainSpec = ChainSpec;
|
||||
|
||||
const SUPPORTED_CHAINS: &'static [&'static str] = &["mainnet", "sepolia", "holesky", "dev"];
|
||||
|
||||
fn parse(s: &str) -> eyre::Result<Arc<ChainSpec>> {
|
||||
@ -48,35 +49,6 @@ impl ChainSpecParser<ChainSpec> for EthChainSpecParser {
|
||||
}
|
||||
}
|
||||
|
||||
impl TypedValueParser for EthChainSpecParser {
|
||||
type Value = Arc<ChainSpec>;
|
||||
|
||||
fn parse_ref(
|
||||
&self,
|
||||
_cmd: &Command,
|
||||
arg: Option<&Arg>,
|
||||
value: &OsStr,
|
||||
) -> Result<Self::Value, clap::Error> {
|
||||
let val =
|
||||
value.to_str().ok_or_else(|| clap::Error::new(clap::error::ErrorKind::InvalidUtf8))?;
|
||||
<Self as ChainSpecParser<ChainSpec>>::parse(val).map_err(|err| {
|
||||
let arg = arg.map(|a| a.to_string()).unwrap_or_else(|| "...".to_owned());
|
||||
let possible_values = Self::SUPPORTED_CHAINS.join(",");
|
||||
let msg = format!(
|
||||
"Invalid value '{val}' for {arg}: {err}.\n [possible values: {possible_values}]"
|
||||
);
|
||||
clap::Error::raw(clap::error::ErrorKind::InvalidValue, msg)
|
||||
})
|
||||
}
|
||||
|
||||
fn possible_values(
|
||||
&self,
|
||||
) -> Option<Box<dyn Iterator<Item = clap::builder::PossibleValue> + '_>> {
|
||||
let values = Self::SUPPORTED_CHAINS.iter().map(clap::builder::PossibleValue::new);
|
||||
Some(Box::new(values))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@ -84,7 +56,7 @@ mod tests {
|
||||
#[test]
|
||||
fn parse_known_chain_spec() {
|
||||
for &chain in EthChainSpecParser::SUPPORTED_CHAINS {
|
||||
assert!(<EthChainSpecParser as ChainSpecParser<ChainSpec>>::parse(chain).is_ok());
|
||||
assert!(<EthChainSpecParser as ChainSpecParser>::parse(chain).is_ok());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user