From c91e34b04a6c6fa1e11abbf7f2ca0fde12cd8269 Mon Sep 17 00:00:00 2001 From: timofey Date: Sun, 30 Jul 2023 18:50:48 +0000 Subject: [PATCH] move `Mode` to `mode` --- src/mode.rs | 105 ++++++++++++++++++++++++++++++++++++ src/rcore.rs | 7 ++- src/rcore/modes.rs | 107 +------------------------------------ src/rstd/atomic/modes.rs | 3 ++ src/rstd/inlining/modes.rs | 2 + 5 files changed, 115 insertions(+), 109 deletions(-) diff --git a/src/mode.rs b/src/mode.rs index e69de29..f343173 100644 --- a/src/mode.rs +++ b/src/mode.rs @@ -0,0 +1,105 @@ +/// See [`ModeResult`]. +pub type ParseSuccess = ::ParseSuccess; + +/// [`Mode`] equivalent of [`ParseResult`] for extension. +/// +/// [`ParseResult`]: crate::rcore::ParseResult +pub type ExtensionResult = ::ExtensionResult; + +/// See [`Mode::prepare`]. +pub type ExtensionSource = ::ExtensionSource; + +/// [`Mode`] equivalent of [`ParseResult`]. +/// +/// [`ParseResult`]: crate::rcore::ParseResult +pub type ModeResult = Result, E>; + +/// Mode of parsing. +/// +/// | [`Mode`] | [`Mode::ParseSuccess`] | [`Mode::ExtensionResult`] | [`Mode::ExtensionSource`] | +/// |------------------|------------------------------|---------------------------------|------------------------------| +/// | [`RegularMode`] | `A` | [`Result`] | `A` | +/// | [`InliningMode`] | [`(A, I)`] | `E` | [`()`] | +/// +/// [`RegularMode`]: crate::rcore::RegularMode +/// [`InliningMode`]: crate::rstd::inlining::InliningMode +/// [`(A, I)`]: tuple +/// [`()`]: unit +pub trait Mode { + /// Successful parsing, may countain the parser itself + /// (`I`, usually [`Inlining`]). + /// + /// [`Inlining`]: crate::rcore::Inlining + type ParseSuccess; + + /// Result of extending the value, failing sometimes or always. + type ExtensionResult; + + /// Data enough to try extending the value. + /// + /// May be empty for always-failing extensions. + type ExtensionSource; + + /// Do something with the successfully parsed value, potentially failing. + /// + /// Useful for for wrappers and chaining parsing after [`InliningFactory`]. + /// + /// See also [`Mode::map`] + /// + /// [`InliningFactory`]: crate::rstd::inlining::InliningFactory + fn bind( + s: Self::ParseSuccess, + f: impl FnOnce(A0) -> Result, + ) -> ModeResult; + + /// Map the successfully parsed value. + /// + /// Useful for for wrappers. + /// + /// See also [`Mode::bind`] + fn map( + s: Self::ParseSuccess, + f: impl FnOnce(A0) -> A1, + ) -> Self::ParseSuccess; + + /// Discard any extra information contained in [`Mode::ParseSuccess`]. + fn seal(s: Self::ParseSuccess) -> A; + + /// Map the error of an extension result. + /// + /// Useful for for wrappers and chaining parsing after [`InliningFactory`]. + /// + /// [`InliningFactory`]: crate::rstd::inlining::InliningFactory + fn xmap_err( + result: Self::ExtensionResult, + f: impl FnOnce(E0) -> E1, + ) -> Self::ExtensionResult; + + /// Do something with the extension result, potentially failing. + /// + /// Useful for wrappers and chaining extension after [`InliningFactory`]. + /// + /// [`InliningFactory`]: crate::rstd::inlining::InliningFactory + fn xbind( + result: Self::ExtensionResult, + f: impl FnOnce(A0) -> Result, + ) -> Self::ExtensionResult; + + /// Convert [`Mode::ExtensionResult`] to [`Result`]. + fn xseal(result: Self::ExtensionResult) -> Result; + + fn smap( + source: Self::ExtensionSource, + f: impl FnOnce(A0) -> A1, + ) -> Self::ExtensionSource; + + fn prepare(a: A) -> Self::ExtensionSource; + + /// For abstract extension implementations. + fn xsbind( + ab: Self::ExtensionSource, + t2ab: impl FnOnce(AB) -> (A, B), + ce: impl FnOnce(Self::ExtensionSource) -> Self::ExtensionResult, + ab2t: impl FnOnce(A, B) -> Result, + ) -> Self::ExtensionResult; +} diff --git a/src/rcore.rs b/src/rcore.rs index 453b540..be3875b 100644 --- a/src/rcore.rs +++ b/src/rcore.rs @@ -33,10 +33,9 @@ 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::{ - CRegularFactory, ExtensionResult, ExtensionResultM, ExtensionResultP, ExtensionSource, - ExtensionSourceM, ExtensionSourceP, FactoryModeParse, FactoryModeProxy, ImplMode, Mode, - ModeResult, ModeResultM, ModeResultP, ParseMode, ParseModeExt, ParseSuccess, ParseSuccessP, - RegularFactory, RegularMode, WithMode, WithParseMode, + CRegularFactory, ExtensionResultM, ExtensionResultP, ExtensionSourceM, ExtensionSourceP, + FactoryModeParse, FactoryModeProxy, ImplMode, ModeResultM, ModeResultP, ParseMode, + ParseModeExt, ParseSuccessP, 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 a4fd908..90560f6 100644 --- a/src/rcore/modes.rs +++ b/src/rcore/modes.rs @@ -1,112 +1,9 @@ use std::marker::PhantomData; +use crate::mode::*; + use super::*; -/// See [`ModeResult`]. -pub type ParseSuccess = ::ParseSuccess; - -/// [`Mode`] equivalent of [`ParseResult`] for extension. -pub type ExtensionResult = ::ExtensionResult; - -/// See [`Mode::prepare`]. -pub type ExtensionSource = ::ExtensionSource; - -/// [`Mode`] equivalent of [`ParseResult`]. -pub type ModeResult = Result, E>; - -/// Mode of parsing. -/// -/// | [`Mode`] | [`Mode::ParseSuccess`] | [`Mode::ExtensionResult`] | [`Mode::ExtensionSource`] | -/// |------------------|------------------------------|---------------------------------|------------------------------| -/// | [`RegularMode`] | `A` | [`Result`] | `A` | -/// | [`InliningMode`] | [`(A, I)`] | `E` | [`()`] | -/// -/// [`InliningMode`]: crate::rstd::inlining::InliningMode -/// [`(A, I)`]: tuple -/// [`()`]: unit -pub trait Mode { - /// Successful parsing, may countain the parser itself - /// (`I`, usually [`Inlining`]). - /// - /// See [`FactoryParse::deserialize`]. - type ParseSuccess; - - /// Result of extending the value, failing sometimes or always. - /// - /// See [`FactoryParse::extend`]. - type ExtensionResult; - - /// Data enough to try extending the value. - /// - /// May be empty for always-failing extensions. - /// - /// See [`FactoryParse::extend`]. - type ExtensionSource; - - /// Do something with the successfully parsed value, potentially failing. - /// - /// Useful for for wrappers and chaining parsing after [`InliningFactory`]. - /// - /// See also [`Mode::map`] - /// - /// [`InliningFactory`]: crate::rstd::inlining::InliningFactory - fn bind( - s: Self::ParseSuccess, - f: impl FnOnce(A0) -> Result, - ) -> ModeResult; - - /// Map the successfully parsed value. - /// - /// Useful for for wrappers. - /// - /// See also [`Mode::bind`] - fn map( - s: Self::ParseSuccess, - f: impl FnOnce(A0) -> A1, - ) -> Self::ParseSuccess; - - /// Discard any extra information contained in [`Mode::ParseSuccess`]. - fn seal(s: Self::ParseSuccess) -> A; - - /// Map the error of an extension result. - /// - /// Useful for for wrappers and chaining parsing after [`InliningFactory`]. - /// - /// [`InliningFactory`]: crate::rstd::inlining::InliningFactory - fn xmap_err( - result: Self::ExtensionResult, - f: impl FnOnce(E0) -> E1, - ) -> Self::ExtensionResult; - - /// Do something with the extension result, potentially failing. - /// - /// Useful for wrappers and chaining extension after [`InliningFactory`]. - /// - /// [`InliningFactory`]: crate::rstd::inlining::InliningFactory - fn xbind( - result: Self::ExtensionResult, - f: impl FnOnce(A0) -> Result, - ) -> Self::ExtensionResult; - - /// Convert [`Mode::ExtensionResult`] to [`Result`]. - fn xseal(result: Self::ExtensionResult) -> Result; - - fn smap( - source: Self::ExtensionSource, - f: impl FnOnce(A0) -> A1, - ) -> Self::ExtensionSource; - - fn prepare(a: A) -> Self::ExtensionSource; - - /// For abstract [`FactoryParse::extend`] implementations. - fn xsbind( - ab: Self::ExtensionSource, - t2ab: impl FnOnce(AB) -> (A, B), - ce: impl FnOnce(Self::ExtensionSource) -> Self::ExtensionResult, - ab2t: impl FnOnce(A, B) -> Result, - ) -> Self::ExtensionResult; -} - /// Type that a [`Mode`] associated with it. pub trait ParseMode { /// Associated [`Mode`]. diff --git a/src/rstd/atomic/modes.rs b/src/rstd/atomic/modes.rs index afdd7ac..1ed8066 100644 --- a/src/rstd/atomic/modes.rs +++ b/src/rstd/atomic/modes.rs @@ -1,3 +1,6 @@ +#[cfg(doc)] +use crate::mode::*; + use super::*; impl Atomic for A { diff --git a/src/rstd/inlining/modes.rs b/src/rstd/inlining/modes.rs index 52a193f..a4da016 100644 --- a/src/rstd/inlining/modes.rs +++ b/src/rstd/inlining/modes.rs @@ -1,3 +1,5 @@ +use crate::mode::*; + use super::*; /// [`Mode`] for [`InliningFactory`].