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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use std::{
borrow::{Borrow, Cow},
fmt::{self, Display, Formatter},
hash::Hash,
ops::Deref,
path::PathBuf,
sync::Arc,
};
use hotg_rune_core::Shape;
use indexmap::IndexMap;
use legion::Entity;
use crate::parse::{Path, ResourceType};
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Sink {
pub kind: SinkKind,
pub args: IndexMap<String, ResourceOrString>,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case", tag = "type")]
pub enum SinkKind {
Serial,
Tensor,
Other(String),
}
impl Display for SinkKind {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
SinkKind::Serial => write!(f, "serial"),
SinkKind::Tensor => write!(f, "tensor"),
SinkKind::Other(s) => write!(f, "{}", s),
}
}
}
impl<'a> From<&'a str> for SinkKind {
fn from(s: &'a str) -> SinkKind {
match s {
"serial" | "SERIAL" => SinkKind::Serial,
"tensor" | "TENSOR" => SinkKind::Tensor,
_ => SinkKind::Other(s.to_string()),
}
}
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Model {
pub model_file: ModelFile,
pub args: IndexMap<String, ResourceOrString>,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ModelFile {
FromDisk(PathBuf),
Resource(Entity),
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Source {
pub kind: SourceKind,
pub parameters: IndexMap<String, ResourceOrString>,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case", tag = "type")]
pub enum SourceKind {
Random,
Accelerometer,
Sound,
Image,
Raw,
FloatImage,
Other(String),
}
impl SourceKind {
pub const fn as_capability_index(&self) -> Option<u32> {
match self {
SourceKind::Random => Some(hotg_rune_core::capabilities::RAND),
SourceKind::Accelerometer => {
Some(hotg_rune_core::capabilities::ACCEL)
},
SourceKind::Sound => Some(hotg_rune_core::capabilities::SOUND),
SourceKind::Image => Some(hotg_rune_core::capabilities::IMAGE),
SourceKind::Raw => Some(hotg_rune_core::capabilities::RAW),
SourceKind::FloatImage => {
Some(hotg_rune_core::capabilities::FLOAT_IMAGE)
},
_ => None,
}
}
pub fn as_capability_name(&self) -> Option<&'static str> {
self.as_capability_index()
.and_then(hotg_rune_core::capabilities::name)
}
}
impl<'a> From<&'a str> for SourceKind {
fn from(s: &'a str) -> SourceKind {
match s {
"rand" | "RAND" => SourceKind::Random,
"accel" | "ACCEL" => SourceKind::Accelerometer,
"sound" | "SOUND" => SourceKind::Sound,
"image" | "IMAGE" => SourceKind::Image,
"raw" | "RAW" => SourceKind::Raw,
"float-image" | "FLOAT_IMAGE" => SourceKind::FloatImage,
_ => SourceKind::Other(s.to_string()),
}
}
}
impl Display for SourceKind {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
SourceKind::Other(custom) => Display::fmt(custom, f),
_ => write!(f, "{:?}", self),
}
}
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct ProcBlock {
pub path: Path,
pub parameters: IndexMap<String, ResourceOrString>,
}
impl ProcBlock {
pub(crate) fn name(&self) -> &str {
let full_name = self.path.sub_path.as_ref().unwrap_or(&self.path.base);
let start_of_name = full_name.rfind('/').map(|ix| ix + 1).unwrap_or(0);
&full_name[start_of_name..]
}
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Resource {
pub default_value: Option<ResourceSource>,
pub ty: ResourceType,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum ResourceSource {
Inline(String),
FromDisk(PathBuf),
}
#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
serde::Serialize,
serde::Deserialize,
)]
pub struct Name(String);
impl<S: Into<String>> From<S> for Name {
fn from(s: S) -> Self { Name(s.into()) }
}
impl Deref for Name {
type Target = String;
fn deref(&self) -> &String { &self.0 }
}
impl Display for Name {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { f.write_str(&self.0) }
}
impl Borrow<String> for Name {
fn borrow(&self) -> &String { &self.0 }
}
impl Borrow<str> for Name {
fn borrow(&self) -> &str { &self.0 }
}
impl<S> AsRef<S> for Name
where
String: AsRef<S>,
{
fn as_ref(&self) -> &S { self.0.as_ref() }
}
#[derive(
Debug, Default, PartialEq, Clone, serde::Serialize, serde::Deserialize,
)]
pub struct NameTable(IndexMap<Name, Entity>);
impl NameTable {
pub(crate) fn clear(&mut self) { self.0.clear(); }
pub(crate) fn insert(&mut self, name: Name, ent: Entity) {
self.0.insert(name, ent);
}
}
impl Deref for NameTable {
type Target = IndexMap<Name, Entity>;
fn deref(&self) -> &Self::Target { &self.0 }
}
impl From<IndexMap<Name, Entity>> for NameTable {
fn from(m: IndexMap<Name, Entity>) -> Self { NameTable(m) }
}
#[derive(
Debug,
Default,
Copy,
Clone,
PartialEq,
Eq,
Hash,
serde::Serialize,
serde::Deserialize,
)]
pub struct PipelineNode;
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Tensor(pub Shape<'static>);
impl From<Shape<'static>> for Tensor {
fn from(s: Shape<'static>) -> Self { Tensor(s) }
}
#[derive(
Debug, Default, Clone, PartialEq, serde::Serialize, serde::Deserialize,
)]
pub struct Outputs {
pub tensors: Vec<Entity>,
}
#[derive(
Debug, Default, Clone, PartialEq, serde::Serialize, serde::Deserialize,
)]
pub struct Inputs {
pub tensors: Vec<Entity>,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ResourceData(pub Arc<[u8]>);
impl<T: Into<Arc<[u8]>>> From<T> for ResourceData {
fn from(data: T) -> Self { ResourceData(data.into()) }
}
impl AsRef<[u8]> for ResourceData {
fn as_ref(&self) -> &[u8] { &*self }
}
impl Deref for ResourceData {
type Target = [u8];
fn deref(&self) -> &Self::Target { &self.0 }
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ModelData(pub Arc<[u8]>);
impl<A: Into<Arc<[u8]>>> From<A> for ModelData {
fn from(data: A) -> Self { ModelData(data.into()) }
}
impl Deref for ModelData {
type Target = [u8];
fn deref(&self) -> &Self::Target { &self.0 }
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Mimetype(Cow<'static, str>);
impl Mimetype {
pub const ONNX: Mimetype =
Mimetype(Cow::Borrowed(hotg_rune_core::ONNX_MIMETYPE));
pub const TENSORFLOW: Mimetype =
Mimetype(Cow::Borrowed(hotg_rune_core::TF_MIMETYPE));
pub const TENSORFLOW_JS: Mimetype =
Mimetype(Cow::Borrowed(hotg_rune_core::TFJS_MIMETYPE));
pub const TENSORFLOW_LITE: Mimetype =
Mimetype(Cow::Borrowed(hotg_rune_core::TFLITE_MIMETYPE));
}
impl Deref for Mimetype {
type Target = str;
fn deref(&self) -> &Self::Target { &self.0 }
}
impl Default for Mimetype {
fn default() -> Self { Mimetype::TENSORFLOW_LITE }
}
impl From<&'static str> for Mimetype {
fn from(s: &'static str) -> Self { Mimetype(Cow::Borrowed(s)) }
}
impl From<String> for Mimetype {
fn from(s: String) -> Self { Mimetype(Cow::Owned(s)) }
}
impl AsRef<str> for Mimetype {
fn as_ref(&self) -> &str { &self.0 }
}
#[derive(
Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
)]
pub enum ResourceOrString {
String(String),
Resource(Entity),
}
impl<'a> From<&'a str> for ResourceOrString {
fn from(s: &'a str) -> Self { ResourceOrString::String(s.into()) }
}
impl From<String> for ResourceOrString {
fn from(s: String) -> Self { ResourceOrString::String(s) }
}