Skip to content

Commit

Permalink
Use enum_dispatch to autogen Node enum
Browse files Browse the repository at this point in the history
  • Loading branch information
m00nwtchr committed Jul 24, 2024
1 parent 3b35672 commit 84c8933
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 180 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ anyhow = "1"

strum = { version = "0.26", features = ["derive"] }
derive_more = "0.99"
enum_dispatch = "0.3"
parking_lot = "0.12"
confique = { version = "0.2", default-features = false, features = ["toml"] }

Expand Down
226 changes: 46 additions & 180 deletions src/flow/node.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
#![allow(clippy::module_name_repetitions)]
use std::sync::Arc;

use super::feed::Feed;
#[cfg(feature = "filter")]
use super::filter::Filter;
#[cfg(feature = "html")]
use super::html::Html;
#[cfg(feature = "retrieve")]
use super::retrieve::Retrieve;
#[cfg(feature = "sanitise")]
use super::sanitise::Sanitise;
#[cfg(feature = "wasm")]
use super::wasm::Wasm;
use crate::{
flow::{ai::AI, seen::Seen},
websub::WebSub,
};

use std::{future::Future, pin::Pin, sync::Arc};

Check failure on line 3 in src/flow/node.rs

View workflow job for this annotation

GitHub Actions / all (ubuntu-latest, stable)

unused imports: `future::Future`, `pin::Pin`

Check failure on line 3 in src/flow/node.rs

View workflow job for this annotation

GitHub Actions / all

unused imports: `future::Future`, `pin::Pin`

Check failure on line 3 in src/flow/node.rs

View workflow job for this annotation

GitHub Actions / all (ubuntu-latest, beta)

unused imports: `future::Future` and `pin::Pin`

use anyhow::anyhow;
use async_trait::async_trait;
use bytes::Bytes;
use derive_more::From;
use enum_dispatch::enum_dispatch;
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use strum::{Display, EnumDiscriminants};

use crate::{
flow::{ai::AI, seen::Seen},
websub::WebSub,
};

#[async_trait]
#[enum_dispatch]
pub trait NodeTrait: Sync + Send {
fn inputs(&self) -> &[Arc<IO>];
fn outputs(&self) -> &[Arc<IO>];
Expand Down Expand Up @@ -56,196 +49,69 @@ pub trait NodeTrait: Sync + Send {
}
}

#[derive(Serialize, Deserialize, Display)]
#[serde(tag = "type")]
#[enum_dispatch(NodeTrait)]
pub enum Node {
AI(AI),
Feed(super::feed::Feed),
#[cfg(feature = "filter")]
Filter(super::filter::Filter),
#[cfg(feature = "html")]
Html(super::html::Html),
#[cfg(feature = "retrieve")]
Retrieve(super::retrieve::Retrieve),
#[cfg(feature = "sanitise")]
Sanitise(super::sanitise::Sanitise),
Seen(Seen),
#[cfg(feature = "wasm")]
#[serde(skip)]
Wasm(super::wasm::Wasm),
#[serde(skip)]
Other(Box<dyn NodeTrait>),
}

