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
391
392
393
394
395
396
397
use std::collections::HashMap;
use std::collections::HashSet;
use std::fmt;

use error_chain::bail;
use serde_derive::{Deserialize, Serialize};

use flowrstructs::input::InputInitializer;

use crate::compiler::loader::Validate;
use crate::errors::*;
use crate::model::datatype::DataType;
use crate::model::datatype::HasDataType;
use crate::model::name::HasName;
use crate::model::name::Name;
use crate::model::route::HasRoute;
use crate::model::route::Route;
use crate::model::route::SetIORoutes;

#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub enum IOType {
    FunctionIO,
    FlowInput,
    FlowOutput,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct IO {
    #[serde(default = "Name::default")]
    #[serde(skip_serializing_if = "Name::empty")]
    name: Name,
    #[serde(rename = "type", default = "default_type")]
    datatype: DataType,
    #[serde(rename = "value")]
    initializer: Option<InputInitializer>,

    #[serde(skip_deserializing)]
    route: Route,
    #[serde(skip_deserializing, default = "default_io_type")]
    io_type: IOType,
}

impl IO {
    pub fn new<D: Into<DataType>, R: Into<Route> >(datatype: D, route: R) -> Self {
        let mut io = IO::default();
        io.datatype = datatype.into();
        io.route = route.into();
        io
    }

    pub fn flow_io(&self) -> bool {
        self.io_type != IOType::FunctionIO
    }

    pub fn io_type(&self) -> &IOType {
        &self.io_type
    }

    pub fn set_flow_io(&mut self, io_type: IOType) {
        self.io_type = io_type;
    }

    pub fn datatype(&self) -> &DataType {
        &self.datatype
    }

    pub fn set_route(&mut self, route: &Route, io_type: &IOType) {
        self.route = route.clone();
        self.io_type = io_type.clone();
    }

    fn set_route_from_parent(&mut self, parent: &Route, io_type: &IOType) {
        if self.name().is_empty() {
            self.set_route(&parent, &io_type);
        } else {
            self.set_route(&Route::from(&format!("{}/{}", parent, self.name)), &io_type);
        }
    }

    pub fn set_datatype(&mut self, datatype: &DataType) {
        self.datatype = datatype.clone()
    }

    pub fn get_initializer(&self) -> &Option<InputInitializer> {
        &self.initializer
    }

    pub fn set_initializer(&mut self, initial_value: &Option<InputInitializer>) {
        // Avoid overwriting a possibly Some() value with a None value
        if initial_value.is_some() {
            self.initializer = initial_value.clone();
        }
    }
}

impl Default for IO {
    fn default() -> Self {
        IO {
            name: Name::default(),
            datatype: default_type(),
            route: Route::default(),
            io_type: IOType::FunctionIO,
            initializer: None,
        }
    }
}

impl fmt::Display for IO {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "'{}' @ '{}'", self.name, self.route)
    }
}

impl HasName for IO {
    fn name(&self) -> &Name { &self.name }
    fn alias(&self) -> &Name { &self.name }
}

impl HasDataType for IO {
    fn datatype(&self) -> &DataType {
        &self.datatype
    }
}

impl HasRoute for IO {
    fn route(&self) -> &Route {
        &self.route
    }

    fn route_mut(&mut self) -> &mut Route {
        &mut self.route
    }
}

fn default_type() -> DataType {
    DataType::from("Value")
}

fn default_io_type() -> IOType { IOType::FunctionIO }

impl Validate for IO {
    fn validate(&self) -> Result<()> {
        self.datatype.valid()
    }
}

pub type IOSet = Option<Vec<IO>>;

impl Validate for IOSet {
    fn validate(&self) -> Result<()> {
        let mut name_set = HashSet::new();
        if let Some(ios) = self {
            for io in ios {
                io.validate()?;

                if io.name.is_empty() && !ios.is_empty() {
                    bail!("Cannot have empty IO name when there are multiple IOs");
                }

                if !name_set.insert(&io.name) {
                    bail!("Two IOs cannot have the same name: '{}'", io.name);
                }
            }
        }
        Ok(())
    }
}

impl SetIORoutes for IOSet {
    fn set_io_routes_from_parent(&mut self, parent: &Route, io_type: IOType) {
        if let Some(ref mut ios) = *self {
            for io in ios {
                io.set_route_from_parent(parent, &io_type)
            }
        }
    }
}

pub trait Find {
    fn find_by_name(&mut self, name: &Name, initial_value: &Option<InputInitializer>) -> Result<IO>;
    fn find_by_route(&mut self, route: &Route, initial_value: &Option<InputInitializer>) -> Result<IO>;
}

impl Find for IOSet {
    fn find_by_name(&mut self, name: &Name, initial_value: &Option<InputInitializer>) -> Result<IO> {
        if let Some(ref mut ios) = self {
            for io in ios {
                if io.name() == name {
                    io.set_initializer(initial_value);
                    return Ok(io.clone());
                }
            }
            bail!("No input or output with name '{}' was found", name);
        }
        bail!("No inputs or outputs found when looking for input/output named '{}'", name)
    }

