ranvier_std/nodes/
math.rs1use async_trait::async_trait;
2use num_traits::Num;
3use ranvier_core::{bus::Bus, outcome::Outcome, transition::Transition};
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6use std::{fmt::Debug, marker::PhantomData};
7
8#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq)]
9pub enum MathOperation {
10 Add,
11 Sub,
12 Mul,
13 Div,
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
17pub struct MathNode<T> {
18 pub operation: MathOperation,
19 pub operand: T,
20 #[serde(skip)]
21 pub _marker: PhantomData<T>,
22}
23
24impl<T> MathNode<T> {
25 pub fn new(operation: MathOperation, operand: T) -> Self {
26 Self {
27 operation,
28 operand,
29 _marker: PhantomData,
30 }
31 }
32}
33
34#[async_trait]
35impl<T> Transition<T, T> for MathNode<T>
36where
37 T: Num + Clone + Send + Sync + Debug + 'static,
38{
39 type Error = std::convert::Infallible;
40 type Resources = ();
41
42 async fn run(
43 &self,
44 input: T,
45 _resources: &Self::Resources,
46 _bus: &mut Bus,
47 ) -> Outcome<T, Self::Error> {
48 let result = match self.operation {
49 MathOperation::Add => input.clone() + self.operand.clone(),
50 MathOperation::Sub => input.clone() - self.operand.clone(),
51 MathOperation::Mul => input.clone() * self.operand.clone(),
52 MathOperation::Div => input.clone() / self.operand.clone(),
53 };
54
55 Outcome::next(result)
56 }
57}