use core::{cmp, fmt};
use bitcoin::absolute;
pub const MAX_ABSOLUTE_LOCKTIME: u32 = 0x8000_0000;
pub const MIN_ABSOLUTE_LOCKTIME: u32 = 1;
#[derive(Debug, PartialEq)]
pub struct AbsLockTimeError {
value: u32,
}
impl fmt::Display for AbsLockTimeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.value < MIN_ABSOLUTE_LOCKTIME {
f.write_str("absolute locktimes in Miniscript have a minimum value of 1")
} else {
debug_assert!(self.value > MAX_ABSOLUTE_LOCKTIME);
write!(
f,
"absolute locktimes in Miniscript have a maximum value of 0x{:08x}; got 0x{:08x}",
MAX_ABSOLUTE_LOCKTIME, self.value
)
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for AbsLockTimeError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AbsLockTime(absolute::LockTime);
impl AbsLockTime {
pub fn from_consensus(n: u32) -> Result<Self, AbsLockTimeError> {
if n >= MIN_ABSOLUTE_LOCKTIME && n <= MAX_ABSOLUTE_LOCKTIME {
Ok(AbsLockTime(absolute::LockTime::from_consensus(n)))
} else {
Err(AbsLockTimeError { value: n })
}
}
pub fn to_consensus_u32(self) -> u32 { self.0.to_consensus_u32() }
pub fn is_block_height(&self) -> bool { self.0.is_block_height() }
pub fn is_block_time(&self) -> bool { self.0.is_block_time() }
}
impl From<AbsLockTime> for absolute::LockTime {
fn from(lock_time: AbsLockTime) -> absolute::LockTime { lock_time.0 }
}
impl cmp::PartialOrd for AbsLockTime {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { Some(self.cmp(other)) }
}
impl cmp::Ord for AbsLockTime {
fn cmp(&self, other: &Self) -> cmp::Ordering {
let this = self.0.to_consensus_u32();
let that = other.0.to_consensus_u32();
this.cmp(&that)
}
}
impl fmt::Display for AbsLockTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.0, f) }
}