reth_optimism_cli/chainspec.rs
1use reth_cli::chainspec::{parse_genesis, ChainSpecParser};
2use reth_optimism_chainspec::{generated_chain_value_parser, OpChainSpec, SUPPORTED_CHAINS};
3use std::sync::Arc;
4
5/// Optimism chain specification parser.
6#[derive(Debug, Clone, Default)]
7#[non_exhaustive]
8pub struct OpChainSpecParser;
9
10impl ChainSpecParser for OpChainSpecParser {
11 type ChainSpec = OpChainSpec;
12
13 const SUPPORTED_CHAINS: &'static [&'static str] = SUPPORTED_CHAINS;
14
15 fn parse(s: &str) -> eyre::Result<Arc<Self::ChainSpec>> {
16 chain_value_parser(s)
17 }
18}
19
20/// Clap value parser for [`OpChainSpec`]s.
21///
22/// The value parser matches either a known chain, the path
23/// to a json file, or a json formatted string in-memory. The json needs to be a Genesis struct.
24pub fn chain_value_parser(s: &str) -> eyre::Result<Arc<OpChainSpec>, eyre::Error> {
25 if let Some(op_chain_spec) = generated_chain_value_parser(s) {
26 Ok(op_chain_spec)
27 } else {
28 Ok(Arc::new(parse_genesis(s)?.into()))
29 }
30}
31
32#[cfg(test)]
33mod tests {
34 use super::*;
35
36 #[test]
37 fn parse_known_chain_spec() {
38 for &chain in OpChainSpecParser::SUPPORTED_CHAINS {
39 assert!(
40 <OpChainSpecParser as ChainSpecParser>::parse(chain).is_ok(),
41 "Failed to parse {chain}"
42 );
43 }
44 }
45}