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
use serde::*;
use serde_json::*;
use std::collections::HashMap;

use ndata::data::*;
use ndata::dataobject::*;

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

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

#[derive(Debug, Serialize, Deserialize)]
pub struct Connection {
  pub src: Dest,
  pub dest: Dest,

  #[serde(default)]
  pub done: bool,
}

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

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

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

impl Connection {
  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 duplicate(&self) -> Condition {
    Condition {
      value: self.value,
      rule: self.rule.to_owned(),
    }
  }
}

impl Operation {
  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 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) -> Result<Case> {
    let c: Case = serde_json::from_str(data)?;
    Ok(c)
  }
  
  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,
    }
  }
}