47 lines
1.5 KiB
Rust
47 lines
1.5 KiB
Rust
//! This module allows to describe a primitive subset of [Mentionable] types, [Atomic]s,
|
|
//! simple static types, which are completely [Context]-independent.
|
|
|
|
pub mod atomic_object;
|
|
pub mod boolean;
|
|
pub mod plain;
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
use crate::core::*;
|
|
|
|
use super::*;
|
|
|
|
/// This trait combines functionality of [`Mentionable`] and [`Factory`],
|
|
/// while limiting [`Mentionable::points`] (and corresponding [`Mentionable::topology`]) to an empty sequence.
|
|
pub trait Atomic: 'static + Sized + Clone + Serializable {
|
|
/// Equivalent of [`Factory::ParseError`].
|
|
type ParseError: Error;
|
|
/// Static equivalent of [`Factory::deserialize`].
|
|
fn deserialize(deserializer: &mut dyn Deserializer) -> Result<Self, Self::ParseError>;
|
|
/// Static equivalent of [`Factory::unexpected_tail`].
|
|
fn unexpected_tail(tail: &[u8]) -> Self::ParseError;
|
|
}
|
|
|
|
fn _parse_slice<A: Atomic>(slice: &[u8]) -> Result<A, A::ParseError> {
|
|
let mut deserializer = SliceDeserializer::from(slice);
|
|
let mentionable = A::deserialize(&mut deserializer)?;
|
|
let tail = deserializer.read_all();
|
|
if tail.is_empty() {
|
|
Ok(mentionable)
|
|
} else {
|
|
Err(A::unexpected_tail(tail))
|
|
}
|
|
}
|
|
|
|
/// Extension trait to provide method-like utilities associated with [Atomic]s.
|
|
pub trait ExtAtomic: Atomic {
|
|
/// Static equivalent of [`ExtFactory::parse_slice`].
|
|
fn parse_slice(slice: &[u8]) -> Result<Self, Self::ParseError>;
|
|
}
|
|
|
|
impl<A: Atomic> ExtAtomic for A {
|
|
fn parse_slice(slice: &[u8]) -> Result<Self, Self::ParseError> {
|
|
_parse_slice(slice)
|
|
}
|
|
}
|