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};

/// The version of the CID.
#[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 {
    /// CID version 0.
    V0,
    /// CID version 1.
    V1,
}

impl Version {
    /// Check if the version of `data` string is CIDv0.
    pub fn is_v0_str(data: &str) -> bool {
        // v0 is a Base58Btc encoded sha hash, so it has
        // fixed length and always begins with "Qm"
        data.len() == 46 && data.starts_with("Qm")
    }

    /// Check if the version of `data` bytes is CIDv0.
    pub fn is_v0_binary(data: &[u8]) -> bool {
        data.len() == 34 && data.starts_with(&[0x12, 0x20])
    }
}

/// Convert a number to the matching version, or `Error` if no valid version is matching.
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,
        }
    }
}