diff --git a/zingolib/src/config.rs b/zingolib/src/config.rs index f60389b80..bb1cfc770 100644 --- a/zingolib/src/config.rs +++ b/zingolib/src/config.rs @@ -49,13 +49,18 @@ impl std::fmt::Display for ChainType { } } +/// Invalid chain type. +#[derive(thiserror::Error, Debug)] +#[error("Invalid chain type '{0}'. Expected one of: 'mainnet', 'testnet' or 'regtest'.")] +pub struct InvalidChainType(String); + impl TryFrom<&str> for ChainType { type Error = InvalidChainType; fn try_from(value: &str) -> Result { match value { - "mainnet" => Ok(ChainType::Mainnet), - "testnet" => Ok(ChainType::Testnet), + "mainnet" | "main" => Ok(ChainType::Mainnet), + "testnet" | "test" => Ok(ChainType::Testnet), "regtest" => Ok(ChainType::Regtest(ActivationHeights::default())), _ => Err(InvalidChainType(value.to_string())), } @@ -107,11 +112,6 @@ pub(crate) mod consealed { } } -/// Invalid chain type. -#[derive(thiserror::Error, Debug)] -#[error("Invalid chain type '{0}'. Expected one of: 'mainnet', 'testnet' or 'regtest'.")] -pub struct InvalidChainType(String); - /// Configuration data for the construction of a [`crate::wallet::LightWallet`]. #[derive(Clone, Debug, PartialEq, Eq)] pub enum WalletConfig { @@ -462,7 +462,9 @@ fn wallet_dir_or_default(opt_wallet_dir: Option, chain: ChainType) -> P #[cfg(test)] mod tests { - use crate::config::{ChainType, ClientConfig}; + use crate::config::ChainType; + use crate::config::ClientConfig; + use zingo_common_components::protocol::ActivationHeights; #[tokio::test] async fn test_load_clientconfig() { @@ -483,4 +485,30 @@ mod tests { assert_eq!(valid_config.indexer_uri(), valid_uri); assert_eq!(valid_config.chain_type(), ChainType::Mainnet); } + + #[test] + fn test_chaintypes_from_strings() { + assert_eq!(ChainType::try_from("mainnet").unwrap(), ChainType::Mainnet); + assert_eq!(ChainType::try_from("testnet").unwrap(), ChainType::Testnet); + // the following 2 variants match the string from get_lightd_info, from the LightdInfo struct + // as referenced in librustzcash/zcash_client_backend/src/proto/service.rs 17a4830c70 + // and provided by an indexer (previously called lightwallet) + assert_eq!(ChainType::try_from("main").unwrap(), ChainType::Mainnet); + assert_eq!(ChainType::try_from("test").unwrap(), ChainType::Testnet); + assert_eq!( + ChainType::try_from("regtest").unwrap(), + ChainType::Regtest(ActivationHeights::default()) + ); + assert!(ChainType::try_from("invalid").is_err()); + let err = ChainType::try_from("badvalue").unwrap_err(); + assert_eq!(err.0, "badvalue"); + assert!(ChainType::try_from("").is_err()); + assert!(ChainType::try_from("Mainnet").is_err()); + assert!(ChainType::try_from("MAINNET").is_err()); + assert!(ChainType::try_from("Testnet").is_err()); + assert!(ChainType::try_from("Regtest").is_err()); + assert!(ChainType::try_from("mainnet ").is_err()); // trailing space + assert!(ChainType::try_from(" mainnet").is_err()); // leading space + assert!(ChainType::try_from("regtест").is_err()); // lookalike chars + } }