    // TODO improve the Route handling of this - maybe moving into Router
    // TODO return a reference to the IO, with same lifetime as IOSet?
    fn find_by_route(&mut self, sub_route: &Route, initial_value: &Option<InputInitializer>) -> Result<IO> {
        if let Some(ref mut ios) = self {
            for io in ios {
                let (array_route, index, array_index) = sub_route.without_trailing_array_index();
                if array_index && (io.datatype().is_array()) && (Route::from(io.name()) == array_route.into_owned()) {
                    io.set_initializer(initial_value);

                    let mut found = io.clone();
                    found.set_datatype(&io.datatype.within_array()?); // the type within the array
                    let new_route = Route::from(format!("{}/{}", found.route(), index));
                    found.set_route(&new_route, &io.io_type);
                    return Ok(found);
                }

                if Route::from(io.name()) == *sub_route {
                    io.set_initializer(initial_value);
                    return Ok(io.clone());
                }
            }
            bail!("No output with sub-route '{}' was found", sub_route);
        }

        bail!("No inputs or outputs found when looking for input/output with sub-route '{}'", sub_route)
    }
}

impl IO {
    pub fn set_initial_values(ios: &mut IOSet, initializers: &Option<HashMap<String, InputInitializer>>) {
        if let Some(inits) = initializers {
            if let Some(inputs) = ios {
                for initializer in inits {
                    // initializer.0 is io name, initializer.1 is the initial value to set it to
                    for (index, input) in inputs.iter_mut().enumerate() {
                        if *input.name() == Name::from(initializer.0) ||
                            (initializer.0.as_str() == "default" && index == 0) {
                            input.initializer = Some(initializer.1.clone());
                        }
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod test {
    use crate::compiler::loader::Validate;
    use crate::model::datatype::DataType;
    use crate::model::io::IOType;
    use crate::model::name::HasName;
    use crate::model::name::Name;
    use crate::model::route::Route;

    use super::IO;

    #[test]
    fn deserialize_empty_string() {
        let input_str = "";

        let output: IO = toml::from_str(input_str).unwrap();
        output.validate().unwrap();
        assert_eq!(output.datatype, DataType::from("Value"));
        assert_eq!(output.name, Name::default());
    }

    #[test]
    fn deserialize_valid_type() {
        let input_str = "
        type = 'String'
        ";

        let output: IO = toml::from_str(input_str).unwrap();
        output.validate().unwrap();
    }

    #[test]
    fn deserialize_invalid_type() {
        let input_str = "
        type = 'Unknown'
        ";

        let output: IO = toml::from_str(input_str).unwrap();
        assert!(output.validate().is_err());
    }

    #[test]
    fn deserialize_name() {
        let input_str = "
        name = '/sub_route'
        type = 'String'
        ";

        let output: IO = toml::from_str(input_str).unwrap();
        output.validate().unwrap();
        assert_eq!("/sub_route", output.name.to_string());
    }

    #[test]
    fn deserialize_valid_string_type() {
        let input_str = "
        name = 'input'
        type = 'String'
        ";

        let input: IO = toml::from_str(input_str).unwrap();
        input.validate().unwrap();
    }

    #[test]
    fn methods_work() {
        let input_str = "
        name = 'input'
        type = 'String'
        ";

        let input: IO = toml::from_str(input_str).unwrap();
        assert_eq!(Name::from("input"), *input.name());
        assert_eq!(&DataType::from("String"), input.datatype());
    }

    #[test]
    fn deserialize_valid_json_type() {
        let input_str = "
        name = 'input'
        type = 'Value'
        ";

        let input: IO = toml::from_str(input_str).unwrap();
        input.validate().unwrap();
    }

    #[test]
    fn deserialize_extra_field_fails() {
        let input_str = "
        name = 'input'
        foo = 'extra token'
        type = 'Value'
        ";

        let input: Result<IO, _> = toml::from_str(input_str);
        assert!(input.is_err());
    }

    #[test]
    fn unique_io_names_validate() {
        let io0 = IO {
            name: Name::from("io_name"),
            datatype: DataType::from("String"),
            route: Route::default(),
            io_type: IOType::FunctionIO,
            initializer: None,
        };
        let io1 = IO {
            name: Name::from("different_name"),
            datatype: DataType::from("String"),
            route: Route::default(),
            io_type: IOType::FunctionIO,
            initializer: None,
        };
        let ioset = Some(vec!(io0, io1));
        ioset.validate().unwrap()
    }

    #[test]
    fn non_unique_io_names_wont_validate() {
        let io0 = IO {
            name: Name::from("io_name"),
            datatype: DataType::from("String"),
            route: Route::default(),
            io_type: IOType::FunctionIO,
            initializer: None,
        };
        let io1 = io0.clone();
        let ioset = Some(vec!(io0, io1));
        assert!(ioset.validate().is_err());
    }

    #[test]
    fn multiple_inputs_empty_name_not_allowed() {
        let io0 = IO {
            name: Name::from("io_name"),
            datatype: DataType::from("String"),
            route: Route::default(),
            io_type: IOType::FunctionIO,
            initializer: None,
        };
        let io1 = IO {
            name: Name::default(),
            datatype: DataType::from("String"),
            route: Route::default(),
            io_type: IOType::FunctionIO,
            initializer: None,
        };
        let ioset = Some(vec!(io0, io1));
        assert!(ioset.validate().is_err());
    }
}