Skip to main content

ranvier_std/nodes/
string.rs

1use async_trait::async_trait;
2use ranvier_core::{bus::Bus, outcome::Outcome, transition::Transition};
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5use std::fmt::Debug;
6
7#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq)]
8pub enum StringOperation {
9    Append(String),
10    Prepend(String),
11    ToUpper,
12    ToLower,
13}
14
15#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
16pub struct StringNode {
17    pub operation: StringOperation,
18}
19
20impl StringNode {
21    pub fn new(operation: StringOperation) -> Self {
22        Self { operation }
23    }
24}
25
26#[async_trait]
27impl Transition<String, String> for StringNode {
28    type Error = String;
29    type Resources = ();
30
31    async fn run(
32        &self,
33        input: String,
34        _resources: &Self::Resources,
35        _bus: &mut Bus,
36    ) -> Outcome<String, Self::Error> {
37        match &self.operation {
38            StringOperation::Append(s) => Outcome::next(format!("{}{}", input, s)),
39            StringOperation::Prepend(s) => Outcome::next(format!("{}{}", s, input)),
40            StringOperation::ToUpper => Outcome::next(input.to_uppercase()),
41            StringOperation::ToLower => Outcome::next(input.to_lowercase()),
42        }
43    }
44}