Skip to content

Commit

Permalink
feat(graph): Add basic graph structure and begin filling out graph
Browse files Browse the repository at this point in the history
methods.
  • Loading branch information
TheGoodall committed Mar 6, 2023
1 parent c03017a commit 67c0535
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 2 deletions.
64 changes: 64 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Expand Up @@ -5,3 +5,6 @@ edition = "2021"


[dependencies]
anyhow = "1.0.69"
petgraph = { version = "0.6.3", features = ["stable_graph"] }
smol_str = "0.1.24"
83 changes: 83 additions & 0 deletions 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<PortID, Port>,
}

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<Node, Edge, Directed>);

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<Node> {
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<EdgeIndex> {
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();
}
}
1 change: 1 addition & 0 deletions src/lib.rs
@@ -0,0 +1 @@
pub mod graph;
7 changes: 5 additions & 2 deletions src/main.rs
@@ -1,3 +1,6 @@
fn main() {
println!("Hello, world!");
use chromic::graph;

fn main() -> anyhow::Result<()> {
let _ = graph::Graph::new();
Ok(())
}

0 comments on commit 67c0535

Please sign in to comment.