use std::error::Error; use std::fmt::Display; use std::rc::Rc; use crate::core::*; use crate::func::*; use crate::std::cast::CastError; use sha2::{Digest, Sha256}; pub struct TestContext; #[derive(Debug)] pub enum TestLookupError<'a> { Typeless(TypelessError<'a>), Cast(CastError<'a>), EmptyResolverAccess(Address), } impl<'a> From> for TestLookupError<'a> { fn from(value: TypelessError<'a>) -> Self { Self::Typeless(value) } } impl<'a> From> for TestLookupError<'a> { fn from(value: CastError<'a>) -> Self { Self::Cast(value) } } impl<'a> Display for TestLookupError<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { TestLookupError::Typeless(typeless_error) => { f.write_fmt(format_args!("typeless lookup failure: {}", typeless_error)) } TestLookupError::Cast(cast_error) => { f.write_fmt(format_args!("cast failure: {}", cast_error)) } TestLookupError::EmptyResolverAccess(address) => f.write_fmt(format_args!( "accessed an empty resolved at address {}.", address )), } } } impl<'a> Error for TestLookupError<'a> {} impl Context for TestContext { type T = classes::solo::SoloClass; type LookupError<'a> = TestLookupError<'a>; fn hash(s: &[u8]) -> Hash { let mut hasher = Sha256::new(); hasher.update(s); hasher.finalize().into() } } pub struct EmptyResolver; impl<'a> Resolver<'a, TestContext> for EmptyResolver { fn resolve(self: std::rc::Rc, address: Address) -> HashResolution<'a, TestContext> { Err(TestLookupError::EmptyResolverAccess(address)) } } pub fn reparse<'a, A: Mentionable<'a, TestContext>>(mentionable: Rc) -> A { let factory = mentionable.factory(); TypelessMentionable::from_typed(mentionable) .cast(factory) .expect("cast failed") }