Add NetworkValidationError
The `require_network` function can fail in one way only, add a specific error for the failure.
This commit is contained in:
parent
d85817b880
commit
a92dc9c35c
|
@ -9,6 +9,26 @@ use crate::blockdata::script::{witness_program, witness_version};
|
|||
use crate::prelude::*;
|
||||
use crate::{base58, Network};
|
||||
|
||||
/// Address's network differs from required one.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct NetworkValidationError {
|
||||
/// Network that was required.
|
||||
pub(crate) required: Network,
|
||||
/// The address itself.
|
||||
pub(crate) address: Address<NetworkUnchecked>,
|
||||
}
|
||||
|
||||
impl fmt::Display for NetworkValidationError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "address ")?;
|
||||
fmt::Display::fmt(&self.address.0, f)?;
|
||||
write!(f, " is not valid on {}", self.required)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl std::error::Error for NetworkValidationError {}
|
||||
|
||||
/// Address error.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
|
|
|
@ -55,7 +55,7 @@ use self::error::P2shError;
|
|||
#[rustfmt::skip] // Keep public re-exports separate.
|
||||
#[doc(inline)]
|
||||
pub use self::{
|
||||
error::{Error, ParseError, UnknownAddressTypeError, UnknownHrpError, FromScriptError},
|
||||
error::{Error, NetworkValidationError, ParseError, UnknownAddressTypeError, UnknownHrpError, FromScriptError, },
|
||||
};
|
||||
|
||||
/// The different types of addresses.
|
||||
|
@ -667,11 +667,11 @@ impl Address<NetworkUnchecked> {
|
|||
/// For details about this mechanism, see section [*Parsing addresses*](Address#parsing-addresses)
|
||||
/// on [`Address`].
|
||||
#[inline]
|
||||
pub fn require_network(self, required: Network) -> Result<Address, Error> {
|
||||
pub fn require_network(self, required: Network) -> Result<Address, NetworkValidationError> {
|
||||
if self.is_valid_for_network(required) {
|
||||
Ok(self.assume_checked())
|
||||
} else {
|
||||
Err(Error::NetworkValidation { required, address: self })
|
||||
Err(NetworkValidationError { required, address: self })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue