Something went wrong. Try again.
This string has no description
Something went wrong. Try again.
15 kB · 545 lines · Created 5mo ago Rust
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546use std::fmt::Debug;
type OpResult<Op> = Result< (<Op as Operator>::Output, <Op as Operator>::Inverse), <<Op as Operator>::Target as OperatorTarget>::Error,>;
type BindFn<Op, Pass> = fn( &<Op as Operator>::Target, Pass,) -> Result<Op, <<Op as Operator>::Target as OperatorTarget>::Error>;
pub trait OperatorTarget { type Event; type Error;}
pub trait Operator { type Input; type Output; type Target: OperatorTarget; type Inverse: Operator<Input = Self::Output, Output = Self::Input, Target = Self::Target, Inverse = Self>;
fn apply( &self, target: &mut Self::Target, events: &mut Vec<<Self::Target as OperatorTarget>::Event>, input: Self::Input, ) -> OpResult<Self> where Self: Sized;
fn seq<Op>(self, next: Op) -> Chain<Self, Op> where Self: Sized, Op: Operator<Input = Self::Output, Target = Self::Target>, { Chain(self, next) }
fn bind<Op>(self, f: BindFn<Op, Self::Output>) -> Chain<Self, Bind<Op, Self::Output>> where Self: Sized, Op: Operator<Target = Self::Target>, { Chain(self, Bind::new(f)) }
fn with(self, input: Self::Input) -> Chain<Const<Self::Input, Self::Target>, Self> where Self: Sized, { Chain(Const::new(input), self) }}
#[derive(Debug, Clone)]pub struct Chain<A, B>(A, B);
impl<A, B, S1, S2, S3, T> Operator for Chain<A, B>where A: Operator<Input = S1, Output = S2, Target = T>, B: Operator<Input = S2, Output = S3, Target = T>, T: OperatorTarget,{ type Input = S1; type Output = S3; type Target = T; type Inverse = Chain<B::Inverse, A::Inverse>;
fn apply( &self, target: &mut Self::Target, events: &mut Vec<<Self::Target as OperatorTarget>::Event>, input: Self::Input, ) -> Result<(Self::Output, Self::Inverse), <Self::Target as OperatorTarget>::Error> { let (s2, inv_a) = self.0.apply(target, events, input)?; let (s3, inv_b) = self.1.apply(target, events, s2)?; Ok((s3, Chain(inv_b, inv_a))) }}
#[derive(Clone, Copy)]pub struct Read<Target: OperatorTarget, Input, Output> { f: fn(&Target, Input) -> Result<Output, Target::Error>,}
impl<Target: OperatorTarget, Input, Output> Read<Target, Input, Output> { pub fn new(f: fn(&Target, Input) -> Result<Output, Target::Error>) -> Self { Self { f } }}
impl<Target: OperatorTarget, Input, Output> Debug for Read<Target, Input, Output> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_tuple("Read").finish() }}
impl<Target: OperatorTarget, Input, Output> Operator for Read<Target, Input, Output>where Input: Clone,{ type Input = Input; type Output = Output; type Target = Target; type Inverse = Forget<Target, Input, Output>;
fn apply( &self, target: &mut Self::Target, _events: &mut Vec<<Self::Target as OperatorTarget>::Event>, input: Self::Input, ) -> Result<(Self::Output, Self::Inverse), <Self::Target as OperatorTarget>::Error> { Ok(((self.f)(target, input.clone())?, Forget::new(self.f, input))) }}
#[derive(Clone, Copy)]pub struct Forget<Target: OperatorTarget, Input, Output> { f: fn(&Target, Input) -> Result<Output, Target::Error>, i: Input,}
impl<Target: OperatorTarget, Input, Output> Forget<Target, Input, Output> { pub fn new(f: fn(&Target, Input) -> Result<Output, Target::Error>, i: Input) -> Self { Self { f, i } }}
impl<Target: OperatorTarget, Input, Output> Debug for Forget<Target, Input, Output> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_tuple("Forget").finish() }}
impl<Target: OperatorTarget, Input, Output> Operator for Forget<Target, Input, Output>where Input: Clone,{ type Input = Output; type Output = Input; type Target = Target; type Inverse = Read<Target, Input, Output>;
fn apply( &self, _target: &mut Self::Target, _events: &mut Vec<<Self::Target as OperatorTarget>::Event>, _input: Self::Input, ) -> Result<(Self::Output, Self::Inverse), <Self::Target as OperatorTarget>::Error> { Ok((self.i.clone(), Read::new(self.f))) }}
#[derive(Clone)]pub struct Bind<Op: Operator, Pass> { f: BindFn<Op, Pass>,}
impl<Op: Operator, Pass> Bind<Op, Pass> { pub fn new(f: BindFn<Op, Pass>) -> Self { Self { f } }}
impl<Op: Operator, Pass> Debug for Bind<Op, Pass> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_tuple("Bind").finish() }}
impl<Op, Pass> Operator for Bind<Op, Pass>where Op: Operator<Input = ()>, Pass: Clone,{ type Input = Pass; type Output = Op::Output; type Target = Op::Target; type Inverse = Bound<Op, Pass>;
fn apply( &self, target: &mut Self::Target, events: &mut Vec<<Self::Target as OperatorTarget>::Event>, input: Self::Input, ) -> Result<(Self::Output, Self::Inverse), <Self::Target as OperatorTarget>::Error> { let op = (self.f)(target, input.clone())?; op.apply(target, events, ()) .map(|(out, inv)| (out, Bound::new(self.f, input, inv))) }}
pub struct Bound<Op: Operator, Pass> { f: BindFn<Op, Pass>, i: Pass, inverse: <Op as Operator>::Inverse,}
impl<Op: Operator, Pass> Bound<Op, Pass> { pub fn new(f: BindFn<Op, Pass>, i: Pass, inverse: <Op as Operator>::Inverse) -> Self { Self { f, i, inverse } }}
impl<Op: Operator, Pass> Debug for Bound<Op, Pass> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_tuple("Bound").finish() }}
impl<Op, Pass> Operator for Bound<Op, Pass>where Op: Operator<Input = ()>, Pass: Clone,{ type Input = Op::Output; type Output = Pass; type Target = Op::Target; type Inverse = Bind<Op, Pass>;
fn apply( &self, target: &mut Self::Target, events: &mut Vec<<Self::Target as OperatorTarget>::Event>, input: Self::Input, ) -> Result<(Self::Output, Self::Inverse), <Self::Target as OperatorTarget>::Error> { let _ = self.inverse.apply(target, events, input)?; Ok((self.i.clone(), Bind::new(self.f))) }}
#[derive(Clone)]pub struct Const<T, Target>(T, std::marker::PhantomData<Target>);
impl<T, Target> Const<T, Target> { pub fn new(value: T) -> Self { Self(value, std::marker::PhantomData) }}
impl<T, Target> From<T> for Const<T, Target> { fn from(value: T) -> Self { Self::new(value) }}
impl<T, Target> Debug for Const<T, Target> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_tuple("Const").finish() }}
impl<T, Target> Operator for Const<T, Target>where T: Clone, Target: OperatorTarget,{ type Input = (); type Output = T; type Target = Target; type Inverse = Discard<T, Target>;
fn apply( &self, _target: &mut Self::Target, _events: &mut Vec<<Self::Target as OperatorTarget>::Event>, _input: Self::Input, ) -> Result<(Self::Output, Self::Inverse), <Self::Target as OperatorTarget>::Error> { Ok((self.0.clone(), Discard::new())) }}
#[derive(Clone)]pub struct Discard<T, Target>(std::marker::PhantomData<(T, Target)>);
impl<T, Target> Discard<T, Target> { pub fn new() -> Self { Self(std::marker::PhantomData) }}
impl<T, Target> Default for Discard<T, Target> { fn default() -> Self { Self::new() }}
impl<T, Target> Debug for Discard<T, Target> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_tuple("Discard").finish() }}
impl<T, Target> Operator for Discard<T, Target>where T: Clone, Target: OperatorTarget,{ type Input = T; type Output = (); type Target = Target; type Inverse = Const<T, Target>;
fn apply( &self, _target: &mut Self::Target, _events: &mut Vec<<Self::Target as OperatorTarget>::Event>, input: Self::Input, ) -> Result<(Self::Output, Self::Inverse), <Self::Target as OperatorTarget>::Error> { Ok(((), Const::new(input))) }}
#[derive(Clone)]pub struct Push<T, Input, Target>(T, std::marker::PhantomData<(Input, Target)>);
impl<T, Input, Target> Push<T, Input, Target> { pub fn new(value: T) -> Self { Self(value, std::marker::PhantomData) }}
impl<T, Input, Target> Debug for Push<T, Input, Target> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_tuple("Push").finish() }}
impl<T, Input, Target> Operator for Push<T, Input, Target>where T: Clone, Target: OperatorTarget,{ type Input = Input; type Output = (Input, T); type Target = Target; type Inverse = Pop<T, Input, Target>;
fn apply( &self, _target: &mut Self::Target, _events: &mut Vec<<Self::Target as OperatorTarget>::Event>, input: Self::Input, ) -> Result<(Self::Output, Self::Inverse), <Self::Target as OperatorTarget>::Error> { Ok(((input, self.0.clone()), Pop::new(self.0.clone()))) }}
#[derive(Clone)]pub struct Pop<T, Input, Target>(T, std::marker::PhantomData<(Input, Target)>);
impl<T, Input, Target> Pop<T, Input, Target> { pub fn new(value: T) -> Self { Self(value, std::marker::PhantomData) }}
impl<T, Input, Target> Debug for Pop<T, Input, Target> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_tuple("Pop").finish() }}
impl<T, Input, Target> Operator for Pop<T, Input, Target>where T: Clone, Target: OperatorTarget,{ type Input = (Input, T); type Output = Input; type Target = Target; type Inverse = Push<T, Input, Target>;
fn apply( &self, _target: &mut Self::Target, _events: &mut Vec<<Self::Target as OperatorTarget>::Event>, input: Self::Input, ) -> Result<(Self::Output, Self::Inverse), <Self::Target as OperatorTarget>::Error> { Ok((input.0, Push::new(self.0.clone()))) }}
#[derive(Debug, Clone)]pub enum Or<A, B, T> { Left(A, std::marker::PhantomData<T>), Right(B, std::marker::PhantomData<T>),}
impl<A, B, T> Operator for Or<A, B, T>where A: Operator<Target = T>, B: Operator<Input = A::Input, Output = A::Output, Target = T>, T: OperatorTarget,{ type Input = A::Input; type Output = A::Output; type Target = T; type Inverse = Or<A::Inverse, B::Inverse, T>;
fn apply( &self, target: &mut Self::Target, events: &mut Vec<<Self::Target as OperatorTarget>::Event>, input: Self::Input, ) -> Result<(Self::Output, Self::Inverse), <Self::Target as OperatorTarget>::Error> { match self { Or::Left(op, _) => op .apply(target, events, input) .map(|(out, inv)| (out, Or::Left(inv, std::marker::PhantomData))), Or::Right(op, _) => op .apply(target, events, input) .map(|(out, inv)| (out, Or::Right(inv, std::marker::PhantomData))), } }}
pub trait ErasedOperator { type Target: OperatorTarget;
fn apply_erased( &self, target: &mut Self::Target, events: &mut Vec<<Self::Target as OperatorTarget>::Event>, ) -> Result< Box<dyn ErasedOperator<Target = Self::Target>>, <Self::Target as OperatorTarget>::Error, >;}
impl<T: OperatorTarget> ErasedOperator for Box<dyn ErasedOperator<Target = T>> { type Target = T;
fn apply_erased( &self, target: &mut Self::Target, events: &mut Vec<<Self::Target as OperatorTarget>::Event>, ) -> Result< Box<dyn ErasedOperator<Target = Self::Target>>, <Self::Target as OperatorTarget>::Error, > { self.as_ref().apply_erased(target, events) }}
impl<Op, Target> ErasedOperator for Opwhere Op: Operator<Input = (), Target = Target> + 'static, Op::Output: Debug + Clone + 'static, Target: OperatorTarget,{ type Target = Target;
fn apply_erased( &self, target: &mut Self::Target, events: &mut Vec<<Self::Target as OperatorTarget>::Event>, ) -> Result< Box<dyn ErasedOperator<Target = Self::Target>>, <Self::Target as OperatorTarget>::Error, > { self.apply(target, events, ()).map(Into::into) }}
impl<I, Op, Ta> ErasedOperator for (I, Op)where I: Debug + Clone + 'static, Ta: OperatorTarget, Op: Operator<Input = I, Target = Ta> + 'static, Op::Output: Debug + Clone,{ type Target = Ta;
fn apply_erased( &self, target: &mut Self::Target, events: &mut Vec<<Self::Target as OperatorTarget>::Event>, ) -> Result< Box<dyn ErasedOperator<Target = Self::Target>>, <Self::Target as OperatorTarget>::Error, > { self.1.apply(target, events, self.0.clone()).map(Into::into) }}
impl<Op, Target> From<Op> for Box<dyn ErasedOperator<Target = Target>>where Op: Operator<Input = (), Target = Target> + 'static, Op::Output: Debug + Clone + 'static, Target: OperatorTarget,{ fn from(op: Op) -> Self { Box::new(((), op)) }}
impl<I, Op, Ta: OperatorTarget> From<(I, Op)> for Box<dyn ErasedOperator<Target = Ta>>where I: Debug + Clone + 'static, Op: Operator<Input = I, Target = Ta> + 'static, Op::Output: Debug + Clone + 'static,{ fn from(op: (I, Op)) -> Self { Box::new(op) }}
pub type ErasedOperators<Target> = Vec<Box<dyn ErasedOperator<Target = Target>>>;
impl<Target> Operator for ErasedOperators<Target>where Target: OperatorTarget,{ type Input = (); type Output = (); type Target = Target; type Inverse = Self;
fn apply( &self, target: &mut Self::Target, events: &mut Vec<<Self::Target as OperatorTarget>::Event>, _input: Self::Input, ) -> Result<(Self::Output, Self::Inverse), <Self::Target as OperatorTarget>::Error> { let mut inverses = Vec::new(); for op in self { match op.apply_erased(target, events) { Ok(inv) => inverses.push(inv), Err(e) => { // Rollback all applied operators in reverse order for inv in inverses.into_iter().rev() { // Ignore errors during rollback to ensure best effort at restoring state let _ = inv.apply_erased(target, events); } return Err(e); } } } inverses.reverse(); Ok(((), inverses)) }}