AParseResult

This commit is contained in:
AF 2023-06-28 15:45:52 +00:00
parent 59e3e4f5d6
commit 712026be40
4 changed files with 13 additions and 13 deletions

View File

@ -12,6 +12,8 @@ use crate::rcore::*;
use super::*; use super::*;
pub type AParseResult<A> = Result<A, <A as Atomic>::AParseError>;
/// This trait combines functionality of [`Mentionable`] and [`Factory`], /// This trait combines functionality of [`Mentionable`] and [`Factory`],
/// while limiting [`Mentionable::points_typed`] (and corresponding [`Mentionable::topology`]) /// while limiting [`Mentionable::points_typed`] (and corresponding [`Mentionable::topology`])
/// to an empty sequence. /// to an empty sequence.
@ -19,12 +21,12 @@ pub trait Atomic: 'static + Send + Sync + Send + Clone + Serializable {
/// Equivalent of [`Factory::ParseError`]. /// Equivalent of [`Factory::ParseError`].
type AParseError: Error; type AParseError: Error;
/// Static equivalent of [`Factory::deserialize`]. /// Static equivalent of [`Factory::deserialize`].
fn a_deserialize(deserializer: &mut dyn Deserializer) -> Result<Self, Self::AParseError>; fn a_deserialize(deserializer: &mut dyn Deserializer) -> AParseResult<Self>;
/// Static equivalent of [`Factory::extend`]. /// Static equivalent of [`Factory::extend`].
fn a_extend(self, tail: &[u8]) -> Result<Self, Self::AParseError>; fn a_extend(self, tail: &[u8]) -> AParseResult<Self>;
} }
fn _parse_slice<A: Atomic>(slice: &[u8]) -> Result<A, A::AParseError> { fn _parse_slice<A: Atomic>(slice: &[u8]) -> AParseResult<A> {
let mut deserializer = SliceDeserializer::from(slice); let mut deserializer = SliceDeserializer::from(slice);
let atomic = A::a_deserialize(&mut deserializer)?; let atomic = A::a_deserialize(&mut deserializer)?;
let tail = deserializer.read_all(); let tail = deserializer.read_all();
@ -38,13 +40,11 @@ fn _parse_slice<A: Atomic>(slice: &[u8]) -> Result<A, A::AParseError> {
/// Extension trait to provide method-like utilities associated with [Atomic]s. /// Extension trait to provide method-like utilities associated with [Atomic]s.
pub trait AtomicExt: Atomic { pub trait AtomicExt: Atomic {
/// Static equivalent of [`FactoryExt::parse_slice`]. /// Static equivalent of [`FactoryExt::parse_slice`].
fn parse_slice(slice: &[u8]) -> Result<Self, Self::AParseError> { fn parse_slice(slice: &[u8]) -> AParseResult<Self> {
_parse_slice(slice) _parse_slice(slice)
} }
fn o_deserialise<'a, Ctx: Context<'a>>( fn o_deserialise<'a, Ctx: Context<'a>>(dectx: &mut dyn DeCtx<'a, Ctx>) -> AParseResult<Self> {
dectx: &mut dyn DeCtx<'a, Ctx>,
) -> Result<Self, Self::AParseError> {
Self::a_deserialize(dectx.deserializer()) Self::a_deserialize(dectx.deserializer())
} }
} }

View File

@ -37,11 +37,11 @@ impl From<&[u8]> for IntParseError {
impl Atomic for u64 { impl Atomic for u64 {
type AParseError = IntParseError; type AParseError = IntParseError;
fn a_deserialize(deserializer: &mut dyn Deserializer) -> Result<Self, Self::AParseError> { fn a_deserialize(deserializer: &mut dyn Deserializer) -> AParseResult<Self> {
Ok(u64::from_le_bytes(deserializer.read_n_const::<8>()?)) Ok(u64::from_le_bytes(deserializer.read_n_const::<8>()?))
} }
fn a_extend(self, tail: &[u8]) -> Result<Self, Self::AParseError> { fn a_extend(self, tail: &[u8]) -> AParseResult<Self> {
Err(Self::a_extension_error(tail)) Err(Self::a_extension_error(tail))
} }
} }

View File

@ -44,7 +44,7 @@ impl From<&[u8]> for BooleanParseError {
impl Atomic for bool { impl Atomic for bool {
type AParseError = BooleanParseError; type AParseError = BooleanParseError;
fn a_deserialize(deserializer: &mut dyn Deserializer) -> Result<Self, Self::AParseError> { fn a_deserialize(deserializer: &mut dyn Deserializer) -> AParseResult<Self> {
let byte = deserializer.read_n_const::<1>()?; let byte = deserializer.read_n_const::<1>()?;
match byte[0] { match byte[0] {
0 => Ok(false), 0 => Ok(false),
@ -53,7 +53,7 @@ impl Atomic for bool {
} }
} }
fn a_extend(self, tail: &[u8]) -> Result<Self, Self::AParseError> { fn a_extend(self, tail: &[u8]) -> AParseResult<Self> {
Err(Self::a_extension_error(tail)) Err(Self::a_extension_error(tail))
} }
} }

View File

@ -31,11 +31,11 @@ impl Serializable for Plain {
impl Atomic for Plain { impl Atomic for Plain {
type AParseError = PlainParseError; type AParseError = PlainParseError;
fn a_deserialize(deserializer: &mut dyn Deserializer) -> Result<Self, Self::AParseError> { fn a_deserialize(deserializer: &mut dyn Deserializer) -> AParseResult<Self> {
Ok(Plain::from_slice(deserializer.read_all())) Ok(Plain::from_slice(deserializer.read_all()))
} }
fn a_extend(mut self, tail: &[u8]) -> Result<Self, Self::AParseError> { fn a_extend(mut self, tail: &[u8]) -> AParseResult<Self> {
self.data.extend_from_slice(tail); self.data.extend_from_slice(tail);
Ok(self) Ok(self)
} }