use core::{any::TypeId, marker::PhantomData, ops::ControlFlow};
use crate::{
any::{static_wrapper::OwnedStatic, LtTypeId},
any_trait,
effect::{Effect, Future},
never::Never,
protocol::{
self,
visitor::{
request_hint::RequestHint,
tag::{Dyn, Tag, DynTag},
value::Value, sequence::{DynSequence, Sequence, SequenceFlow},
},
},
DynWalker, Flow,
};
pub struct Visitor<E>(usize, PhantomData<fn() -> E>);
any_trait! {
impl['a, 'ctx, E] Visitor<E> = [
dyn RequestHint<'ctx, Effect = E> + 'a,
DynTag<'a, 'ctx, Dyn, E>,
dyn Value<'a, 'ctx, OwnedStatic<&'static str>, Effect = E> + 'a,
dyn Value<'a, 'ctx, OwnedStatic<TypeId>, Effect = E> + 'a,
dyn Value<'a, 'ctx, OwnedStatic<usize>, Effect = E> + 'a,
DynSequence<'a, 'ctx, E>,
] else fallback where E: Effect<'ctx>
}
fn fallback(id: LtTypeId<'_>) {
println!("Unknown trait: {}", id);
}
impl<'ctx, E: Effect<'ctx>> Visitor<E> {
pub fn new() -> Self {
Self(0, PhantomData)
}
fn tab(&self) {
for _ in 0..self.0 {
print!(" ");
}
}
}
impl<'ctx, E: Effect<'ctx>> RequestHint<'ctx> for Visitor<E> {
type Effect = E;
fn request_hint<'a>(
&'a mut self,
_walker: crate::protocol::Walker<'a, 'ctx>,
) -> Future<'a, 'ctx, ControlFlow<(), ()>, Self::Effect>
where
Self: 'a,
{
self.tab();
println!("Visit request hint (no hint given)");
E::wrap(async { ControlFlow::Continue(()) })
}
}
impl<'ctx, E: Effect<'ctx>> Tag<'ctx, Dyn> for Visitor<E> {
type Effect = E;
fn visit<'a>(
&'a mut self,
kind: Dyn,
walker: DynWalker<'a, 'ctx, Self::Effect>,
) -> Future<'a, 'ctx, Flow<Never>, Self::Effect>
where
Self: 'a,
{
self.tab();
println!("Visit tag: {}", kind.0);
E::wrap(async {
self.0 += 1;
let result = walker.walk(self).await;
self.0 -= 1;
match result {
ControlFlow::Continue(()) => Flow::Continue,
ControlFlow::Break(()) => Flow::Break,
}
})
}
}
impl<'a, 'ctx: 'a, E: Effect<'ctx>> Value<'a, 'ctx, OwnedStatic<&'static str>> for Visitor<E> {
type Effect = E;
fn visit(
&'a mut self,
OwnedStatic(value): OwnedStatic<&'static str>,
) -> Future<'a, 'ctx, ControlFlow<(), ()>, Self::Effect>
where
Self: 'a,
{
self.tab();
println!("Visit static str: {:?}", value);
E::wrap(async { ControlFlow::Continue(()) })
}
}
impl<'a, 'ctx: 'a, E: Effect<'ctx>> Value<'a, 'ctx, OwnedStatic<usize>> for Visitor<E> {
type Effect = E;
fn visit(
&'a mut self,
OwnedStatic(value): OwnedStatic<usize>,
) -> Future<'a, 'ctx, ControlFlow<(), ()>, Self::Effect>
where
Self: 'a,
{
self.tab();
println!("Visit usize: {}", value);
E::wrap(async { ControlFlow::Continue(()) })
}
}
impl<'a, 'ctx: 'a, E: Effect<'ctx>> Value<'a, 'ctx, OwnedStatic<TypeId>> for Visitor<E> {
type Effect = E;
fn visit(
&'a mut self,
OwnedStatic(value): OwnedStatic<TypeId>,
) -> Future<'a, 'ctx, ControlFlow<(), ()>, Self::Effect>
where
Self: 'a,
{
self.tab();
println!("Visit type ID: {:?}", value);
E::wrap(async { ControlFlow::Continue(()) })
}
}
impl<'ctx, E: Effect<'ctx>> Sequence<'ctx> for Visitor<E> {
type Effect = E;
fn visit<'a>(
&'a mut self,
scope: protocol::visitor::sequence::DynSequenceScope<'a, 'ctx, Self::Effect>,
) -> Future<'a, 'ctx, Flow<Never>, Self::Effect> {
self.tab();
E::wrap(async {
println!("Visit sequence, size hint: {:?}", scope.size_hint().await);
self.0 += 1;
let flow = loop {
self.tab();
println!("Calling next");
self.0 += 1;
match scope.next(self).await {
SequenceFlow::Done => {
self.tab();
println!("Sequence done");
break Flow::Continue
},
SequenceFlow::Continue => {},
SequenceFlow::Break => break Flow::Break,
}
self.0 -= 1;
};
self.0 -= 2;
flow
})
}
}