tierkreis_core/
lib.rs

1//! Core Tierkreis data structures for [Graph], [Value], [Type], [symbol]s,
2//! [Namespace], and [FunctionDeclaration] with instances thereof for [builtins].
3//!
4//! [Type]: graph::Type
5//! [Graph]: graph::Graph
6//! [Value]: graph::Value
7//! [FunctionDeclaration]: namespace::FunctionDeclaration
8//! [Namespace]: namespace::Namespace
9pub mod builtins;
10pub mod graph;
11pub mod namespace;
12#[allow(missing_docs, clippy::all)]
13pub mod portgraph;
14pub mod prelude;
15pub mod symbol;
16pub mod type_checker;
17
18#[cfg(test)]
19mod tests {
20    use std::error::Error;
21
22    use super::graph::Type;
23    use crate::graph::{Graph, GraphBuilder, NodeIndex};
24    use rstest::rstest;
25
26    /*
27    Construct graph
28                input
29                /    \
30          middle_1   middle_2
31                \     /
32                 output
33    */
34    #[rstest]
35    fn test_creation() -> Result<(), Box<dyn Error>> {
36        let [i, o] = Graph::boundary();
37        let middle_1;
38        let middle_2;
39        let exit;
40        let graph = {
41            let mut builder = GraphBuilder::new();
42
43            let enter = builder.add_node("split")?;
44            exit = builder.add_node("merge")?;
45            middle_1 = builder.add_node("id")?;
46            middle_2 = builder.add_node("id")?;
47            builder.add_edge((i, "enter"), (enter, "in"), None)?;
48            builder.add_edge((enter, "first"), (middle_1, "in"), None)?;
49            builder.add_edge((enter, "second"), (middle_2, "in"), None)?;
50            builder.add_edge((middle_1, "out"), (exit, "first"), Type::Int)?;
51            builder.add_edge((middle_2, "out"), (exit, "second"), None)?;
52            builder.add_edge((exit, "out"), (o, "exit"), None)?;
53            builder.build()?
54        };
55
56        assert_eq!(graph.node_inputs(i).count(), 0);
57        assert_eq!(graph.node_outputs(i).count(), 1);
58        assert_eq!(graph.node_inputs(o).count(), 1);
59        assert_eq!(graph.node_outputs(o).count(), 0);
60
61        assert_eq!(graph.nodes().count(), 6);
62        assert_eq!(graph.edges().count(), 6);
63
64        assert_eq!(
65            graph.node_output((middle_1, "out")),
66            graph.node_input((exit, "first"))
67        );
68
69        assert_eq!(graph.node(NodeIndex::end()), None);
70        assert_eq!(graph.node_inputs(NodeIndex::end()).count(), 0);
71        assert_eq!(graph.node_outputs(NodeIndex::end()).count(), 0);
72        assert_eq!(graph.node_input((NodeIndex::end(), "port")), None);
73        assert_eq!(graph.node_output((NodeIndex::end(), "port")), None);
74
75        Ok(())
76    }
77}