//! Core module for ADN functionality. //! Brings [`Mentionable`]/[`Factory`]/[`Origin`] concepts from the original implementation in Python. //! Allows for more generic behaviour via [`Context`], as opposed to original async-only. mod addresses; mod hashing; mod origin; mod point; mod points; mod resolution; mod resolver_origin; mod serialization; mod slice_deserializer; use std::{error::Error, rc::Rc}; use crate::func::*; pub use self::addresses::*; pub use self::hashing::*; pub use self::origin::*; pub use self::point::*; pub use self::points::TakesPoints; pub use self::resolution::*; pub use self::serialization::*; pub use self::slice_deserializer::*; /// Basic support for tracing events across the execution. pub trait Diagnostic<'a, T: Monad<'a>> { /// Specify that the evaluation happens after a specific event. fn after<'b, A: 'a>(fa: T::F<'a, A>, event: impl 'b + FnOnce() -> String) -> T::F<'a, A> where 'a: 'b; /// Specify that the evaluation happens before a specific event. fn before<'b, A: 'a>(fa: T::F<'a, A>, event: impl 'b + FnOnce() -> String) -> T::F<'a, A> where 'a: 'b; /// Label the evaluation step as a specific named action. fn wrapped<'b, A: 'a>(fa: T::F<'a, A>, event: impl 'b + FnOnce() -> String) -> T::F<'a, A> where 'a: 'b; } /// Execution context. pub trait Context<'a>: 'a { /// Type to provide for [Monad]ic representation of computation, mostly that of resolution ([`Resolution`]). type T: Monad<'a>; /// Type to allow improved support for result evaluation. /// This is important for async applications stopping early. type Fallible: MonadFailAny<'a, T = Self::T>; /// See [`Diagnostic`]. type D: Diagnostic<'a, Self::T>; /// Type to represent resolution errors mainly arising in [`Resolver::resolve`]. type LookupError: 'a + Error; /// Get [type@Hash] of a slice, mostly for use in [`Point`]. fn hash(s: &[u8]) -> Hash; } /// Helper alias for [`WeakFunctor::F`] of [`Context::T`]. pub type Wrapped<'a, Ctx, A> = Wrap<'a, A, >::T>; /// Fundamental trait for ADN objects. pub trait Mentionable<'a, Ctx: Context<'a>>: 'a + Serializable { /// Type of the associated factory. type Fctr: Factory<'a, Ctx, Mtbl = Self>; /// Value of the associated factory. fn factory(&self) -> Self::Fctr; /// See implementation for the definition. /// Hash of all the references' points concatenated, ordered, non-unique. /// Used for walking over object trees to ensure two objects with different references don't collide. fn topology(&self) -> Hash { let mut vec = Vec::new(); self.points_typed(&mut vec); Ctx::hash(&vec) } /// References ([Point]s) to other objects. Typed. fn points_typed(&self, points: &mut impl TakesPoints<'a, Ctx>); } pub type Fctr<'a, Ctx, A> = >::Fctr; /// Shorthand for the type of vaalues returned by [`Factory::deserialize`]. pub type ParseResult<'a, Ctx, F> = Result, >::ParseError>; /// Trait representing deserialisation rules for [Mentionable]s. /// Crucial for [`crate::rstd::typeless`]. pub trait Factory<'a, Ctx: Context<'a>>: 'a + Send + Sync + Clone { /// Type of the associated objects. type Mtbl: Mentionable<'a, Ctx, Fctr = Self>; /// Type of an error that [`Factory::deserialize`] can fail with. type ParseError: 'a + Error; /// See [`Deserializer`], [`Resolver`]. fn deserialize( &self, deserializer: &mut dyn Deserializer, resolver: Rc>, addresses: &mut Addresses, ) -> ParseResult<'a, Ctx, Self>; /// Called by finite stream parsers if there's any data left. fn unexpected_tail(&self, tail: &[u8]) -> Self::ParseError; } pub type Mtbl<'a, Ctx, F> = >::Mtbl; pub type ParseError<'a, Ctx, F> = >::ParseError; /// Extension trait for factories. pub trait ExtFactory<'a, Ctx: Context<'a>>: Factory<'a, Ctx> { /// Parse the object from a slice. fn parse_slice( &self, slice: &[u8], resolver: Rc>, ) -> ParseResult<'a, Ctx, Self>; }