From fdb2a8621dbb08f8cb5a28ee6aaafe07bdbb5d77 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Wed, 23 Dec 2020 09:00:04 +0000 Subject: [rust] DOT DSL --- dot-dsl/src/lib.rs | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 dot-dsl/src/lib.rs (limited to 'dot-dsl/src/lib.rs') diff --git a/dot-dsl/src/lib.rs b/dot-dsl/src/lib.rs new file mode 100644 index 0000000..654ee09 --- /dev/null +++ b/dot-dsl/src/lib.rs @@ -0,0 +1,119 @@ +pub mod graph { + + use graph_items::edge::Edge; + use graph_items::node::Node; + use std::collections::HashMap; + + pub mod graph_items { + + pub mod node { + + use std::collections::HashMap; + + #[derive(Default, Debug, PartialEq, Eq, Clone)] + pub struct Node<'a> { + pub name: &'a str, + attrs: HashMap, + } + + impl<'a> Node<'a> { + pub fn new(name: &'a str) -> Node<'a> { + Node { + name, + ..Default::default() + } + } + + pub fn with_attrs(mut self, attrs: &[(&'a str, &'a str)]) -> Node<'a> { + for (key, value) in attrs { + self.attrs.insert(key.to_string(), value.to_string()); + } + self + } + + pub fn get_attr(&self, key: &str) -> Option<&str> { + self.attrs.get(key).map(|s| &s[..]) + } + } + } + + pub mod edge { + + use std::collections::HashMap; + + #[derive(Default, Debug, PartialEq, Eq, Clone)] + pub struct Edge<'a> { + pub x: &'a str, + pub y: &'a str, + attrs: HashMap, + } + + impl<'a> Edge<'a> { + pub fn new(x: &'a str, y: &'a str) -> Edge<'a> { + Edge { + x, + y, + ..Default::default() + } + } + + pub fn with_attrs(mut self, attrs: &[(&'a str, &'a str)]) -> Edge<'a> { + for (key, value) in attrs { + self.attrs.insert(key.to_string(), value.to_string()); + } + self + } + + pub fn get_attr(&self, key: &str) -> Option<&str> { + self.attrs.get(key).map(|s| &s[..]) + } + } + } + } + + #[derive(Default, Debug)] + pub struct Graph<'a> { + pub nodes: Vec>, + pub edges: Vec>, + pub attrs: HashMap, + } + + impl<'a> Graph<'a> { + pub fn new() -> Self { + Default::default() + } + + pub fn with_attrs(mut self, attrs: &[(&'a str, &'a str)]) -> Graph<'a> { + for (key, value) in attrs { + self.attrs.insert(key.to_string(), value.to_string()); + } + self + } + + pub fn with_nodes(mut self, nodes: &[Node<'a>]) -> Graph<'a> { + for node in nodes { + self.nodes.push(node.clone()); + } + self + } + + pub fn with_edges(mut self, edges: &[Edge<'a>]) -> Graph<'a> { + for edge in edges { + self.edges.push(edge.clone()) + } + self + } + + pub fn get_node(&self, name: &str) -> Option<&Node<'a>> { + self.nodes.iter().find(|&node| node.name == name) + } + + pub fn get_edge(&self, x: &str, y: &str) -> Option<&Edge<'a>> { + self.edges.iter().find(|&edge| edge.x == x && edge.y == y) + } + + pub fn get_attr(&self, key: &str) -> Option<&str> { + self.attrs.get(key).map(|s| &s[..]) + } + } +} -- cgit v1.2.3