use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ChainId(String);
impl ChainId {
pub fn new(id: impl Into<String>) -> Self {
Self(id.into())
}
pub fn mainnet() -> Self {
Self("mainnet".to_string())
}
pub fn testnet() -> Self {
Self("testnet".to_string())
}
pub fn is_mainnet(&self) -> bool {
self.0 == "mainnet"
}
pub fn is_testnet(&self) -> bool {
self.0 == "testnet"
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Default for ChainId {
fn default() -> Self {
Self::mainnet()
}
}
impl AsRef<str> for ChainId {
fn as_ref(&self) -> &str {
&self.0
}
}
impl fmt::Display for ChainId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_chain_id_display() {
assert_eq!(ChainId::mainnet().to_string(), "mainnet");
assert_eq!(ChainId::testnet().to_string(), "testnet");
assert_eq!(ChainId::new("custom").to_string(), "custom");
}
#[test]
fn test_chain_id_predicates() {
assert!(ChainId::mainnet().is_mainnet());
assert!(!ChainId::mainnet().is_testnet());
assert!(ChainId::testnet().is_testnet());
assert!(!ChainId::testnet().is_mainnet());
}
#[test]
fn test_default_is_mainnet() {
assert_eq!(ChainId::default(), ChainId::mainnet());
}
#[test]
fn test_as_str() {
assert_eq!(ChainId::mainnet().as_str(), "mainnet");
assert_eq!(ChainId::new("localnet").as_str(), "localnet");
}
#[test]
fn test_as_ref_str() {
let chain_id = ChainId::mainnet();
let s: &str = chain_id.as_ref();
assert_eq!(s, "mainnet");
}
#[test]
fn test_custom_chain_id() {
let chain_id = ChainId::new("my-custom-network");
assert_eq!(chain_id.as_str(), "my-custom-network");
assert!(!chain_id.is_mainnet());
assert!(!chain_id.is_testnet());
}
}