|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + connection::{in_channel::InChannels, out_channel::OutChannels}, |
| 5 | + utils::{env::EnvVar, output::Output}, |
| 6 | +}; |
| 7 | + |
| 8 | +use super::{ |
| 9 | + action::{Action, EmptyAction}, |
| 10 | + node::{Node, NodeId, NodeName, NodeTable}, |
| 11 | +}; |
| 12 | + |
| 13 | +/// # Default node type |
| 14 | +/// |
| 15 | +/// [`DefaultNode`] is a default implementation of the [`Node`] trait. Users can use this node |
| 16 | +/// type to build tasks to meet most needs. |
| 17 | +/// |
| 18 | +/// ## Create a `DefaultNode`: |
| 19 | +/// - use the method `new`. Required attributes: node's name; [`NodeTable`](for id allocation). |
| 20 | +/// |
| 21 | +/// ```rust |
| 22 | +/// use dagrs::{NodeName, NodeTable, DefaultNode}; |
| 23 | +/// |
| 24 | +/// let node_name = "Node X"; |
| 25 | +/// let mut node_table = NodeTable::new(); |
| 26 | +/// let mut node = DefaultNode::new( |
| 27 | +/// NodeName::from(node_name), |
| 28 | +/// &mut node_table, |
| 29 | +/// ); |
| 30 | +/// ``` |
| 31 | +/// |
| 32 | +/// - use the method `with_action`. Required attributes: node's name; [`NodeTable`](for id allocation); |
| 33 | +/// execution logic [`Action`]. |
| 34 | +/// |
| 35 | +/// ```rust |
| 36 | +/// use dagrs::{NodeName, NodeTable, DefaultNode, EmptyAction}; |
| 37 | +/// |
| 38 | +/// let node_name = "Node X"; |
| 39 | +/// let mut node_table = NodeTable::new(); |
| 40 | +/// let mut node = DefaultNode::with_action( |
| 41 | +/// NodeName::from(node_name), |
| 42 | +/// Box::new(EmptyAction), |
| 43 | +/// &mut node_table, |
| 44 | +/// ); |
| 45 | +/// ``` |
| 46 | +pub struct DefaultNode { |
| 47 | + id: NodeId, |
| 48 | + name: NodeName, |
| 49 | + action: Box<dyn Action>, |
| 50 | + in_channels: InChannels, |
| 51 | + out_channels: OutChannels, |
| 52 | +} |
| 53 | + |
| 54 | +impl Node for DefaultNode { |
| 55 | + fn id(&self) -> NodeId { |
| 56 | + self.id.clone() |
| 57 | + } |
| 58 | + |
| 59 | + fn name(&self) -> NodeName { |
| 60 | + self.name.clone() |
| 61 | + } |
| 62 | + |
| 63 | + fn input_channels(&mut self) -> &mut InChannels { |
| 64 | + &mut self.in_channels |
| 65 | + } |
| 66 | + |
| 67 | + fn output_channels(&mut self) -> &mut OutChannels { |
| 68 | + &mut self.out_channels |
| 69 | + } |
| 70 | + |
| 71 | + fn run(&mut self, env: Arc<EnvVar>) -> Output { |
| 72 | + tokio::runtime::Runtime::new().unwrap().block_on(async { |
| 73 | + self.action |
| 74 | + .run(&mut self.in_channels, &self.out_channels, env) |
| 75 | + .await |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl DefaultNode { |
| 81 | + pub fn new(name: NodeName, node_table: &mut NodeTable) -> Self { |
| 82 | + Self { |
| 83 | + id: node_table.alloc_id_for(&name), |
| 84 | + name, |
| 85 | + action: Box::new(EmptyAction), |
| 86 | + in_channels: InChannels::default(), |
| 87 | + out_channels: OutChannels::default(), |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + pub fn with_action( |
| 92 | + name: NodeName, |
| 93 | + action: Box<dyn Action>, |
| 94 | + node_table: &mut NodeTable, |
| 95 | + ) -> Self { |
| 96 | + Self { |
| 97 | + id: node_table.alloc_id_for(&name), |
| 98 | + name, |
| 99 | + action, |
| 100 | + in_channels: InChannels::default(), |
| 101 | + out_channels: OutChannels::default(), |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +#[cfg(test)] |
| 107 | +mod test_default_node { |
| 108 | + |
| 109 | + use std::sync::Arc; |
| 110 | + |
| 111 | + use crate::{Content, EnvVar, InChannels, Node, NodeName, NodeTable, OutChannels, Output}; |
| 112 | + |
| 113 | + use super::{Action, DefaultNode}; |
| 114 | + |
| 115 | + use async_trait::async_trait; |
| 116 | + |
| 117 | + /// An implementation of [`Action`] that returns [`Output::Out`] containing a String "Hello world". |
| 118 | + #[derive(Default)] |
| 119 | + pub struct HelloAction; |
| 120 | + #[async_trait] |
| 121 | + impl Action for HelloAction { |
| 122 | + async fn run(&self, _: &mut InChannels, _: &OutChannels, _: Arc<EnvVar>) -> Output { |
| 123 | + Output::Out(Some(Content::new("Hello world".to_string()))) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + impl HelloAction { |
| 128 | + pub fn new() -> Box<Self> { |
| 129 | + Box::new(Self::default()) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /// Test for create a default node. |
| 134 | + /// |
| 135 | + /// Step 1: create a [`DefaultNode`] with [`HelloAction`]. |
| 136 | + /// |
| 137 | + /// Step 2: run the node and verify its output. |
| 138 | + #[test] |
| 139 | + fn create_default_node() { |
| 140 | + let node_name = "Test Node"; |
| 141 | + |
| 142 | + let mut node_table = NodeTable::new(); |
| 143 | + let mut node = DefaultNode::with_action( |
| 144 | + NodeName::from(node_name), |
| 145 | + HelloAction::new(), |
| 146 | + &mut node_table, |
| 147 | + ); |
| 148 | + |
| 149 | + // Check if node table has key-value pair (node.name, node.id) |
| 150 | + assert_eq!(node_table.get(node_name).unwrap(), &node.id()); |
| 151 | + |
| 152 | + let env = Arc::new(EnvVar::new(node_table)); |
| 153 | + let out = node.run(env).get_out().unwrap(); |
| 154 | + let out: &String = out.get().unwrap(); |
| 155 | + assert_eq!(out, "Hello world"); |
| 156 | + } |
| 157 | +} |
0 commit comments