libtlg_rs/
types.rs

1use std::{collections::HashMap, hash::Hash};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
4/// TLG Color Type
5pub enum TlgColorType {
6    /// Grayscale 8-bit
7    Grayscale8,
8    /// BGR 8-bit
9    Bgr24,
10    /// BGRA 8-bit
11    Bgra32,
12}
13
14#[derive(Debug, Clone)]
15/// TLG Image
16pub struct Tlg {
17    /// Tag dictionary
18    pub tags: HashMap<Vec<u8>, Vec<u8>>,
19    /// TLG Version: 0=unknown, 5=v5, 6=v6
20    pub version: u32,
21    /// Image width
22    pub width: u32,
23    /// Image height
24    pub height: u32,
25    /// Color type
26    pub color: TlgColorType,
27    /// Image data
28    pub data: Vec<u8>,
29}
30
31#[derive(Debug)]
32/// TLG Error
33pub enum TlgError {
34    /// IO Error
35    Io(std::io::Error),
36    /// Invalid TLG format
37    InvalidFormat,
38    /// Unsupported color type
39    UnsupportedColorType(u8),
40    /// Index out of range error
41    IndexOutOfRange,
42    /// Unsupported compressed method
43    UnsupportedCompressedMethod(u8),
44    /// String type error
45    Str(String),
46    #[cfg(feature = "encode")]
47    #[cfg_attr(docsrs, doc(cfg(feature = "encode")))]
48    /// Encoding error
49    EncodeError(String),
50}
51
52impl std::fmt::Display for TlgError {
53    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        match self {
55            TlgError::Io(e) => write!(f, "IO Error: {}", e),
56            TlgError::InvalidFormat => write!(f, "Invalid TLG format"),
57            TlgError::UnsupportedColorType(c) => write!(f, "Unsupported color type: {}", c),
58            TlgError::IndexOutOfRange => write!(f, "Index out of range"),
59            TlgError::UnsupportedCompressedMethod(m) => {
60                write!(f, "Unsupported compressed method: {}", m)
61            }
62            TlgError::Str(s) => write!(f, "{}", s),
63            #[cfg(feature = "encode")]
64            TlgError::EncodeError(s) => write!(f, "Encoding error: {}", s),
65        }
66    }
67}
68
69impl From<std::io::Error> for TlgError {
70    fn from(err: std::io::Error) -> Self {
71        TlgError::Io(err)
72    }
73}
74
75impl From<String> for TlgError {
76    fn from(err: String) -> Self {
77        TlgError::Str(err)
78    }
79}
80
81impl From<&str> for TlgError {
82    fn from(err: &str) -> Self {
83        TlgError::Str(err.to_string())
84    }
85}
86
87impl From<&String> for TlgError {
88    fn from(err: &String) -> Self {
89        TlgError::Str(err.clone())
90    }
91}
92
93impl std::error::Error for TlgError {}