fdg_sim/
lib.rs

1#![doc = include_str!("../README.md")]
2#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
3
4/// Change forces that define how your graph behaves.
5pub mod force;
6
7mod graph;
8mod simulation;
9
10#[cfg(feature = "json")]
11/// Import and export graphs with the [jsongraph](http://jsongraphformat.info/) specification.
12pub mod json;
13
14#[cfg(feature = "gml")]
15/// Import and export graphs with [Graph Modelling Language](https://en.wikipedia.org/wiki/Graph_Modelling_Language) (GML).
16pub mod gml;
17
18/// Exports graphs into the [DOT](https://en.wikipedia.org/wiki/DOT_(graph_description_language)) language for use with visualizers like [Graphviz](https://graphviz.org/).
19pub mod dot;
20
21pub use glam;
22pub use petgraph;
23
24pub use {
25    graph::{ForceGraph, ForceGraphHelper},
26    simulation::{Dimensions, Node, Simulation, SimulationParameters},
27};
28
29mod tests {
30    #[test]
31    fn dot() {
32        use super::{dot, ForceGraph, ForceGraphHelper};
33
34        const RESULT: &str = "graph {\n    0 [ label = \"one\" ]\n    1 [ label = \"two\" ]\n    2 [ label = \"three\" ]\n    0 -- 1 [ ]\n    1 -- 2 [ ]\n}\n";
35
36        let mut graph: ForceGraph<(), ()> = ForceGraph::default();
37        let one = graph.add_force_node("one", ());
38        let two = graph.add_force_node("two", ());
39        let three = graph.add_force_node("three", ());
40
41        graph.add_edge(one, two, ());
42        graph.add_edge(two, three, ());
43
44        let dot = dot::graph_to_dot(&graph).unwrap();
45
46        assert_eq!(dot, RESULT);
47    }
48
49    #[test]
50    #[cfg(feature = "gml")]
51    fn gml() {
52        use super::{gml, ForceGraph, ForceGraphHelper};
53
54        let mut graph: ForceGraph<(), ()> = ForceGraph::default();
55        let one = graph.add_force_node("one", ());
56        let two = graph.add_force_node("two", ());
57        let three = graph.add_force_node("three", ());
58
59        graph.add_edge(one, two, ());
60        graph.add_edge(two, three, ());
61
62        let gml = gml::graph_to_gml(&graph);
63        let new_graph = gml::graph_from_gml(gml).unwrap();
64
65        assert_eq!(new_graph.node_count(), 3);
66        assert_eq!(new_graph.edge_count(), 2);
67
68        assert!(new_graph
69            .node_weights()
70            .find(|x| x.name == "one".to_string())
71            .is_some());
72        assert!(new_graph
73            .node_weights()
74            .find(|x| x.name == "two".to_string())
75            .is_some());
76        assert!(new_graph
77            .node_weights()
78            .find(|x| x.name == "three".to_string())
79            .is_some());
80    }
81
82    #[test]
83    #[cfg(feature = "json")]
84    fn json() {
85        use super::{json, ForceGraph, ForceGraphHelper};
86
87        let mut graph: ForceGraph<&str, &str> = ForceGraph::default();
88        let one = graph.add_force_node("one", "onedata");
89        let two = graph.add_force_node("two", "twodata");
90        let three = graph.add_force_node("three", "threedata");
91
92        graph.add_edge(one, two, "onetwoedgedata");
93        graph.add_edge(two, three, "twothreeedgedata");
94
95        let json = json::graph_to_json(&graph).unwrap();
96
97        let ag = json::graph_from_json(json.to_string()).unwrap();
98
99        assert_eq!(ag.node_count(), 3);
100        assert_eq!(ag.edge_count(), 2);
101
102        assert_eq!(
103            ag.node_weights()
104                .find(|x| x.name == "one")
105                .unwrap()
106                .data
107                .get("metadata")
108                .unwrap()
109                .to_string()
110                .replace('"', ""),
111            "onedata".to_string()
112        );
113
114        assert!(ag
115            .edge_weights()
116            .find(|x| x.to_string().replace('"', "") == "onetwoedgedata".to_string())
117            .is_some());
118    }
119}