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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use std::fmt;

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

use flowrstructs::output_connection::OutputConnection;

use crate::compiler::loader::Validate;
use crate::errors::*;
use crate::model::io::{IO, IOType};
use crate::model::io::IOSet;
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;
use crate::model::route::SetRoute;

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct Function {
    #[serde(rename = "function")]
    name: Name,
    #[serde(default = "Function::default_impure")]
    impure: bool,
    implementation: String,
    #[serde(rename = "input")]
    pub inputs: IOSet,
    #[serde(rename = "output")]
    outputs: IOSet,

    #[serde(skip_deserializing)]
    alias: Name,
    #[serde(skip_deserializing, default = "Function::default_source_url")]
    source_url: String,
    #[serde(skip_deserializing)]
    route: Route,
    #[serde(skip_deserializing)]
    lib_reference: Option<String>,

    #[serde(skip_deserializing)]
    output_connections: Vec<OutputConnection>,
    #[serde(skip_deserializing)]
    id: usize,
    #[serde(skip_deserializing)]
    flow_id: usize,
}

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

impl HasRoute for Function {
    fn route(&self) -> &Route {
        &self.route
    }
    fn route_mut(&mut self) -> &mut Route {
        &mut self.route
    }
}

impl Function {
    #[allow(clippy::too_many_arguments)]
    pub fn new(name: Name, impure: bool, implementation: String, alias: Name, inputs: IOSet, outputs: IOSet, source_url: &str,
               route: Route, lib_reference: Option<String>, output_connections: Vec<OutputConnection>,
               id: usize, flow_id: usize) -> Self {
        Function {
            name,
            impure,
            implementation,
            alias,
            inputs,
            outputs,
            source_url: source_url.to_string(),
            route,
            lib_reference,
            output_connections,
            id,
            flow_id,
        }
    }

    pub fn set_id(&mut self, id: usize) {
        self.id = id;
    }

    pub fn get_id(&self) -> usize {
        self.id
    }

    pub fn set_flow_id(&mut self, flow_id: usize) {
        self.flow_id = flow_id;
    }

    pub fn get_flow_id(&self) -> usize {
        self.flow_id
    }

    pub fn is_impure(&self) -> bool {
        self.impure
    }

    pub fn get_inputs(&self) -> &IOSet {
        &self.inputs
    }

    pub fn get_mut_inputs(&mut self) -> &mut IOSet {
        &mut self.inputs
    }

    pub fn get_outputs(&self) -> IOSet {
        self.outputs.clone()
    }

    pub fn add_output_route(&mut self, output_route: OutputConnection) {
        self.output_connections.push(output_route);
    }

    pub fn get_output_connections(&self) -> &Vec<OutputConnection> {
        &self.output_connections
    }

    pub fn get_implementation(&self) -> &str {
        &self.implementation
    }

    pub fn set_implementation(&mut self, implementation: &str) {
        self.implementation = implementation.to_owned();
    }

    pub fn get_source_url(&self) -> &str {
        &self.source_url
    }

    pub fn set_source_url(&mut self, source: &str) {
        self.source_url = source.to_owned();
    }

    fn default_source_url() -> String {
        "file:///".to_string()
    }

    fn default_impure() -> bool {
        false
    }

    pub fn set_alias(&mut self, alias: &Name) {
        if alias.is_empty() {
            self.alias = self.name.clone();
        } else {
            self.alias = alias.clone();
        }
    }

    pub fn set_lib_reference(&mut self, lib_reference: Option<String>) {
        self.lib_reference = lib_reference
    }

    pub fn get_lib_reference(&self) -> &Option<String> {
        &self.lib_reference
    }
}

impl Validate for Function {
    fn validate(&self) -> Result<()> {
        self.name.validate()?;

        let mut io_count = 0;

        if let Some(ref inputs) = self.inputs {
            for i in inputs {
                io_count += 1;
                i.validate()?
            }
        }

        if let Some(ref outputs) = self.outputs {
            for i in outputs {
                io_count += 1;
                i.validate()?
            }
        }

        // A function must have at least one valid input or output
        if io_count == 0 {
            bail!("A function must have at least one input or output");
        }

        Ok(())
    }
}

impl fmt::Display for Function {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "name: \t\t{}", self.name)?;
        writeln!(f, "alias: \t\t{}", self.alias)?;
        writeln!(f, "id: \t\t{}", self.id)?;
        writeln!(f, "flow_id: \t\t{}", self.flow_id)?;

        writeln!(f, "inputs:")?;
        if let Some(ref inputs) = self.inputs {
            for input in inputs {
                writeln!(f, "\t{:#?}", input)?;
            }
        }

        writeln!(f, "outputs:")?;
        if let Some(ref outputs) = self.outputs {
            for output in outputs {
                writeln!(f, "\t{:#?}", output)?;
            }
        }

        Ok(())
    }
}

impl Default for Function {
    fn default() -> Function {
        Function {
            name: Name::default(),
            impure: false,
            implementation: "".to_owned(),
            alias: Name::default(),
            inputs: None,
            outputs: Some(vec!(IO::new("Value", Route::default()))),
            source_url: Function::default_source_url(),
            route: Route::default(),
            lib_reference: None,
            output_connections: vec!(OutputConnection::new("".to_string(), 0, 0, 0, 0, false, Some("".to_string()))),
            id: 0,
            flow_id: 0,
        }
    }
}