#[async_trait]
impl NodeTrait for Node {
impl NodeTrait for Box<dyn NodeTrait> {
fn inputs(&self) -> &[Arc<IO>] {
match self {
Self::AI(n) => n.inputs(),
Self::Feed(n) => n.inputs(),
Self::Filter(n) => n.inputs(),
#[cfg(feature = "html")]
Self::Html(n) => n.inputs(),
#[cfg(feature = "retrieve")]
Self::Retrieve(n) => n.inputs(),
#[cfg(feature = "sanitise")]
Self::Sanitise(n) => n.inputs(),
Self::Seen(n) => n.inputs(),
#[cfg(feature = "wasm")]
Self::Wasm(n) => n.inputs(),
Self::Other(n) => n.inputs(),
}
(**self).inputs()
}

fn outputs(&self) -> &[Arc<IO>] {
match self {
Self::AI(n) => n.outputs(),
Self::Feed(n) => n.outputs(),
Self::Filter(n) => n.outputs(),
#[cfg(feature = "html")]
Self::Html(n) => n.outputs(),
#[cfg(feature = "retrieve")]
Self::Retrieve(n) => n.outputs(),
#[cfg(feature = "sanitise")]
Self::Sanitise(n) => n.outputs(),
Self::Seen(n) => n.outputs(),
#[cfg(feature = "wasm")]
Self::Wasm(n) => n.outputs(),
Self::Other(n) => n.outputs(),
}
(**self).outputs()
}

fn input_types(&self) -> &[DataKind] {
match self {
Self::AI(n) => n.input_types(),
Self::Feed(n) => n.input_types(),
Self::Filter(n) => n.input_types(),
#[cfg(feature = "html")]
Self::Html(n) => n.input_types(),
#[cfg(feature = "retrieve")]
Self::Retrieve(n) => n.input_types(),
#[cfg(feature = "sanitise")]
Self::Sanitise(n) => n.input_types(),
Self::Seen(n) => n.input_types(),
#[cfg(feature = "wasm")]
Self::Wasm(n) => n.input_types(),
Self::Other(n) => n.input_types(),
}
(**self).input_types()
}

fn output_types(&self) -> &[DataKind] {
match self {
Self::AI(n) => n.output_types(),

Self::Feed(n) => n.output_types(),
Self::Filter(n) => n.output_types(),
#[cfg(feature = "html")]
Self::Html(n) => n.output_types(),
#[cfg(feature = "retrieve")]
Self::Retrieve(n) => n.output_types(),
#[cfg(feature = "sanitise")]
Self::Sanitise(n) => n.output_types(),
Self::Seen(n) => n.output_types(),
#[cfg(feature = "wasm")]
Self::Wasm(n) => n.output_types(),
Self::Other(n) => n.output_types(),
}
(**self).output_types()
}

fn is_dirty(&self) -> bool {
match self {
Self::AI(n) => n.is_dirty(),

Self::Feed(n) => n.is_dirty(),
Self::Filter(n) => n.is_dirty(),
#[cfg(feature = "html")]
Self::Html(n) => n.is_dirty(),
#[cfg(feature = "retrieve")]
Self::Retrieve(n) => n.is_dirty(),
#[cfg(feature = "sanitise")]
Self::Sanitise(n) => n.is_dirty(),
Self::Seen(n) => n.is_dirty(),
#[cfg(feature = "wasm")]
Self::Wasm(n) => n.is_dirty(),
Self::Other(n) => n.is_dirty(),
}
(**self).is_dirty()
}

async fn run(&self) -> anyhow::Result<()> {
match self {
Self::AI(n) => n.run().await,

Self::Feed(n) => n.run().await,
Self::Filter(n) => n.run().await,
#[cfg(feature = "html")]
Self::Html(n) => n.run().await,
#[cfg(feature = "retrieve")]
Self::Retrieve(n) => n.run().await,
#[cfg(feature = "sanitise")]
Self::Sanitise(n) => n.run().await,
Self::Seen(n) => n.run().await,
#[cfg(feature = "wasm")]
Self::Wasm(n) => n.run().await,
Self::Other(n) => n.run().await,
}
(**self).run().await
}

fn set_input(&mut self, index: usize, input: Arc<IO>) {
match self {
Self::AI(n) => n.set_input(index, input),

Self::Feed(n) => n.set_input(index, input),
Self::Filter(n) => n.set_input(index, input),
#[cfg(feature = "html")]
Self::Html(n) => n.set_input(index, input),
#[cfg(feature = "retrieve")]
Self::Retrieve(n) => n.set_input(index, input),
#[cfg(feature = "sanitise")]
Self::Sanitise(n) => n.set_input(index, input),
Self::Seen(n) => n.set_input(index, input),
#[cfg(feature = "wasm")]
Self::Wasm(n) => n.set_input(index, input),
Self::Other(n) => n.set_input(index, input),
}
(**self).set_input(index, input)
}

fn set_output(&mut self, index: usize, output: Arc<IO>) {
match self {
Self::AI(n) => n.set_output(index, output),

Self::Feed(n) => n.set_output(index, output),
Self::Filter(n) => n.set_output(index, output),
#[cfg(feature = "html")]
Self::Html(n) => n.set_output(index, output),
#[cfg(feature = "retrieve")]
Self::Retrieve(n) => n.set_output(index, output),
#[cfg(feature = "sanitise")]
Self::Sanitise(n) => n.set_output(index, output),
Self::Seen(n) => n.set_output(index, output),
#[cfg(feature = "wasm")]
Self::Wasm(n) => n.set_output(index, output),
Self::Other(n) => n.set_output(index, output),
}
(**self).set_output(index, output)
}

fn web_sub(&self) -> Option<WebSub> {
match self {
Self::AI(n) => n.web_sub(),

Self::Feed(n) => n.web_sub(),
Self::Filter(n) => n.web_sub(),
#[cfg(feature = "html")]
Self::Html(n) => n.web_sub(),
#[cfg(feature = "retrieve")]
Self::Retrieve(n) => n.web_sub(),
#[cfg(feature = "sanitise")]
Self::Sanitise(n) => n.web_sub(),
Self::Seen(n) => n.web_sub(),
#[cfg(feature = "wasm")]
Self::Wasm(n) => n.web_sub(),
Self::Other(n) => n.web_sub(),
}
fn connect(&mut self, io: Arc<IO>, port: usize) {
(**self).connect(io, port)
}
}

#[derive(Serialize, Deserialize, From, Display)]
#[serde(tag = "type")]
pub enum Node {
AI(AI),
Feed(Feed),
#[cfg(feature = "filter")]
Filter(Filter),
#[cfg(feature = "html")]
Html(Html),
#[cfg(feature = "retrieve")]
Retrieve(Retrieve),
#[cfg(feature = "sanitise")]
Sanitise(Sanitise),
Seen(Seen),
#[cfg(feature = "wasm")]
#[serde(skip)]
Wasm(Wasm),
#[serde(skip)]
Other(Box<dyn NodeTrait>),
fn web_sub(&self) -> Option<WebSub> {
(**self).web_sub()
}
}

#[derive(EnumDiscriminants, Serialize, Deserialize, Debug, From, Clone, PartialEq)]
Expand Down

0 comments on commit 84c8933

Please sign in to comment.