use core::convert::TryFrom;
use crate::error::{Error, Result};
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug, Hash)]
#[cfg_attr(feature = "scale-codec", derive(parity_scale_codec::Decode))]
#[cfg_attr(feature = "scale-codec", derive(parity_scale_codec::Encode))]
pub enum Version {
V0,
V1,
}
impl Version {
pub fn is_v0_str(data: &str) -> bool {
data.len() == 46 && data.starts_with("Qm")
}
pub fn is_v0_binary(data: &[u8]) -> bool {
data.len() == 34 && data.starts_with(&[0x12, 0x20])
}
}
impl TryFrom<u64> for Version {
type Error = Error;
fn try_from(raw: u64) -> Result<Self> {
match raw {
0 => Ok(Self::V0),
1 => Ok(Self::V1),
_ => Err(Error::InvalidCidVersion),
}
}
}
impl From<Version> for u64 {
fn from(ver: Version) -> u64 {
match ver {
Version::V0 => 0,
Version::V1 => 1,
}
}
}