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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DecthingsElementType {
    F32,
    F64,
    I8,
    I16,
    I32,
    I64,
    U8,
    U16,
    U32,
    U64,
    String,
    Boolean,
    Binary,
    Image,
    Audio,
    Video,
}

impl std::fmt::Display for DecthingsElementType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Self::F32 => "f32",
            Self::F64 => "f64",
            Self::I8 => "i8",
            Self::I16 => "i16",
            Self::I32 => "i32",
            Self::I64 => "i64",
            Self::U8 => "u8",
            Self::U16 => "u16",
            Self::U32 => "u32",
            Self::U64 => "u64",
            Self::String => "string",
            Self::Boolean => "boolean",
            Self::Binary => "binary",
            Self::Image => "image",
            Self::Audio => "audio",
            Self::Video => "video",
        };
        write!(f, "{}", s)
    }
}

#[derive(Debug)]
pub enum SetFormatError {
    LengthNot3Bytes,
}

#[derive(Debug, Clone)]
pub struct DecthingsElementImage<'a> {
    format: &'a str,
    pub data: &'a [u8],
}

impl<'a> DecthingsElementImage<'a> {
    pub fn new(format: &'a str, data: &'a [u8]) -> Result<Self, SetFormatError> {
        let mut v = Self { format: "", data };
        v.set_format(format)?;
        Ok(v)
    }

    pub fn format(&self) -> &str {
        self.format
    }

    pub fn set_format(&mut self, format: &'a str) -> Result<(), SetFormatError> {
        if format.len() != 3 {
            return Err(SetFormatError::LengthNot3Bytes);
        }
        self.format = format;
        Ok(())
    }
}

#[derive(Debug, Clone)]
pub struct DecthingsElementAudio<'a> {
    format: &'a str,
    pub data: &'a [u8],
}

impl<'a> DecthingsElementAudio<'a> {
    pub fn new(format: &'a str, data: &'a [u8]) -> Result<Self, SetFormatError> {
        let mut v = Self { format: "", data };
        v.set_format(format)?;
        Ok(v)
    }

    pub fn format(&self) -> &str {
        self.format
    }

    pub fn set_format(&mut self, format: &'a str) -> Result<(), SetFormatError> {
        if format.len() != 3 {
            return Err(SetFormatError::LengthNot3Bytes);
        }
        self.format = format;
        Ok(())
    }
}

#[derive(Debug, Clone)]
pub struct DecthingsElementVideo<'a> {
    format: &'a str,
    pub data: &'a [u8],
}

impl<'a> DecthingsElementVideo<'a> {
    pub fn new(format: &'a str, data: &'a [u8]) -> Result<Self, SetFormatError> {
        let mut v = Self { format: "", data };
        v.set_format(format)?;
        Ok(v)
    }

    pub fn format(&self) -> &str {
        self.format
    }

    pub fn set_format(&mut self, format: &'a str) -> Result<(), SetFormatError> {
        if format.len() != 3 {
            return Err(SetFormatError::LengthNot3Bytes);
        }
        self.format = format;
        Ok(())
    }
}