image_blp/types/
version.rs

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use std::fmt;
use std::str;

/// Version of type format that determines structure of file. Encodes
/// magic bytes that are expected at start of the file.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum BlpVersion {
    Blp0,
    Blp1,
    Blp2,
}

impl BlpVersion {
    /// Convert to 4 ASCII symbols that are put into beginning of file
    /// to mark that the file has specific version of the format.
    pub fn to_magic(&self) -> [u8; 4] {
        let mut magic: [u8; 4] = Default::default();
        match self {
            BlpVersion::Blp0 => magic.copy_from_slice("BLP0".as_bytes()),
            BlpVersion::Blp1 => magic.copy_from_slice("BLP1".as_bytes()),
            BlpVersion::Blp2 => magic.copy_from_slice("BLP2".as_bytes()),
        }
        magic
    }

    /// Convert from 4 ASCII symbols from the start of file to known
    /// tag of version.
    pub fn from_magic(magic: [u8; 4]) -> Option<BlpVersion> {
        // Use that ascii is valid subset of utf8
        let magic_str = str::from_utf8(&magic).ok()?;

        match magic_str {
            "BLP0" => Some(BlpVersion::Blp0),
            "BLP1" => Some(BlpVersion::Blp1),
            "BLP2" => Some(BlpVersion::Blp2),
            _ => None,
        }
    }
}

impl fmt::Display for BlpVersion {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            BlpVersion::Blp0 => write!(f, "BLP0"),
            BlpVersion::Blp1 => write!(f, "BLP1"),
            BlpVersion::Blp2 => write!(f, "BLP2"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UnknownBlpVersion(String);

impl fmt::Display for UnknownBlpVersion {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Unknown BLP version: {}", self.0)
    }
}

impl std::str::FromStr for BlpVersion {
    type Err = UnknownBlpVersion;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.trim().to_lowercase().as_ref() {
            "blp0" => Ok(BlpVersion::Blp0),
            "blp1" => Ok(BlpVersion::Blp1),
            "blp2" => Ok(BlpVersion::Blp2),
            _ => Err(UnknownBlpVersion(s.to_owned())),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn known_magics() {
        let magic1 = [0x42, 0x4c, 0x50, 0x31];
        let version1 = BlpVersion::from_magic(magic1);
        assert_eq!(version1, Some(BlpVersion::Blp1));
        assert_eq!(version1.unwrap().to_magic(), magic1);

        let magic2 = [0x42, 0x4c, 0x50, 0x32];
        let version2 = BlpVersion::from_magic(magic2);
        assert_eq!(version2, Some(BlpVersion::Blp2));
        assert_eq!(version2.unwrap().to_magic(), magic2);

        let magic3 = [0x42, 0x4c, 0x50, 0x30];
        let version3 = BlpVersion::from_magic(magic3);
        assert_eq!(version3, Some(BlpVersion::Blp0));
        assert_eq!(version3.unwrap().to_magic(), magic3);
    }
}