From 67c0535bbfc814391c1dd2c8f6e88bd996c7e623 Mon Sep 17 00:00:00 2001 From: James Goodall Date: Mon, 6 Mar 2023 00:22:59 +0000 Subject: [PATCH] feat(graph): Add basic graph structure and begin filling out graph methods. --- Cargo.lock | 64 ++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 ++ src/graph.rs | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + src/main.rs | 7 +++-- 5 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 src/graph.rs create mode 100644 src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index c90f9cd..7134467 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,70 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "anyhow" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224afbd727c3d6e4b90103ece64b8d1b67fbb1973b1046c2281eed3f3803f800" + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + [[package]] name = "chromic" version = "0.0.1" +dependencies = [ + "anyhow", + "petgraph", + "smol_str", +] + +[[package]] +name = "fixedbitset" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "indexmap" +version = "1.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "petgraph" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dd7d28ee937e54fe3080c91faa1c3a46c06de6252988a7f4592ba2310ef22a4" +dependencies = [ + "fixedbitset", + "indexmap", +] + +[[package]] +name = "serde" +version = "1.0.152" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" + +[[package]] +name = "smol_str" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fad6c857cbab2627dcf01ec85a623ca4e7dcb5691cbaa3d7fb7653671f0d09c9" +dependencies = [ + "serde", +] diff --git a/Cargo.toml b/Cargo.toml index b758b32..4c10f27 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,6 @@ edition = "2021" [dependencies] +anyhow = "1.0.69" +petgraph = { version = "0.6.3", features = ["stable_graph"] } +smol_str = "0.1.24" diff --git a/src/graph.rs b/src/graph.rs new file mode 100644 index 0000000..bd19848 --- /dev/null +++ b/src/graph.rs @@ -0,0 +1,83 @@ +use petgraph::{ + stable_graph::{EdgeIndex, NodeIndex, StableGraph}, + Directed, +}; +use std::collections::HashMap; + +use smol_str::SmolStr; + +#[derive(Clone, Eq, PartialEq, Hash)] +pub struct PortID { + pub id: SmolStr, + pub direction: PortDirection, +} + +#[derive(Clone, Eq, PartialEq, Hash)] +pub enum PortDirection { + Input, + Output, +} +pub struct Port {} + +pub struct Node { + pub ports: HashMap, +} + +impl Node { + pub fn has_port(&self, id: &PortID) -> bool { + self.ports.contains_key(id) + } +} + +pub struct Edge(pub PortID, pub PortID); + +pub struct Graph(StableGraph); + +impl Graph { + pub fn new() -> Self { + Self(StableGraph::new()) + } +} + +impl Default for Graph { + fn default() -> Self { + Self::new() + } +} + +impl Graph { + pub fn add_node(&mut self, node: Node) -> NodeIndex { + self.0.add_node(node) + } + + pub fn remove_node(&mut self, i: NodeIndex) -> Option { + self.0.remove_node(i) + } + + pub fn get_node(&self, i: NodeIndex) -> Option<&Node> { + self.0.node_weight(i) + } + + pub fn add_edge( + &mut self, + a: NodeIndex, + b: NodeIndex, + edge: Edge, + ) -> anyhow::Result { + match (self.get_node(a), self.get_node(b)) { + (None, _) => anyhow::bail!("Node A doesn't exist"), + (_, None) => anyhow::bail!("Node B doesn't exist"), + (Some(node_a), Some(node_b)) => { + match (node_a.has_port(&edge.0), node_b.has_port(&edge.1)) { + (false, _) => anyhow::bail!("Output port doesn't exist"), + (_, false) => anyhow::bail!("input port doesn't exist"), + (true, true) => Ok(self.0.add_edge(a, b, edge)), + } + } + } + } + + pub fn clear_edges(&mut self) { + self.0.clear_edges(); + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..6f94350 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod graph; diff --git a/src/main.rs b/src/main.rs index e7a11a9..484de26 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ -fn main() { - println!("Hello, world!"); +use chromic::graph; + +fn main() -> anyhow::Result<()> { + let _ = graph::Graph::new(); + Ok(()) }