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