image_blp/types/
version.rsuse std::fmt;
use std::str;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum BlpVersion {
Blp0,
Blp1,
Blp2,
}
impl BlpVersion {
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
}
pub fn from_magic(magic: [u8; 4]) -> Option<BlpVersion> {
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);
}
}