1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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,
}
}
}