use core::fmt;
use bitcoin::bip32::Xpub;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct InconsistentKeySourcesError(pub Xpub);
impl fmt::Display for InconsistentKeySourcesError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "combining PSBT, key-source conflict for xpub {}", self.0)
}
}
#[cfg(feature = "std")]
impl std::error::Error for InconsistentKeySourcesError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum FeeError {
FundingUtxo(FundingUtxoError),
InputOverflow,
OutputOverflow,
Negative,
}
impl fmt::Display for FeeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use FeeError::*;
match *self {
FundingUtxo(ref e) => write_err!(f, "funding utxo error for input"; e),
InputOverflow => f.write_str("integer overflow in fee calculation adding input"),
OutputOverflow => f.write_str("integer overflow in fee calculation adding output"),
Negative => f.write_str("PSBT has a negative fee which is not allowed"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for FeeError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use FeeError::*;
match *self {
FundingUtxo(ref e) => Some(e),
InputOverflow | OutputOverflow | Negative => None,
}
}
}
impl From<FundingUtxoError> for FeeError {
fn from(e: FundingUtxoError) -> Self { Self::FundingUtxo(e) }
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum FundingUtxoError {
OutOfBounds {
vout: usize,
len: usize,
},
MissingUtxo,
}
impl fmt::Display for FundingUtxoError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use FundingUtxoError::*;
match *self {
OutOfBounds { vout, len } =>
write!(f, "vout {} out of bounds for tx list len: {}", vout, len),
MissingUtxo => write!(f, "no funding utxo found"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for FundingUtxoError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use FundingUtxoError::*;
match *self {
OutOfBounds { .. } | MissingUtxo => None,
}
}
}
macro_rules! write_err {
($writer:expr, $string:literal $(, $args:expr)*; $source:expr) => {
{
#[cfg(feature = "std")]
{
let _ = &$source; write!($writer, $string $(, $args)*)
}
#[cfg(not(feature = "std"))]
{
write!($writer, concat!($string, ": {}") $(, $args)*, $source)
}
}
}
}
pub(crate) use write_err;