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