ici_files/
file.rs

1use crate::errors::IndexedImageError;
2use crate::errors::IndexedImageError::*;
3use crate::file::FileType::*;
4
5//last is file version
6pub(crate) const HEADER: [u8; 4] = [b'I', b'C', b'I', 1];
7
8#[derive(Debug, Clone, Eq, PartialEq)]
9pub enum FileType {
10    Image,
11    Animated,
12}
13
14impl FileType {
15    pub(crate) fn to_byte(&self) -> u8 {
16        match self {
17            Image => 1,
18            Animated => 2,
19        }
20    }
21
22    pub(crate) fn from_byte(byte: u8) -> Option<FileType> {
23        match byte {
24            1 => Some(Image),
25            2 => Some(Animated),
26            _ => None,
27        }
28    }
29
30    pub fn name(&self) -> &'static str {
31        match self {
32            Image => "Image",
33            Animated => "Animated Image",
34        }
35    }
36
37    pub fn ext(&self) -> &'static str {
38        match self {
39            Image => "ici",
40            Animated => "ica",
41        }
42    }
43}
44
45pub(super) fn verify_format(bytes: &[u8]) -> Result<FileType, IndexedImageError> {
46    if bytes.len() < 10 {
47        return Err(NotIciFile);
48    }
49    if bytes[0..HEADER.len()] != HEADER {
50        return Err(NotIciFile);
51    }
52    let format = bytes[HEADER.len()];
53    match FileType::from_byte(format) {
54        None => Err(UnknownIciVersion(format)),
55        Some(file_type) => Ok(file_type),
56    }
57}