1use core::convert::TryFrom;
2
3use crate::error::{Error, Result};
4
5#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug, Hash)]
7#[cfg_attr(feature = "scale-codec", derive(parity_scale_codec::Decode))]
8#[cfg_attr(feature = "scale-codec", derive(parity_scale_codec::Encode))]
9pub enum Version {
10 V0,
12 V1,
14}
15
16impl Version {
17 pub fn is_v0_str(data: &str) -> bool {
19 data.len() == 46 && data.starts_with("Qm")
22 }
23
24 pub fn is_v0_binary(data: &[u8]) -> bool {
26 data.len() == 34 && data.starts_with(&[0x12, 0x20])
27 }
28}
29
30impl TryFrom<u64> for Version {
32 type Error = Error;
33
34 fn try_from(raw: u64) -> Result<Self> {
35 match raw {
36 0 => Ok(Self::V0),
37 1 => Ok(Self::V1),
38 _ => Err(Error::InvalidCidVersion),
39 }
40 }
41}
42
43impl From<Version> for u64 {
44 fn from(ver: Version) -> u64 {
45 match ver {
46 Version::V0 => 0,
47 Version::V1 => 1,
48 }
49 }
50}