79 lines
1.6 KiB
Rust
79 lines
1.6 KiB
Rust
use crate::{GraphError, GraphResult};
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct Node {
|
|
pub kind: NodeKind,
|
|
pub pos: glam::Vec2,
|
|
pub inputs: Vec<Option<SlotIndex>>,
|
|
}
|
|
|
|
impl Node {
|
|
pub fn unwrap_inputs(&self) -> GraphResult<Vec<SlotIndex>> {
|
|
let mut inputs = Vec::with_capacity(self.inputs.len());
|
|
|
|
for input in self.inputs.iter() {
|
|
if let Some(input) = input {
|
|
inputs.push(*input);
|
|
} else {
|
|
return Err(GraphError::MissingEdge);
|
|
}
|
|
}
|
|
|
|
Ok(inputs)
|
|
}
|
|
|
|
/// Temporary helper function for writing tests
|
|
pub fn literal(val: f32) -> Self {
|
|
Self {
|
|
kind: NodeKind::Lit(val),
|
|
pos: glam::Vec2::ZERO,
|
|
inputs: vec![],
|
|
}
|
|
}
|
|
|
|
/// Temporary helper function for writing tests
|
|
pub fn op(op: AtomOp, lhs: usize, rhs: usize) -> Self {
|
|
let lhs = SlotIndex { node: lhs, slot: 0 };
|
|
let rhs = SlotIndex { node: rhs, slot: 0 };
|
|
|
|
Self {
|
|
kind: NodeKind::Op(op),
|
|
pos: glam::Vec2::ZERO,
|
|
inputs: vec![Some(lhs), Some(rhs)],
|
|
}
|
|
}
|
|
|
|
pub fn at(self, pos: glam::Vec2) -> Self {
|
|
Self {
|
|
pos,
|
|
..self
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Copy, Debug, Clone)]
|
|
pub enum NodeKind {
|
|
Lit(f32),
|
|
Op(AtomOp),
|
|
}
|
|
|
|
#[derive(Copy, Debug, Clone)]
|
|
pub enum AtomOp {
|
|
Add,
|
|
Sub,
|
|
Mul,
|
|
Div,
|
|
}
|
|
|
|
#[derive(Copy, Debug, Clone, Hash, PartialEq, Eq)]
|
|
pub struct SlotIndex {
|
|
pub node: usize,
|
|
pub slot: usize,
|
|
}
|
|
|
|
#[derive(Copy, Debug, Clone, Hash, PartialEq, Eq)]
|
|
pub struct Edge {
|
|
pub input: SlotIndex,
|
|
pub output: SlotIndex,
|
|
}
|