impl SetRoute for Function {
    fn set_routes_from_parent(&mut self, parent_route: &Route) {
        self.route = Route::from(format!("{}/{}", parent_route, self.alias));
        self.inputs.set_io_routes_from_parent(&self.route, IOType::FunctionIO);
        self.outputs.set_io_routes_from_parent(&self.route, IOType::FunctionIO);
    }
}

#[cfg(test)]
mod test {
    use flowrstructs::output_connection::OutputConnection;

    use crate::compiler::loader::Validate;
    use crate::model::datatype::DataType;
    use crate::model::io::Find;
    use crate::model::name::HasName;
    use crate::model::name::Name;
    use crate::model::route::HasRoute;
    use crate::model::route::Route;
    use crate::model::route::SetRoute;

    use super::Function;

    #[test]
    fn function_with_no_io_not_valid() {
        let fun = Function {
            name: Name::from("test_function"),
            impure: false,
            implementation: "".to_owned(),
            alias: Name::from("test_function"),
            source_url: Function::default_source_url(),
            inputs: Some(vec!()), // No inputs!
            outputs: None,         // No output!
            route: Route::default(),
            lib_reference: None,
            output_connections: vec!(OutputConnection::new("test_function".to_string(), 0, 0, 0, 0, false, None)),
            id: 0,
            flow_id: 0,
        };

        assert_eq!(fun.validate().is_err(), true);
    }

    #[test]
    fn deserialize_missing_name() {
        let function_str = "
        type = 'Value'
        ";

        let r_f: Result<Function, _> = toml::from_str(function_str);
        assert!(r_f.is_err());
    }

    #[test]
    fn deserialize_invalid() {
        let function_str = "
        name = 'test_function'
        ";

        let function: Result<Function, _> = toml::from_str(function_str);
        assert!(function.is_err());
    }

    #[test]
    fn deserialize_output_empty() {
        let function_str = "
        function = 'test_function'
        implementation = 'test.rs'

        [[output]]
        ";

        let function: Function = toml::from_str(function_str).unwrap();
        function.validate().unwrap();
        assert!(function.outputs.is_some());
    }

    #[test]
    fn deserialize_extra_field_fails() {
        let function_str = "
        function = 'test_function'
        implementation = 'test.rs'
        [[output]]
        foo = 'true'
        ";

        let function: Result<Function, _> = toml::from_str(function_str);
        assert!(function.is_err());
    }

    #[test]
    fn deserialize_default_output() {
        let function_str = "
        function = 'test_function'
        implementation = 'test.rs'
        [[output]]
        type = 'String'
        ";

        let function: Function = toml::from_str(function_str).unwrap();
        function.validate().unwrap();
        assert!(function.outputs.is_some());
        let output = &function.outputs.unwrap()[0];
        assert_eq!(*output.name(), Name::default());
        assert_eq!(output.datatype(), &DataType::from("String"));
    }

    #[test]
    fn deserialize_output_specified() {
        let function_str = "
        function = 'test_function'
        implementation = 'test.rs'

        [[output]]
        name = 'sub_output'
        type = 'String'
        ";

        let function: Function = toml::from_str(function_str).unwrap();
        function.validate().unwrap();
        assert!(function.outputs.is_some());
        let output = &function.outputs.unwrap()[0];
        assert_eq!(*output.name(), Name::from("sub_output"));
        assert_eq!(output.datatype(), &DataType::from("String"));
    }

    #[test]
    fn deserialize_two_outputs_specified() {
        let function_str = "
        function = 'test_function'
        implementation = 'test.rs'

        [[output]]
        name = 'sub_output'
        type = 'String'
        [[output]]
        name = 'other_output'
        type = 'Number'
        ";

        let function: Function = toml::from_str(function_str).unwrap();
        function.validate().unwrap();
        assert!(function.outputs.is_some());
        let outputs = function.outputs.unwrap();
        assert_eq!(outputs.len(), 2);
        let output0 = &outputs[0];
        assert_eq!(*output0.name(), Name::from("sub_output"));
        assert_eq!(output0.datatype(), &DataType::from("String"));
        let output1 = &outputs[1];
        assert_eq!(*output1.name(), Name::from("other_output"));
        assert_eq!(output1.datatype(), &DataType::from("Number"));
    }

    #[test]
    fn set_routes() {
        let function_str = "
        function = 'test_function'
        implementation = 'test.rs'

        [[output]]
        name = 'sub_output'
        type = 'String'
        [[output]]
        name = 'other_output'
        type = 'Number'
        ";

        // Setup
        let mut function: Function = toml::from_str(function_str).unwrap();
        function.alias = Name::from("test_alias");

        // Test
        function.set_routes_from_parent(&Route::from("/flow"));

        assert_eq!(function.route, Route::from("/flow/test_alias"));

        let outputs = function.outputs.unwrap();

        let output0 = &outputs[0];
        assert_eq!(*output0.route(), Route::from("/flow/test_alias/sub_output"));

        let output1 = &outputs[1];
        assert_eq!(*output1.route(), Route::from("/flow/test_alias/other_output"));
    }

    #[test]
    fn get_array_element_of_root_output() {
        // Create a function where the output is an Array of String
        let function_str = "
        function = 'test_function'
        implementation = 'test.rs'

        [[output]]
        type = 'Array/String'
        ";

        // Setup
        let mut function: Function = toml::from_str(function_str).unwrap();
        function.alias = Name::from("test_alias");
        function.set_routes_from_parent(&Route::from("/flow"));

        // Test
        // Try and get the output using a route to a specific element of the output
        let output = function.outputs.find_by_route(&Route::from("/0"), &None).unwrap();
        assert_eq!(*output.name(), Name::default());
    }
}