From 5dff836c58f7d8807db19d729ae0402416b0c863 Mon Sep 17 00:00:00 2001 From: timofey Date: Sat, 29 Jul 2023 18:32:11 +0000 Subject: [PATCH] `Mode` --- src/rcore.rs | 2 +- src/rcore/modes.rs | 34 +++++++++++++++++++++++++++++++++- src/rstd/inlining/modes.rs | 20 ++++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/rcore.rs b/src/rcore.rs index eb02440..33b64c6 100644 --- a/src/rcore.rs +++ b/src/rcore.rs @@ -33,7 +33,7 @@ pub use self::hashing::{Hash, HASH_SIZE, HASH_ZEROS}; pub use self::inctx::InCtx; pub use self::inlining::{Inlining, InliningExt, InliningResultExt}; pub use self::modes::{ - FactoryProxy, ParseMode, RegularFactory, RegularMode, WithMode, WithParseMode, + FactoryProxy, Mode, ParseMode, RegularFactory, RegularMode, WithMode, WithParseMode, }; pub use self::origin::{OFctr, Origin}; pub use self::point::Point; diff --git a/src/rcore/modes.rs b/src/rcore/modes.rs index 6156134..cf8eb40 100644 --- a/src/rcore/modes.rs +++ b/src/rcore/modes.rs @@ -2,8 +2,22 @@ use std::marker::PhantomData; use super::*; +pub trait Mode { + type ParseResult; + + fn map_err( + result: Self::ParseResult, + f: impl FnOnce(E0) -> E1, + ) -> Self::ParseResult; + + fn bind( + result: Self::ParseResult, + f: impl FnOnce(A0) -> Result, + ) -> Self::ParseResult; +} + pub trait ParseMode { - type Mode: ?Sized; + type Mode: ?Sized + Mode; } pub trait WithParseMode: ParseMode { @@ -49,6 +63,24 @@ where pub struct RegularMode; +impl Mode for RegularMode { + type ParseResult = Result; + + fn map_err( + result: Self::ParseResult, + f: impl FnOnce(E0) -> E1, + ) -> Self::ParseResult { + result.map_err(f) + } + + fn bind( + result: Self::ParseResult, + f: impl FnOnce(A0) -> Result, + ) -> Self::ParseResult { + result.and_then(f) + } +} + pub trait RegularFactory<'a, Ctx: Context<'a>>: FactoryBase<'a, Ctx> + ParseMode { diff --git a/src/rstd/inlining/modes.rs b/src/rstd/inlining/modes.rs index decb4ac..7ac123b 100644 --- a/src/rstd/inlining/modes.rs +++ b/src/rstd/inlining/modes.rs @@ -2,6 +2,26 @@ use super::*; pub struct InliningMode; +impl Mode for InliningMode { + type ParseResult = Result<(A, I), E>; + + fn map_err( + result: Self::ParseResult, + f: impl FnOnce(E0) -> E1, + ) -> Self::ParseResult { + result.map_err(f) + } + + fn bind( + result: Self::ParseResult, + f: impl FnOnce(A0) -> Result, + ) -> Self::ParseResult { + let (a0, i) = result?; + let a1 = f(a0)?; + Ok((a1, i)) + } +} + impl<'a, Ctx: Context<'a>, F: InlineableFactory<'a, Ctx>> FactoryProxy<'a, Ctx> for WithMode {