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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
use std::collections::HashMap;
use ndata::data::*;
use ndata::dataobject::*;
#[cfg(feature="serde_support")]
use serde::*;

#[derive(Debug)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct Case {
  pub input: HashMap<String, Node>,
  pub output: HashMap<String, Node>,
  pub cmds: Vec<Operation>,
  pub cons: Vec<Connection>,
  pub nextcase: Option<Box<Case>>,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct Node {
  #[cfg_attr(feature = "serde_support", serde(default))]
  pub mode: String,
  #[cfg_attr(feature = "serde_support", serde(default))]
  #[cfg_attr(feature = "serde_support", serde(rename = "type"))]
  pub cmd_type: String,
  #[cfg_attr(feature = "serde_support", serde(default))]
  pub x:f32,
  #[cfg_attr(feature = "serde_support", serde(skip))]
  pub val:Data,
  #[cfg_attr(feature = "serde_support", serde(default))]
  pub done: bool,
  pub list: Option<String>,
  #[cfg_attr(feature = "serde_support", serde(rename = "loop"))]
  pub looop: Option<String>,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct Operation {
  #[cfg_attr(feature = "serde_support", serde(rename = "in"))]
  pub input: HashMap<String, Node>,
  #[cfg_attr(feature = "serde_support", serde(rename = "out"))]
  pub output: HashMap<String, Node>,
  pub pos: Pos,
  pub name: String,
  pub width: f64,
  #[cfg_attr(feature = "serde_support", serde(rename = "type"))]
  pub cmd_type: String,
  pub ctype: Option<String>,
  pub cmd: Option<String>,
  pub localdata: Option<Box<Case>>,
  #[cfg_attr(feature = "serde_support", serde(skip))]
  pub result: Option<DataObject>,
  pub condition: Option<Condition>,
  #[cfg_attr(feature = "serde_support", serde(default))]
  pub done: bool,
  #[cfg_attr(feature = "serde_support", serde(default))]
  pub finish: bool,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct Connection {
  pub src: Dest,
  pub dest: Dest,

  #[cfg_attr(feature = "serde_support", serde(default))]
  pub done: bool,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct Pos {
  pub x: f64,
  pub y: f64,
  pub z: f64,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct Dest {
  pub index: i64,
  pub name: String,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct Condition {
  pub value: bool,
  pub rule: String,
}

impl Connection {
  pub fn from_data(data:DataObject) -> Connection {
    let s = data.get_array("src");
    let src =  Dest{
      index: s.get_i64(0),
      name: s.get_string(1),
    };
    let s = data.get_array("dest");
    let dest =  Dest{
      index: s.get_i64(0),
      name: s.get_string(1),
    };
    let done = false;
    Connection {
      src: src,
      dest: dest,
      done: done,
    }
  }

  pub fn duplicate(&self) -> Connection {
    let src =  Dest{
      index: self.src.index,
      name: self.src.name.to_owned(),
    };
    let dest =  Dest{
      index: self.dest.index,
      name: self.dest.name.to_owned(),
    };
    let done = false;
    Connection {
      src: src,
      dest: dest,
      done: done,
    }
  }
}


impl Condition {
  pub fn from_data(data:DataObject) -> Condition {
    Condition {
      value: data.get_bool("value"),
      rule: data.get_string("rule"),
    }
  }

  pub fn duplicate(&self) -> Condition {
    Condition {
      value: self.value,
      rule: self.rule.to_owned(),
    }
  }
}

impl Operation {
  pub fn from_data(data:DataObject) -> Operation {
    let mut input = HashMap::<String, Node>::new();
    let mut output = HashMap::<String, Node>::new();
    let p = data.get_object("pos");
    let pos = Pos{
      x: p.get_f64("x"),
      y: p.get_f64("y"),
      z: p.get_f64("z"),
    };
    let name = data.get_string("name");
    let width = data.get_f64("width");
    let cmd_type = data.get_string("type");
    let mut ctype: Option<String> = None;
    let mut cmd: Option<String> = None;
    let mut localdata: Option<Box<Case>> = None;
    let result = None;
    let mut condition: Option<Condition> = None;
    let done = false;
    let finish = false;
    
    for (k, node) in data.get_object("in").objects() {
      input.insert(k.to_string(), Node::from_data(node.object()));
    }
    
    for (k, node) in data.get_object("out").objects() {
      output.insert(k.to_string(), Node::from_data(node.object()));
    }
    
    if data.has("ctype") { ctype = Some(data.get_string("ctype")); }
    if data.has("cmd") { cmd = Some(data.get_string("cmd")); }
    if data.has("localdata") { localdata = Some(Box::new(Case::from_data(data.get_object("localdata")))); }
    if data.has("condition") { condition = Some(Condition::from_data(data.get_object("condition"))); }
    
    Operation {
      input: input,
      output: output,
      pos: pos,
      name: name,
      width: width,
      cmd_type: cmd_type,
      ctype: ctype,
      cmd: cmd,
      localdata: localdata,
      result: result,
      condition: condition,
      done: done,
      finish: finish,
    }  
  }

  pub fn duplicate(&self) -> Operation {
    let mut input = HashMap::<String, Node>::new();
    let mut output = HashMap::<String, Node>::new();
    let pos = Pos{
      x: self.pos.x,
      y: self.pos.y,
      z: self.pos.z,
    };
    let name = self.name.to_owned();
    let width = self.width;
    let cmd_type = self.cmd_type.to_owned();
    let mut ctype: Option<String> = None;
    let mut cmd: Option<String> = None;
    let mut localdata: Option<Box<Case>> = None;
    let result = None;
    let mut condition: Option<Condition> = None;
    let done = false;
    let finish = false;
    
    for (k, node) in &self.input {
      input.insert(k.to_string(), node.duplicate());
    }
    
    for (k, node) in &self.output {
      output.insert(k.to_string(), node.duplicate());
    }
    
    if !self.ctype.is_none() { ctype = Some(self.ctype.as_ref().unwrap().to_owned()); }
    if !self.cmd.is_none() { cmd = Some(self.cmd.as_ref().unwrap().to_owned()); }
    if !self.localdata.is_none() { localdata = Some(Box::new(self.localdata.as_ref().unwrap().duplicate())); }
    if !self.condition.is_none() { condition = Some(self.condition.as_ref().unwrap().duplicate()); }
    
    Operation {
      input: input,
      output: output,
      pos: pos,
      name: name,
      width: width,
      cmd_type: cmd_type,
      ctype: ctype,
      cmd: cmd,
      localdata: localdata,
      result: result,
      condition: condition,
      done: done,
      finish: finish,
    }  
  }
}

impl Node {
  pub fn from_data(data:DataObject) -> Node {
    let mode = data.get_string("mode");
    let cmd_type = data.get_string("type");
    let x = data.get_f64("x") as f32;
    let val = Data::DNull;
    let done = false;
    let mut list: Option<String> = None;
    let mut looop: Option<String> = None;
    
    if data.has("list") {
      list = Some(data.get_string("list"));
    }
    
    if data.has("loop") {
      looop = Some(data.get_string("loop"));
    }
    
    Node {
      mode: mode,
      cmd_type: cmd_type,
      x: x,
      val: val,
      done: done,
      list: list,
      looop: looop,
    }
  }

  pub fn duplicate(&self) -> Node {
    let mode = self.mode.to_owned();
    let cmd_type = self.cmd_type.to_owned();
    let x = self.x;
    let val = Data::DNull;
    let done = false;
    let mut list: Option<String> = None;
    let mut looop: Option<String> = None;
    
    if !self.list.is_none() {
      list = Some(self.list.as_ref().unwrap().to_owned());
    }
    
    if !self.looop.is_none() {
      looop = Some(self.looop.as_ref().unwrap().to_owned());
    }
    
    Node {
      mode: mode,
      cmd_type: cmd_type,
      x: x,
      val: val,
      done: done,
      list: list,
      looop: looop,
    }
  }
}

impl Case {
  pub fn new(data: &str) -> Case {
    #[cfg(feature="serde_support")]
    return serde_json::from_str(data).unwrap(); 
    
    #[allow(unreachable_code)]
    {
      let data = DataObject::from_string(data);
      return Case::from_data(data);
    }
  }
  
  pub fn from_data(data:DataObject) -> Case {
    let mut input = HashMap::<String, Node>::new();
    let mut output = HashMap::<String, Node>::new();
    let mut cmds = Vec::<Operation>::new();
    let mut cons = Vec::<Connection>::new();
    let mut nextcase = None;
    
    for (k, node) in data.get_object("input").objects() {
      input.insert(k.to_string(), Node::from_data(node.object()));
    }
    
    for (k, node) in data.get_object("output").objects() {
      output.insert(k.to_string(), Node::from_data(node.object()));
    }
    
    for op in data.get_array("cmds").objects() {
      cmds.push(Operation::from_data(op.object()));
    }
    
    for con in data.get_array("cons").objects() {
      cons.push(Connection::from_data(con.object()));
    }
    
    if data.has("nextcase") {
      nextcase = Some(Box::new(Case::from_data(data.get_object("nextcase"))));
    }
    
    Case {
      input: input,
      output: output,
      cmds: cmds,
      cons: cons,
      nextcase: nextcase,
    }
  }
  
  pub fn duplicate(&self) -> Case {
    let mut input = HashMap::<String, Node>::new();
    let mut output = HashMap::<String, Node>::new();
    let mut cmds = Vec::<Operation>::new();
    let mut cons = Vec::<Connection>::new();
    let mut nextcase = None;
    
    for (k, node) in &self.input {
      input.insert(k.to_string(), node.duplicate());
    }
    
    for (k, node) in &self.output {
      output.insert(k.to_string(), node.duplicate());
    }
    
    for op in &self.cmds {
      cmds.push(op.duplicate());
    }
    
    for con in &self.cons {
      cons.push(con.duplicate());
    }
    
    if !self.nextcase.is_none() {
      nextcase = Some(Box::new(self.nextcase.as_ref().unwrap().duplicate()));
    }
    
    Case {
      input: input,
      output: output,
      cmds: cmds,
      cons: cons,
      nextcase: nextcase,
    }
  }
}