fix: unify name of supported chains with strum (#1249)

* add strum

* update strum version

* whitespace

* revert strum

* using strum EnumVariantNames

* fix strum serialize kebab case and edge cases
This commit is contained in:
Shawn Harmsen 2022-05-17 04:28:35 +09:00 committed by GitHub
parent d7b9c59f00
commit ceafcb250b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 3 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/target /target
.vscode .vscode
/.envrc /.envrc
.idea

23
Cargo.lock generated
View File

@ -1260,6 +1260,7 @@ dependencies = [
"rlp-derive", "rlp-derive",
"serde", "serde",
"serde_json", "serde_json",
"strum",
"syn", "syn",
"thiserror", "thiserror",
"tiny-keccak", "tiny-keccak",
@ -3618,6 +3619,28 @@ version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "strum"
version = "0.24.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e96acfc1b70604b8b2f1ffa4c57e59176c7dbb05d556c71ecd2f5498a1dee7f8"
dependencies = [
"strum_macros",
]
[[package]]
name = "strum_macros"
version = "0.24.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6878079b17446e4d3eba6192bb0a2950d5b14f0ed8424b852310e5a94345d0ef"
dependencies = [
"heck",
"proc-macro2",
"quote",
"rustversion",
"syn",
]
[[package]] [[package]]
name = "subtle" name = "subtle"
version = "2.4.1" version = "2.4.1"

View File

@ -31,6 +31,7 @@ bytes = { version = "1.1.0", features = ["serde"] }
hex = { version = "0.4.3", default-features = false, features = ["std"] } hex = { version = "0.4.3", default-features = false, features = ["std"] }
once_cell = { version = "1.10.0", optional = true } once_cell = { version = "1.10.0", optional = true }
unicode-xid = "0.2.3" unicode-xid = "0.2.3"
strum = { version = "0.24", features = ["derive"] }
# macros feature enabled dependencies # macros feature enabled dependencies
cargo_metadata = { version = "0.14.2", optional = true } cargo_metadata = { version = "0.14.2", optional = true }

View File

@ -5,20 +5,23 @@ use core::convert::TryFrom;
use std::{convert::TryInto, default, fmt, str::FromStr}; use std::{convert::TryInto, default, fmt, str::FromStr};
use crate::types::U256; use crate::types::U256;
use strum::{EnumVariantNames};
#[derive(Debug, Clone, Error)] #[derive(Debug, Clone, Error)]
#[error("Failed to parse chain: {0}")] #[error("Failed to parse chain: {0}")]
pub struct ParseChainError(String); pub struct ParseChainError(String);
#[repr(u64)] #[repr(u64)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Deserialize)] #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Deserialize, EnumVariantNames)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
#[strum(serialize_all = "kebab-case")]
pub enum Chain { pub enum Chain {
Mainnet = 1, Mainnet = 1,
Ropsten = 3, Ropsten = 3,
Rinkeby = 4, Rinkeby = 4,
Goerli = 5, Goerli = 5,
Kovan = 42, Kovan = 42,
#[strum(serialize = "xdai")]
XDai = 100, XDai = 100,
Polygon = 137, Polygon = 137,
Fantom = 250, Fantom = 250,
@ -33,12 +36,14 @@ pub enum Chain {
Moonriver = 1285, Moonriver = 1285,
Optimism = 10, Optimism = 10,
OptimismKovan = 69, OptimismKovan = 69,
BinanceSmartChain = 56,
BinanceSmartChainTestnet = 97,
Arbitrum = 42161, Arbitrum = 42161,
ArbitrumTestnet = 421611, ArbitrumTestnet = 421611,
Cronos = 25, Cronos = 25,
CronosTestnet = 338, CronosTestnet = 338,
#[strum(serialize = "bsc")]
BinanceSmartChain = 56,
#[strum(serialize = "bsc-testnet")]
BinanceSmartChainTestnet = 97,
} }
impl fmt::Display for Chain { impl fmt::Display for Chain {