use std::fmt;
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Codex32(codex32::Error),
WrongHrp {
got: String,
},
ThresholdNotZero {
got: u8,
},
ShareIndexNotSecret {
got: char,
},
TagInvalidAlphabet {
got: [u8; 4],
},
UnknownTag {
got: [u8; 4],
},
ReservedTagNotEmittedInV01 {
got: [u8; 4],
},
ReservedPrefixViolation {
got: u8,
},
UnexpectedStringLength {
got: usize,
allowed: &'static [usize],
},
PayloadLengthMismatch {
tag: [u8; 4],
expected: &'static [usize],
got: usize,
},
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Codex32(e) => write!(f, "codex32 parse error: {:?}", e),
Error::WrongHrp { got } => write!(f, "wrong HRP: got {:?}, expected \"ms\"", got),
Error::ThresholdNotZero { got } => {
write!(
f,
"threshold not 0 (got '{}'); v0.1 is single-string only",
*got as char
)
}
Error::ShareIndexNotSecret { got } => {
write!(
f,
"share-index not 's' (got '{}'); BIP-93 requires 's' for threshold=0",
got
)
}
Error::TagInvalidAlphabet { got } => {
write!(f, "tag bytes not in codex32 alphabet: {:?}", got)
}
Error::UnknownTag { got } => write!(
f,
"unknown tag {:?}; not a member of RESERVED_TAG_TABLE",
std::str::from_utf8(got).unwrap_or("<non-utf8>")
),
Error::ReservedTagNotEmittedInV01 { got } => write!(
f,
"tag {:?} reserved-not-emitted in v0.1; deferred to v0.2+",
std::str::from_utf8(got).unwrap_or("<non-utf8>")
),
Error::ReservedPrefixViolation { got } => {
write!(f, "reserved-prefix byte was 0x{:02x}, expected 0x00", got)
}
Error::UnexpectedStringLength { got, allowed } => {
write!(f, "string length {} outside v0.1 set {:?}", got, allowed)
}
Error::PayloadLengthMismatch { tag, expected, got } => write!(
f,
"tag {:?} payload length {} not in expected set {:?}",
std::str::from_utf8(tag).unwrap_or("<non-utf8>"),
got,
expected
),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}
impl From<codex32::Error> for Error {
fn from(e: codex32::Error) -> Self {
Error::Codex32(e)
}
}
pub type Result<T> = std::result::Result<T, Error>;