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
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use crate::Error;

/// Cid protocol version
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub enum Version {
    V0,
    V1,
}

use Version::*;

impl Version {
    /// Generates version from integer value
    pub fn from(raw: u64) -> Result<Version, Error> {
        match raw {
            0 => Ok(V0),
            1 => Ok(V1),
            _ => Err(Error::InvalidCidVersion),
        }
    }

    /// Returns true if string is version 0
    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")
    }

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

impl From<Version> for u64 {
    fn from(ver: Version) -> u64 {
        match ver {
            V0 => 0,
            V1 => 1,
        }
    }
}