pub mod params;
use core::fmt;
use crate::constants::ChainHash;
#[rustfmt::skip] #[doc(inline)]
pub use self::params::Params;
#[doc(no_inline)]
pub use network::ParseNetworkError;
#[doc(inline)]
pub use network::{Network, NetworkKind};
pub trait NetworkExt: sealed::Sealed {
fn chain_hash(self) -> ChainHash;
fn from_chain_hash(chain_hash: ChainHash) -> Option<Self>;
fn params(self) -> &'static Params;
}
impl NetworkExt for Network {
fn chain_hash(self) -> ChainHash {
ChainHash::using_genesis_block_const(self)
}
fn from_chain_hash(chain_hash: ChainHash) -> Option<Self> {
Self::try_from(chain_hash).ok()
}
fn params(self) -> &'static Params {
match self {
Self::Tidecoin => &Params::MAINNET,
Self::Testnet => &Params::TESTNET,
Self::Regtest => &Params::REGTEST,
}
}
}
mod sealed {
pub trait Sealed: Sized {}
impl Sealed for super::Network {}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct UnknownChainHashError(ChainHash);
impl fmt::Display for UnknownChainHashError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "unknown chain hash: {}", self.0)
}
}
#[cfg(feature = "std")]
impl std::error::Error for UnknownChainHashError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}
impl TryFrom<ChainHash> for Network {
type Error = UnknownChainHashError;
fn try_from(chain_hash: ChainHash) -> Result<Self, Self::Error> {
match chain_hash {
ChainHash::TIDECOIN => Ok(Self::Tidecoin),
ChainHash::REGTEST => Ok(Self::Regtest),
_ => Err(UnknownChainHashError(chain_hash)),
}
}
}
#[cfg(test)]
mod tests {}