feat: add FromStr impl for Chain (#756)

* feat: add FromStr impl for Chain

* fix(core): return error if chain not found

Co-authored-by: Georgios Konstantopoulos <me@gakonst.com>
This commit is contained in:
odyslam.eth 2022-01-04 01:51:01 +03:00 committed by GitHub
parent a99949ccc2
commit 96ef787230
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 0 deletions

View File

@ -1,6 +1,12 @@
use std::fmt; use std::fmt;
use thiserror::Error;
use crate::types::U256; use crate::types::U256;
use std::str::FromStr;
#[derive(Debug, Clone, Error)]
#[error("Failed to parse chain: {0}")]
pub struct ParseChainError(String);
#[derive(Debug, Clone, Copy, Eq, PartialEq)] #[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Chain { pub enum Chain {
@ -62,3 +68,28 @@ impl From<Chain> for u64 {
u32::from(chain).into() u32::from(chain).into()
} }
} }
impl FromStr for Chain {
type Err = ParseChainError;
fn from_str(chain: &str) -> Result<Self, Self::Err> {
Ok(match chain {
"mainnet" => Chain::Mainnet,
"ropsten" => Chain::Ropsten,
"rinkeby" => Chain::Rinkeby,
"goerli" => Chain::Goerli,
"kovan" => Chain::Kovan,
"xdai" => Chain::XDai,
"polygon" => Chain::Polygon,
"polygon-mumbai" => Chain::PolygonMumbai,
"avalanche" => Chain::Avalanche,
"avalanche-fuji" => Chain::AvalancheFuji,
"sepolia" => Chain::Sepolia,
"moonbeam" => Chain::Moonbeam,
"moonbeam-dev" => Chain::MoonbeamDev,
"moonriver" => Chain::Moonriver,
"optimism" => Chain::Optimism,
"optimism-kovan" => Chain::OptimismKovan,
_ => return Err(ParseChainError(chain.to_owned())),
})
}
}