AParseResult
This commit is contained in:
parent
59e3e4f5d6
commit
712026be40
@ -12,6 +12,8 @@ use crate::rcore::*;
|
||||
|
||||
use super::*;
|
||||
|
||||
pub type AParseResult<A> = Result<A, <A as Atomic>::AParseError>;
|
||||
|
||||
/// This trait combines functionality of [`Mentionable`] and [`Factory`],
|
||||
/// while limiting [`Mentionable::points_typed`] (and corresponding [`Mentionable::topology`])
|
||||
/// to an empty sequence.
|
||||
@ -19,12 +21,12 @@ pub trait Atomic: 'static + Send + Sync + Send + Clone + Serializable {
|
||||
/// Equivalent of [`Factory::ParseError`].
|
||||
type AParseError: Error;
|
||||
/// 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`].
|
||||
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 atomic = A::a_deserialize(&mut deserializer)?;
|
||||
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.
|
||||
pub trait AtomicExt: Atomic {
|
||||
/// 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)
|
||||
}
|
||||
|
||||
fn o_deserialise<'a, Ctx: Context<'a>>(
|
||||
dectx: &mut dyn DeCtx<'a, Ctx>,
|
||||
) -> Result<Self, Self::AParseError> {
|
||||
fn o_deserialise<'a, Ctx: Context<'a>>(dectx: &mut dyn DeCtx<'a, Ctx>) -> AParseResult<Self> {
|
||||
Self::a_deserialize(dectx.deserializer())
|
||||
}
|
||||
}
|
||||
|
@ -37,11 +37,11 @@ impl From<&[u8]> for IntParseError {
|
||||
impl Atomic for u64 {
|
||||
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>()?))
|
||||
}
|
||||
|
||||
fn a_extend(self, tail: &[u8]) -> Result<Self, Self::AParseError> {
|
||||
fn a_extend(self, tail: &[u8]) -> AParseResult<Self> {
|
||||
Err(Self::a_extension_error(tail))
|
||||
}
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ impl From<&[u8]> for BooleanParseError {
|
||||
impl Atomic for bool {
|
||||
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>()?;
|
||||
match byte[0] {
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
@ -31,11 +31,11 @@ impl Serializable for Plain {
|
||||
impl Atomic for Plain {
|
||||
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()))
|
||||
}
|
||||
|
||||
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);
|
||||
Ok(self)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user