flowcore 1.0.0

Structures shared between runtime and clients
Documentation
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//! TOML serialization for flow and function definitions.
//!
//! Hand-built TOML output that matches the format expected by the flow
//! deserializer. The derived `Serialize` on some flowcore types produces
//! struct-style output that is not compatible with the flow format.

use std::fmt::Write;
use std::path::Path;

use crate::model::flow_definition::FlowDefinition;
use crate::model::function_definition::FunctionDefinition;
use crate::model::input::InputInitializer;
use crate::model::io::IO;
use crate::model::name::HasName;

fn escape_toml_string(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for ch in s.chars() {
        match ch {
            '\\' => out.push_str("\\\\"),
            '"' => out.push_str("\\\""),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            '\u{0008}' => out.push_str("\\b"),
            '\u{000C}' => out.push_str("\\f"),
            other => out.push(other),
        }
    }
    out
}

/// Serialize a `serde_json::Value` into a TOML-compatible inline value string.
pub fn value_to_toml(v: &serde_json::Value) -> String {
    match v {
        serde_json::Value::String(s) => {
            format!("\"{}\"", escape_toml_string(s))
        }
        serde_json::Value::Number(n) => n.to_string(),
        serde_json::Value::Bool(b) => b.to_string(),
        serde_json::Value::Null => "\"null\"".to_string(),
        serde_json::Value::Array(a) => {
            let items: Vec<String> = a.iter().map(value_to_toml).collect();
            format!("[{}]", items.join(", "))
        }
        serde_json::Value::Object(m) => {
            let items: Vec<String> = m
                .iter()
                .map(|(k, val)| format!("{k} = {}", value_to_toml(val)))
                .collect();
            format!("{{ {} }}", items.join(", "))
        }
    }
}

fn initializer_to_toml(init: &InputInitializer) -> String {
    match init {
        InputInitializer::Once(v) => format!("{{ once = {} }}", value_to_toml(v)),
        InputInitializer::Always(v) => format!("{{ always = {} }}", value_to_toml(v)),
    }
}

fn write_io_toml(out: &mut String, section: &str, io: &IO) {
    let _ = writeln!(out, "\n[[{section}]]");
    let name = io.name();
    if !name.is_empty() {
        let _ = writeln!(out, "name = \"{name}\"");
    }
    let types = io.datatypes();
    if types.len() == 1 {
        if let Some(t) = types.first() {
            let _ = writeln!(out, "type = \"{t}\"");
        }
    } else if types.len() > 1 {
        let ts: Vec<String> = types.iter().map(|t| format!("\"{t}\"")).collect();
        let _ = writeln!(out, "type = [{}]", ts.join(", "));
    }
}

