Diffstat (limited to 'tests/demo.rs')
| -rw-r--r-- | tests/demo.rs | 192 |
1 files changed, 0 insertions, 192 deletions
diff --git a/tests/demo.rs b/tests/demo.rs deleted file mode 100644 index b6bf557..0000000 --- a/tests/demo.rs +++ /dev/null @@ -1,192 +0,0 @@ -use std::{collections::VecDeque, ops::ControlFlow}; -use treaty::{ - any::{any_trait, static_wrapper::OwnedStatic}, - build, build_with, - builders::core::array, - effect::{SyncEffect, Yield}, - into_walker, - protocol::{ - visitor::{Sequence, SequenceScope, Value}, - Visitor, - }, - AsVisitor, Builder, Walk, Walker, -}; - -#[test] -fn demo() { - let a = Data::Sequence(vec![ - Data::Bool(true), - Data::Sequence(vec![Data::Bool(false), Data::Bool(true)]), - Data::Bool(false), - ]); - - dbg!(array_to_array2([true, false])); - dbg!(array_to_array([true, true])); - - let s = build_with::<array::Builder<'_, JsonLike, 3, _>, _>(into_walker(&a)).unwrap(); - dbg!(s); - - use treaty::builders::serde::deserialize::Builder as SerdeBuilder; - - let x = build_with::<SerdeBuilder<serde_json::Value, _>, _>(into_walker(&a)).unwrap(); - dbg!(x); - - // let s = build_with::<JsonLike, _>(into_walker(a)).unwrap(); - - todo!(); - // assert_eq!(s, "[true,[false,true,],false,]"); -} - -#[inline(never)] -pub fn array_to_array(x: [bool; 2]) -> [bool; 2] { - build(into_walker(x)).unwrap() -} - -#[inline(never)] -pub fn array_to_array2(x: [bool; 2]) -> [bool; 2] { - array_to_array(x) -} - -#[derive(Debug)] -enum Data { - Bool(bool), - Sequence(Vec<Data>), -} - -const _: () = { - struct Impl<'ctx>(&'ctx Data); - - impl<'ctx> From<&'ctx Data> for Impl<'ctx> { - fn from(value: &'ctx Data) -> Self { - Self(value) - } - } - - impl<'ctx> Walk<'ctx> for &'ctx Data { - type Walker = Impl<'ctx>; - } - - impl<'ctx> Walker<'ctx> for Impl<'ctx> { - type Effect = SyncEffect; - - type Error = (); - - type Output = (); - - fn walk<'a>( - self, - visitor: Visitor<'a, 'ctx, SyncEffect>, - ) -> Yield<'a, 'ctx, Result<Self::Output, Self::Error>, SyncEffect> - where - 'ctx: 'a, - { - match self.0 { - Data::Bool(value) => walk_bool(*value, visitor), - Data::Sequence(value) => walk_vec(value, visitor), - } - Ok(()) - } - } -}; - -fn walk_bool(value: bool, visitor: Visitor<'_, '_, SyncEffect>) { - visitor - .upcast_mut::<dyn Value<OwnedStatic<bool>, SyncEffect>>() - .unwrap() - .visit(OwnedStatic(value)); -} - -fn walk_vec<'a, 'ctx>(value: &'ctx [Data], visitor: Visitor<'a, 'ctx, SyncEffect>) { - struct Scope<'ctx>(VecDeque<&'ctx Data>); - - impl<'ctx> SequenceScope<'ctx, SyncEffect> for Scope<'ctx> { - fn next<'a>( - &'a mut self, - visitor: Visitor<'a, 'ctx, SyncEffect>, - ) -> Yield<'a, 'ctx, ControlFlow<(), treaty::protocol::visitor::Status>, SyncEffect> - where - 'ctx: 'a, - { - if let Some(value) = self.0.pop_front() { - into_walker(value).walk(visitor); - - ControlFlow::Continue(treaty::protocol::visitor::Status::Continue) - } else { - ControlFlow::Continue(treaty::protocol::visitor::Status::Done) - } - } - } - - let mut scope = Scope(value.into_iter().collect()); - - visitor - .upcast_mut::<dyn Sequence<'_, SyncEffect>>() - .unwrap() - .visit(&mut scope); -} - -#[derive(Default)] -struct JsonLike(String); - -impl<'ctx> Builder<'ctx, SyncEffect> for JsonLike { - type Error = (); - - type Value = String; - - fn build<'a>(self) -> Result<Self::Value, Self::Error> - where - Self: 'a, - { - Ok(self.0) - } - - type Seed = (); - - fn from_seed(seed: Self::Seed) -> Self { - Self::default() - } - - type Effect = SyncEffect; -} - -impl<'ctx> AsVisitor<'ctx, SyncEffect> for JsonLike { - fn as_visitor(&mut self) -> Visitor<'_, 'ctx, SyncEffect> { - self - } -} - -any_trait! { - impl['a, 'ctx] JsonLike = [ - dyn Value<'a, 'ctx, OwnedStatic<bool>, SyncEffect> + 'a, - dyn Sequence<'ctx, SyncEffect> + 'a, - ] -} - -impl<'a, 'ctx: 'a> Value<'a, 'ctx, OwnedStatic<bool>, SyncEffect> for JsonLike { - fn visit( - &'a mut self, - value: OwnedStatic<bool>, - ) -> Yield<'a, 'ctx, ControlFlow<(), ()>, SyncEffect> { - self.0.push_str(&format!("{}", value.0)); - ControlFlow::Continue(()) - } -} - -impl<'ctx> Sequence<'ctx, SyncEffect> for JsonLike { - fn visit<'a>( - &'a mut self, - scope: &'a mut dyn SequenceScope<'ctx, SyncEffect>, - ) -> Yield<'a, 'ctx, ControlFlow<(), ()>, SyncEffect> - where - 'ctx: 'a, - { - self.0.push_str("["); - while let ControlFlow::Continue(treaty::protocol::visitor::Status::Continue) = - scope.next(self) - { - self.0.push_str(","); - } - self.0.push_str("]"); - ControlFlow::Continue(()) - } -} |