pub trait TransformNode<Input, Output, Error>: Send + Sync{
// Required methods
fn prep<'life0, 'async_trait>(
&'life0 self,
input: Input,
) -> Pin<Box<dyn Future<Output = Result<Input, Error>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn exec<'life0, 'async_trait>(
&'life0 self,
input: Input,
) -> Pin<Box<dyn Future<Output = Result<Output, Error>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn post<'life0, 'async_trait>(
&'life0 self,
output: Output,
) -> Pin<Box<dyn Future<Output = Result<Output, Error>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
}
Expand description
A simplified transform node trait for functional data transformations
The TransformNode
trait provides a simplified interface for creating nodes
that follow a functional transformation pattern with explicit input and output types.
Unlike the more general LifecycleNode
, which operates on a shared context,
a TransformNode
transforms data directly from input to output.
Each TransformNode
implements a three-phase lifecycle:
prep
: Validates and prepares the input dataexec
: Performs the main transformation from input to outputpost
: Post-processes the output data
§Benefits of TransformNode
- Simpler API focusing on data transformation
- Direct error types specific to the node (vs. generic FloxideError)
- Functional programming style with explicit input/output
- Easier to compose and reason about
§Example
use async_trait::async_trait;
use floxide_transform::TransformNode;
use std::error::Error;
// Custom error type
#[derive(Debug)]
struct MyError(String);
impl std::fmt::Display for MyError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl Error for MyError {}
// A transform node that converts strings to uppercase
struct UppercaseTransformer;
#[async_trait]
impl TransformNode<String, String, MyError> for UppercaseTransformer {
async fn prep(&self, input: String) -> Result<String, MyError> {
if input.trim().is_empty() {
return Err(MyError("Input cannot be empty".to_string()));
}
Ok(input)
}
async fn exec(&self, input: String) -> Result<String, MyError> {
Ok(input.to_uppercase())
}
async fn post(&self, output: String) -> Result<String, MyError> {
Ok(format!("Processed: {}", output))
}
}
Required Methods§
Sourcefn prep<'life0, 'async_trait>(
&'life0 self,
input: Input,
) -> Pin<Box<dyn Future<Output = Result<Input, Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn prep<'life0, 'async_trait>(
&'life0 self,
input: Input,
) -> Pin<Box<dyn Future<Output = Result<Input, Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Preparation phase