use flowly::{Context, ServiceExt, switch};
use flowly_service::{Service, flow};
use futures::StreamExt;
pub struct Service1;
impl Service<i32> for Service1 {
type Out = Result<u64, Error>;
fn handle(&self, item: i32, _cx: &Context) -> impl futures::Stream<Item = Self::Out> {
async_stream::try_stream! {
yield item as u64;
}
}
}
pub struct Service2;
impl Service<i32> for Service2 {
type Out = Result<u64, Error>;
fn handle(&self, item: i32, _cx: &Context) -> impl futures::Stream<Item = Self::Out> {
async_stream::try_stream! {
yield item as u64 + 100;
}
}
}
#[derive(Debug)]
pub enum Error {}
pub struct Service3;
impl Service<i32> for Service3 {
type Out = Result<u64, Error>;
fn handle(&self, item: i32, _cx: &Context) -> impl futures::Stream<Item = Self::Out> {
async_stream::try_stream! {
yield item as u64 * 100;
}
}
}
#[tokio::main]
async fn main() {
let x = flow::<_, Error>() .flow(
switch::<i32, Result<u64, Error>, _, _>(|x| x % 4)
.case(0, Service1)
.case([1, 2], Service2)
.default(Service3),
);
let cx = flowly_service::Context::new();
let y = x.handle_stream(
futures::stream::iter([0, 1i32, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
&cx,
);
println!("{:?}", y.collect::<Vec<_>>().await);
}