decthings_api/tensor/
element.rs

1use std::borrow::Cow;
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
6#[serde(rename_all = "lowercase")]
7pub enum DecthingsElementType {
8    F32,
9    F64,
10    I8,
11    I16,
12    I32,
13    I64,
14    U8,
15    U16,
16    U32,
17    U64,
18    String,
19    Boolean,
20    Binary,
21    Image,
22    Audio,
23    Video,
24}
25
26impl std::fmt::Display for DecthingsElementType {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        let s = match self {
29            Self::F32 => "f32",
30            Self::F64 => "f64",
31            Self::I8 => "i8",
32            Self::I16 => "i16",
33            Self::I32 => "i32",
34            Self::I64 => "i64",
35            Self::U8 => "u8",
36            Self::U16 => "u16",
37            Self::U32 => "u32",
38            Self::U64 => "u64",
39            Self::String => "string",
40            Self::Boolean => "boolean",
41            Self::Binary => "binary",
42            Self::Image => "image",
43            Self::Audio => "audio",
44            Self::Video => "video",
45        };
46        write!(f, "{}", s)
47    }
48}
49
50#[derive(Debug)]
51pub enum SetFormatError {
52    FormatStringTooLong,
53}
54
55#[derive(Debug, Clone)]
56pub struct DecthingsElementImage<'a> {
57    format: Cow<'a, str>,
58    pub data: Cow<'a, [u8]>,
59}
60
61impl<'a> DecthingsElementImage<'a> {
62    pub fn new(
63        format: impl Into<Cow<'a, str>>,
64        data: impl Into<Cow<'a, [u8]>>,
65    ) -> Result<Self, SetFormatError> {
66        let format = format.into();
67        if format.len() > u8::MAX.into() {
68            return Err(SetFormatError::FormatStringTooLong);
69        }
70        Ok(Self {
71            format,
72            data: data.into(),
73        })
74    }
75
76    pub fn format(&self) -> &str {
77        &self.format
78    }
79
80    pub fn set_format(&mut self, format: impl Into<Cow<'a, str>>) -> Result<(), SetFormatError> {
81        let format = format.into();
82        if format.len() > u8::MAX.into() {
83            return Err(SetFormatError::FormatStringTooLong);
84        }
85        self.format = format.into();
86        Ok(())
87    }
88}
89
90#[derive(Debug, Clone)]
91pub struct DecthingsElementAudio<'a> {
92    format: Cow<'a, str>,
93    pub data: Cow<'a, [u8]>,
94}
95
96impl<'a> DecthingsElementAudio<'a> {
97    pub fn new(
98        format: impl Into<Cow<'a, str>>,
99        data: impl Into<Cow<'a, [u8]>>,
100    ) -> Result<Self, SetFormatError> {
101        let format = format.into();
102        if format.len() > u8::MAX.into() {
103            return Err(SetFormatError::FormatStringTooLong);
104        }
105        Ok(Self {
106            format,
107            data: data.into(),
108        })
109    }
110
111    pub fn format(&self) -> &str {
112        &self.format
113    }
114
115    pub fn set_format(&mut self, format: impl Into<Cow<'a, str>>) -> Result<(), SetFormatError> {
116        let format = format.into();
117        if format.len() > u8::MAX.into() {
118            return Err(SetFormatError::FormatStringTooLong);
119        }
120        self.format = format;
121        Ok(())
122    }
123}
124
125#[derive(Debug, Clone)]
126pub struct DecthingsElementVideo<'a> {
127    format: Cow<'a, str>,
128    pub data: Cow<'a, [u8]>,
129}
130
131impl<'a> DecthingsElementVideo<'a> {
132    pub fn new(
133        format: impl Into<Cow<'a, str>>,
134        data: impl Into<Cow<'a, [u8]>>,
135    ) -> Result<Self, SetFormatError> {
136        let format = format.into();
137        if format.len() > u8::MAX.into() {
138            return Err(SetFormatError::FormatStringTooLong);
139        }
140        Ok(Self {
141            format,
142            data: data.into(),
143        })
144    }
145
146    pub fn format(&self) -> &str {
147        &self.format
148    }
149
150    pub fn set_format(&mut self, format: impl Into<Cow<'a, str>>) -> Result<(), SetFormatError> {
151        let format = format.into();
152        if format.len() > u8::MAX.into() {
153            return Err(SetFormatError::FormatStringTooLong);
154        }
155        self.format = format;
156        Ok(())
157    }
158}