use crate::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct ContractVersion {
pub major: u32,
pub minor: u32,
pub patch: u32,
}
impl ContractVersion {
#[must_use]
pub const fn new(major: u32, minor: u32, patch: u32) -> Self {
Self {
major,
minor,
patch,
}
}
#[must_use]
pub fn accepts(self) -> bool {
if CURRENT.major == 0 {
self.major == 0 && READABLE.iter().any(|r| r.minor == self.minor)
} else {
self.major == CURRENT.major
}
}
}
impl std::fmt::Display for ContractVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
}
}
pub const CURRENT: ContractVersion = ContractVersion::new(0, 4, 0);
pub const READABLE: &[ContractVersion] = &[
ContractVersion::new(0, 2, 0),
ContractVersion::new(0, 3, 0),
CURRENT,
];
pub fn ensure_readable(found: ContractVersion) -> Result<(), Error> {
if found.accepts() {
Ok(())
} else {
Err(Error::UnsupportedContract {
found,
supported: CURRENT,
})
}
}
pub(crate) fn readable_summary() -> String {
READABLE
.iter()
.map(|v| format!("{}.{}", v.major, v.minor))
.collect::<Vec<_>>()
.join(", ")
}
#[cfg(test)]
mod tests;