impl FlowDefinition {
    /// Serialize this flow definition to TOML format.
    #[must_use]
    pub fn to_toml(&self) -> String {
        let mut out = String::new();

        let _ = writeln!(out, "flow = \"{}\"", escape_toml_string(&self.name));

        if !self.description.is_empty() {
            let _ = writeln!(
                out,
                "description = \"{}\"",
                escape_toml_string(&self.description)
            );
        }

        if !self.docs.is_empty() {
            let _ = writeln!(out, "docs = \"{}\"", escape_toml_string(&self.docs));
        }

        let md = &self.metadata;
        if !md.version.is_empty() || !md.description.is_empty() || !md.authors.is_empty() {
            out.push_str("\n[metadata]\n");
            if !md.version.is_empty() {
                let _ = writeln!(out, "version = \"{}\"", escape_toml_string(&md.version));
            }
            if !md.description.is_empty() {
                let _ = writeln!(
                    out,
                    "description = \"{}\"",
                    escape_toml_string(&md.description)
                );
            }
            if !md.authors.is_empty() {
                let authors: Vec<String> = md
                    .authors
                    .iter()
                    .map(|a| format!("\"{}\"", escape_toml_string(a)))
                    .collect();
                let _ = writeln!(out, "authors = [{}]", authors.join(", "));
            }
        }

        for input in &self.inputs {
            write_io_toml(&mut out, "input", input);
        }

        for output in &self.outputs {
            write_io_toml(&mut out, "output", output);
        }

        for pref in &self.process_refs {
            out.push_str("\n[[process]]\n");
            if !pref.alias.is_empty() {
                let _ = writeln!(out, "alias = \"{}\"", pref.alias);
            }
            let _ = writeln!(out, "source = \"{}\"", pref.source);

            if let Some(x) = pref.x {
                let _ = writeln!(out, "x = {x}");
            }
            if let Some(y) = pref.y {
                let _ = writeln!(out, "y = {y}");
            }
            if let Some(w) = pref.width {
                let _ = writeln!(out, "width = {w}");
            }
            if let Some(h) = pref.height {
                let _ = writeln!(out, "height = {h}");
            }

            for (port_name, init) in &pref.initializations {
                let _ = writeln!(out, "input.{port_name} = {}", initializer_to_toml(init));
            }
        }

        for conn in &self.connections {
            let _ = writeln!(out, "\n[[connection]]");
            if !conn.name().is_empty() {
                let _ = writeln!(out, "name = \"{}\"", conn.name());
            }
            let _ = writeln!(out, "from = \"{}\"", conn.from());
            if let [single] = conn.to().as_slice() {
                let _ = writeln!(out, "to = \"{single}\"");
            } else {
                let to_strs: Vec<String> = conn.to().iter().map(|r| format!("\"{r}\"")).collect();
                let _ = writeln!(out, "to = [{}]", to_strs.join(", "));
            }
        }

        out
    }

    /// Save this flow definition to a TOML file at the given path.
    ///
    /// # Errors
    ///
    /// Returns an error if the file cannot be written.
    pub fn save_to_file(&self, path: &Path) -> Result<(), String> {
        std::fs::write(path, self.to_toml()).map_err(|e| format!("Could not write file: {e}"))
    }
}

impl FunctionDefinition {
    /// Serialize this function definition to TOML format.
    #[must_use]
    pub fn to_toml(&self) -> String {
        let mut out = format!(
            "function = \"{}\"\nsource = \"{}\"\ntype = \"rust\"\n",
            escape_toml_string(&self.name),
            escape_toml_string(&self.source)
        );

        if !self.description.is_empty() {
            let _ = writeln!(
                out,
                "description = \"{}\"",
                escape_toml_string(&self.description)
            );
        }

        for input in &self.inputs {
            write_io_toml(&mut out, "input", input);
        }

        for output in &self.outputs {
            write_io_toml(&mut out, "output", output);
        }

        out
    }

