use core::marker::PhantomData;
use smol_bytes::Utf8Bytes;
use super::{DriverKind, EdgeId, FanoutKind, NodeId};
use crate::{
drive::{Declared, Streams},
operator::Driven,
};
pub struct NodeHandle<O> {
id: NodeId,
_op: PhantomData<fn() -> O>,
}
impl<O> NodeHandle<O> {
#[inline]
pub(crate) const fn new(id: NodeId) -> Self {
Self {
id,
_op: PhantomData,
}
}
#[inline]
pub const fn id(&self) -> NodeId {
self.id
}
}
impl<O> Clone for NodeHandle<O> {
#[inline]
fn clone(&self) -> Self {
*self
}
}
impl<O> Copy for NodeHandle<O> {}
#[derive(Debug, Clone)]
pub struct Node {
name: Utf8Bytes,
driver: DriverKind,
is_source: bool,
}
impl Node {
#[inline]
pub fn name(&self) -> &str {
&self.name
}
#[inline]
pub const fn driver(&self) -> DriverKind {
self.driver
}
#[inline]
pub const fn is_source(&self) -> bool {
self.is_source
}
}
#[derive(Debug, Clone, Copy)]
pub struct Edge {
from: NodeId,
to: NodeId,
kind: FanoutKind,
}
impl Edge {
#[inline]
pub const fn from(&self) -> NodeId {
self.from
}
#[inline]
pub const fn to(&self) -> NodeId {
self.to
}
#[inline]
pub const fn kind(&self) -> FanoutKind {
self.kind
}
}
pub struct GraphBuilder {
nodes: Vec<Node>,
edges: Vec<Edge>,
}
impl GraphBuilder {
#[inline]
pub const fn new() -> Self {
Self {
nodes: Vec::new(),
edges: Vec::new(),
}
}
#[inline]
pub fn node<O>(&mut self, name: impl Into<Utf8Bytes>, driver: DriverKind) -> NodeHandle<O>
where
O: Declared + Driven<<O as Declared>::Mode>,
{
self.declare(name.into(), driver, false)
}
#[inline]
pub fn source<O>(&mut self, name: impl Into<Utf8Bytes>, driver: DriverKind) -> NodeHandle<O>
where
O: Declared + Driven<<O as Declared>::Mode>,
{
self.declare(name.into(), driver, true)
}
#[inline]
pub fn streaming<O>(&mut self, name: impl Into<Utf8Bytes>, driver: DriverKind) -> NodeHandle<O>
where
O: Declared + Streams<<O as Declared>::Mode>,
{
self.declare(name.into(), driver, false)
}
#[inline]
pub fn streaming_source<O>(
&mut self,
name: impl Into<Utf8Bytes>,
driver: DriverKind,
) -> NodeHandle<O>
where
O: Declared + Streams<<O as Declared>::Mode>,
{
self.declare(name.into(), driver, true)
}
#[inline]
pub fn autonomous<O>(&mut self, name: impl Into<Utf8Bytes>, driver: DriverKind) -> NodeHandle<O>
where
O: Declared + crate::Runs<<O as Declared>::Mode>,
{
self.declare(name.into(), driver, false)
}
#[inline]
pub fn autonomous_source<O>(
&mut self,
name: impl Into<Utf8Bytes>,
driver: DriverKind,
) -> NodeHandle<O>
where
O: Declared + crate::Runs<<O as Declared>::Mode>,
{
self.declare(name.into(), driver, true)
}
#[inline]
pub fn origin<S>(&mut self, name: impl Into<Utf8Bytes>, driver: DriverKind) -> NodeHandle<S>
where
S: crate::DeclaredOrigin + crate::Lived<<S as crate::DeclaredOrigin>::Mode>,
{
self.declare(name.into(), driver, true)
}
fn declare<O>(&mut self, name: Utf8Bytes, driver: DriverKind, is_source: bool) -> NodeHandle<O> {
let id = NodeId::new(self.nodes.len());
self.nodes.push(Node {
name,
driver,
is_source,
});
NodeHandle::new(id)
}
#[inline]
pub fn edge<U, D, M, N>(
&mut self,
from: &NodeHandle<U>,
to: &NodeHandle<D>,
kind: FanoutKind,
) -> EdgeId
where
U: Driven<M>,
D: Driven<N, In = <U as Driven<M>>::Out>,
{
let id = EdgeId::new(self.edges.len());
self.edges.push(Edge {
from: from.id(),
to: to.id(),
kind,
});
id
}
#[inline]
pub fn origin_edge<S, D, N>(
&mut self,
from: &NodeHandle<S>,
to: &NodeHandle<D>,
kind: FanoutKind,
) -> EdgeId
where
S: crate::DeclaredOrigin + crate::Lived<<S as crate::DeclaredOrigin>::Mode>,
D: Driven<N, In = crate::Produced<S>>,
{
let id = EdgeId::new(self.edges.len());
self.edges.push(Edge {
from: from.id(),
to: to.id(),
kind,
});
id
}
#[inline]
pub(crate) fn join(&mut self, from: NodeId, to: NodeId, kind: FanoutKind) -> EdgeId {
let id = EdgeId::new(self.edges.len());
self.edges.push(Edge { from, to, kind });
id
}
#[inline]
pub(crate) fn into_parts(self) -> (Vec<Node>, Vec<Edge>) {
(self.nodes, self.edges)
}
}
impl Default for GraphBuilder {
#[inline]
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests;