use crate::error::SpecError;
use async_trait::async_trait;
#[async_trait]
pub trait Decomposer<Input, Output>: Send + Sync
where
Input: Send + Sync,
Output: Send + Sync,
{
async fn decompose(&self, input: &Input) -> Result<Vec<Output>, SpecError>;
}
#[cfg(test)]
mod tests {
use super::*;
const _: fn() = || {
let _: &dyn Decomposer<String, String>;
};
struct ToyDecomposer;
#[async_trait]
impl Decomposer<String, String> for ToyDecomposer {
async fn decompose(&self, input: &String) -> Result<Vec<String>, SpecError> {
Ok(input.split_whitespace().map(str::to_owned).collect())
}
}
#[tokio::test]
async fn toy_decomposer_splits_words() {
let d = ToyDecomposer;
let out = d.decompose(&"hello world".to_string()).await.unwrap();
assert_eq!(out, vec!["hello", "world"]);
}
}