openquiver-graph 0.1.0

OpenQuiver graph IR: nodes, pins, types. Bottom layer, no internal dependencies.
Documentation
//! Wires between pins.

use serde::{Deserialize, Serialize};

use crate::NodeName;

/// The kind of wire connecting two pins.
///
/// Separating exec from data is the central idea of Blueprints and the main
/// thing setting this system apart from dataflow builders: step order is stated
/// explicitly rather than inferred from who passed a value to whom.
///
/// Variant order is significant. It defines the canonical edge order in a file,
/// where the exec block precedes the data block.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum EdgeKind {
    Exec,
    Data,
}

/// A reference to one pin of one node, as in `ask.text`.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct PinRef {
    pub(crate) node: NodeName,
    pub(crate) pin: String,
}

impl PinRef {
    pub fn new(node: NodeName, pin: impl Into<String>) -> Self {
        Self {
            node,
            pin: pin.into(),
        }
    }

    pub fn node(&self) -> &str {
        self.node.as_str()
    }

    pub fn pin(&self) -> &str {
        &self.pin
    }
}

/// A wire between two pins.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Edge {
    pub(crate) kind: EdgeKind,
    from: PinRef,
    to: PinRef,
}

impl Edge {
    pub fn new(kind: EdgeKind, from: PinRef, to: PinRef) -> Self {
        Self { kind, from, to }
    }

    pub fn kind(&self) -> EdgeKind {
        self.kind
    }

    pub fn from(&self) -> &PinRef {
        &self.from
    }

    pub fn to(&self) -> &PinRef {
        &self.to
    }
}