Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(graph): Add basic graph structure and begin filling out graph
methods.
- Loading branch information
1 parent
c03017a
commit 67c0535
Showing
5 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod graph; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
use chromic::graph; | ||
|
||
fn main() -> anyhow::Result<()> { | ||
let _ = graph::Graph::new(); | ||
Ok(()) | ||
} |