    /// Save this function definition to a TOML file at the given path.
    ///
    /// # Errors
    ///
    /// Returns an error if the file cannot be written.
    pub fn save_to_file(&self, path: &Path) -> Result<(), String> {
        std::fs::write(path, self.to_toml()).map_err(|e| format!("Could not write file: {e}"))
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod test {
    use super::*;
    use crate::model::connection::Connection;
    use crate::model::datatype::DataType;
    use crate::model::io::IO;
    use crate::model::metadata::MetaData;
    use crate::model::name::Name;
    use crate::model::process_reference::ProcessReference;
    use crate::model::route::Route;
    use std::collections::BTreeMap;

    #[test]
    fn flow_to_toml_minimal() {
        let flow = FlowDefinition {
            name: Name::from("test"),
            ..FlowDefinition::default()
        };
        let toml = flow.to_toml();
        assert!(toml.contains("flow = \"test\""));
    }

    #[test]
    fn flow_to_toml_with_description() {
        let flow = FlowDefinition {
            name: Name::from("test"),
            description: "A test flow".into(),
            ..FlowDefinition::default()
        };
        let toml = flow.to_toml();
        assert!(toml.contains("description = \"A test flow\""));
    }

    #[test]
    fn flow_to_toml_with_metadata() {
        let flow = FlowDefinition {
            name: Name::from("test"),
            metadata: MetaData {
                name: String::new(),
                version: "1.0.0".into(),
                description: String::new(),
                authors: vec!["Author".into()],
            },
            ..FlowDefinition::default()
        };
        let toml = flow.to_toml();
        assert!(toml.contains("[metadata]"));
        assert!(toml.contains("version = \"1.0.0\""));
        assert!(toml.contains("authors = [\"Author\"]"));
    }

    #[test]
    fn flow_to_toml_with_io() {
        let flow = FlowDefinition {
            name: Name::from("test"),
            inputs: vec![IO::new_named(
                vec![DataType::from("string")],
                Route::default(),
                "in0",
            )],
            outputs: vec![IO::new_named(
                vec![DataType::from("number")],
                Route::default(),
                "out0",
            )],
            ..FlowDefinition::default()
        };
        let toml = flow.to_toml();
        assert!(toml.contains("[[input]]"));
        assert!(toml.contains("name = \"in0\""));
        assert!(toml.contains("[[output]]"));
        assert!(toml.contains("name = \"out0\""));
    }

    #[test]
    fn flow_to_toml_with_process() {
        let flow = FlowDefinition {
            name: Name::from("test"),
            process_refs: vec![ProcessReference {
                alias: Name::from("add"),
                source: "lib://flowstdlib/math/add".into(),
                initializations: BTreeMap::new(),
                x: Some(100.0),
                y: Some(200.0),
                width: None,
                height: None,
            }],
            ..FlowDefinition::default()
        };
        let toml = flow.to_toml();
        assert!(toml.contains("[[process]]"));
        assert!(toml.contains("alias = \"add\""));
        assert!(toml.contains("source = \"lib://flowstdlib/math/add\""));
        assert!(toml.contains("x = 100"));
        assert!(toml.contains("y = 200"));
    }

    #[test]
    fn flow_to_toml_with_connection() {
        let flow = FlowDefinition {
            name: Name::from("test"),
            connections: vec![Connection::new("input/string", "print")],
            ..FlowDefinition::default()
        };
        let toml = flow.to_toml();
        assert!(toml.contains("[[connection]]"));
        assert!(toml.contains("from = \"input/string\""));
        assert!(toml.contains("to = \"print\""));
    }

    #[test]
    fn flow_to_toml_with_initializer() {
        let mut inits = BTreeMap::new();
        inits.insert(
            "start".into(),
            InputInitializer::Once(serde_json::json!(42)),
        );
        let flow = FlowDefinition {
            name: Name::from("test"),
            process_refs: vec![ProcessReference {
                alias: Name::from("seq"),
                source: "lib://flowstdlib/control/tap".into(),
                initializations: inits,
                x: None,
                y: None,
                width: None,
                height: None,
            }],
            ..FlowDefinition::default()
        };
        let toml = flow.to_toml();
        assert!(toml.contains("input.start = { once = 42 }"));
    }

    #[test]
    fn flow_roundtrip() {
        let flow = FlowDefinition {
            name: Name::from("roundtrip"),
            description: "Test roundtrip".into(),
            inputs: vec![IO::new_named(
                vec![DataType::from("string")],
                Route::default(),
                "input0",
            )],
            process_refs: vec![ProcessReference {
                alias: Name::from("func"),
                source: "func.toml".into(),
                initializations: BTreeMap::new(),
                x: Some(100.0),
                y: Some(200.0),
                width: Some(180.0),
                height: Some(120.0),
            }],
            connections: vec![Connection::new("input/input0", "func")],
            ..FlowDefinition::default()
        };
        let toml = flow.to_toml();

        let url = url::Url::parse("file:///fake.toml").expect("valid url");
        let deserializer =
            crate::deserializers::deserializer::get::<FlowDefinition>(&url).expect("deserializer");
        let parsed = deserializer
            .deserialize(&toml, Some(&url))
            .expect("roundtrip parse failed");
        assert_eq!(parsed.name, "roundtrip");
        assert_eq!(parsed.description, "Test roundtrip");
        assert_eq!(parsed.inputs.len(), 1);
        assert_eq!(parsed.process_refs.len(), 1);
        assert_eq!(parsed.connections.len(), 1);
    }

    #[test]
    fn function_to_toml_minimal() {
        let func = FunctionDefinition {
            name: Name::from("myfunc"),
            source: "myfunc.rs".into(),
            ..FunctionDefinition::default()
        };
        let toml = func.to_toml();
        assert!(toml.contains("function = \"myfunc\""));
        assert!(toml.contains("source = \"myfunc.rs\""));
        assert!(toml.contains("type = \"rust\""));
    }

    #[test]
    fn function_to_toml_with_ports() {
        let func = FunctionDefinition {
            name: Name::from("add"),
            source: "add.rs".into(),
            inputs: vec![
                IO::new_named(vec![DataType::from("number")], Route::default(), "a"),
                IO::new_named(vec![DataType::from("number")], Route::default(), "b"),
            ],
            outputs: vec![IO::new_named(
                vec![DataType::from("number")],
                Route::default(),
                "sum",
            )],
            ..FunctionDefinition::default()
        };
        let toml = func.to_toml();
        assert!(toml.contains("name = \"a\""));
        assert!(toml.contains("name = \"b\""));
        assert!(toml.contains("name = \"sum\""));
        assert_eq!(toml.matches("[[input]]").count(), 2);
        assert_eq!(toml.matches("[[output]]").count(), 1);
    }

    #[test]
    fn function_roundtrip() {
        let func = FunctionDefinition {
            name: Name::from("roundtrip"),
            source: "roundtrip.rs".into(),
            description: "Test func".into(),
            inputs: vec![IO::new_named(
                vec![DataType::from("string")],
                Route::default(),
                "input0",
            )],
            outputs: vec![IO::new_named(
                vec![DataType::from("number")],
                Route::default(),
                "output0",
            )],
            ..FunctionDefinition::default()
        };
        let toml = func.to_toml();

        let url = url::Url::parse("file:///fake.toml").expect("valid url");
        let deserializer =
            crate::deserializers::deserializer::get::<crate::model::process::Process>(&url)
                .expect("deserializer");
        let parsed = deserializer
            .deserialize(&toml, Some(&url))
            .expect("roundtrip parse failed");
        if let crate::model::process::Process::FunctionProcess(f) = parsed {
            assert_eq!(f.name, "roundtrip");
            assert_eq!(f.source, "roundtrip.rs");
            assert_eq!(f.inputs.len(), 1);
            assert_eq!(f.outputs.len(), 1);
        } else {
            panic!("Expected FunctionProcess");
        }
    }

    #[test]
    fn escape_special_chars() {
        assert_eq!(escape_toml_string("hello"), "hello");
        assert_eq!(escape_toml_string("a\"b"), "a\\\"b");
        assert_eq!(escape_toml_string("a\\b"), "a\\\\b");
        assert_eq!(escape_toml_string("a\nb"), "a\\nb");
    }

    #[test]
    fn value_to_toml_types() {
        assert_eq!(value_to_toml(&serde_json::json!("hello")), "\"hello\"");
        assert_eq!(value_to_toml(&serde_json::json!(42)), "42");
        assert_eq!(value_to_toml(&serde_json::json!(true)), "true");
        assert_eq!(value_to_toml(&serde_json::json!(null)), "\"null\"");
        assert_eq!(value_to_toml(&serde_json::json!([1, 2])), "[1, 2]");
    }

    #[test]
    fn initializer_to_toml_once() {
        let init = InputInitializer::Once(serde_json::json!(42));
        assert_eq!(initializer_to_toml(&init), "{ once = 42 }");
    }

    #[test]
    fn initializer_to_toml_always() {
        let init = InputInitializer::Always(serde_json::json!("hello"));
        assert_eq!(initializer_to_toml(&init), "{ always = \"hello\" }");
    }
}