//! # Design
//!
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
#![deny(elided_lifetimes_in_paths)]
#[cfg(feature = "alloc")]
extern crate alloc;
pub mod any;
mod build;
pub mod effect;
pub mod hkt;
pub mod protocol;
pub mod symbol;
mod transform;
mod walk;
// pub use build::Build;
// pub use build::Builder;
//
// pub use walk::Walk;
// pub use walk::Walker;
use core::ops::ControlFlow;
pub use build::*;
use effect::{Effect, Future};
// use protocol::{visitor::tag::TagError, Visitor};
use symbol::Symbol;
pub use transform::*;
pub use walk::*;
// #[doc(hidden)]
pub mod macros;
pub mod never {
mod sealed {
pub trait Extract {
type Never;
}
impl<T> Extract for fn() -> T {
type Never = T;
}
}
/// Test
pub type Never = <fn() -> ! as sealed::Extract>::Never;
}
pub const TAG_TYPE_NAME: Symbol = Symbol::new("Type Name");
pub const TAG_TYPE_ID: Symbol = Symbol::new("Type ID");
pub const TAG_TYPE_SIZE: Symbol = Symbol::new("Type Size");
pub const TAG_FIELD_NAMES: Symbol = Symbol::new("Field Names");
pub const TAG_KEY: Symbol = Symbol::new("Key");
pub const TAG_VALUE: Symbol = Symbol::new("Value");
pub const TAG_SEQ_INDEX: Symbol = Symbol::new("Seq Index");
pub const TAG_SEQ_LEN: Symbol = Symbol::new("Seq Length");
pub const TAG_STRUCT: Symbol = Symbol::new("Struct");
pub const TAG_MAP: Symbol = Symbol::new("Map");
pub const TAG_SEQ: Symbol = Symbol::new("Sequence");
pub const TAG_FIELD: Symbol = Symbol::new("Field");
pub const TAG_KEY_VALUE: Symbol = Symbol::new("Key Value");
pub const TAG_ENUM: Symbol = Symbol::new("Enum");
pub enum DefaultMode {}
#[derive(Clone, Copy, PartialEq, Debug)]
#[must_use]
pub enum Flow {
/// Processing should continue as normal.
Continue,
/// Processing should stop.
///
/// This state signals some error happened.
Break,
/// Processing should stop.
Done,
}
#[macro_export]
macro_rules! Walk {
{
$(#[$($attr:tt)*])*
$vis:vis struct $name:ident {$(
$field:ident: $type:ty
),* $(,)?}
} => {
const _: () = {
impl<'ctx, M: 'ctx, E: $crate::effect::Effect<'ctx>> $crate::Walk<'ctx, M, E> for &'ctx $name {
type Walker = $crate::walkers::core::r#struct::StructWalker<'ctx, $name, Info, M, E>;
fn into_walker(self) -> Self::Walker {
$crate::walkers::core::r#struct::StructWalker::new(self)
}
}
impl<'ctx> $crate::WalkerTypes for &'ctx $name {
type Error = $crate::walkers::core::r#struct::StructWalkError<FieldError<'ctx>>;
type Output = ();
}
$vis enum Info {}
#[derive(Debug)]
#[allow(non_camel_case_types)]
enum FieldErrorKind<'ctx> {$(
$field($crate::walkers::core::key_value::KeyValueError<$crate::never::Never, <&'ctx $type as $crate::WalkerTypes>::Error>)
),*}
#[derive(Debug)]
$vis struct FieldError<'ctx>(FieldErrorKind<'ctx>);
impl<'ctx, M: 'ctx> $crate::walkers::core::r#struct::StructTypeInfo<'ctx, M> for Info {
const NAME: &'static str = stringify!($name);
const FIELDS: &'static [&'static str] = &[$(stringify!($field)),*];
type FieldError = FieldError<'ctx>;
type T = $name;
#[allow(unreachable_code, non_snake_case, non_upper_case_globals, non_camel_case_types)]
fn walk_field<'a, E: $crate::effect::Effect<'ctx>>(
index: usize,
value: &'ctx Self::T,
visitor: $crate::protocol::Visitor<'a, 'ctx>,
) -> $crate::effect::Future<'a, 'ctx, Result<$crate::Flow, Self::FieldError>, E> {
mod fields {
enum Fields {$($field),*}
$(pub const $field: usize = Fields::$field as usize;)*
}
match index {
$(fields::$field => {
let key_walker = $crate::walkers::core::value::ValueWalker::new(stringify!($field));
let value_walker = <&'ctx $type as $crate::Walk::<'ctx, M, E>>::into_walker(&value.$field);
let walker = $crate::walkers::core::key_value::KeyValueWalker::<$crate::protocol::visitor::tag::TagConst<{ $crate::TAG_FIELD.to_int() }>, _, _>::new($crate::protocol::visitor::tag::TagConst, key_walker, value_walker);
E::map($crate::Walker::<'ctx, E>::walk(walker, visitor), |result| match result {
Ok(_) => {
Ok(Flow::Continue)
}
Err(err) => {
Err(FieldError(FieldErrorKind::$field(err)))
}
})
})*
_ => E::ready(Ok($crate::Flow::Done))
}
}
}
};
};
}
pub struct Demo {
pub a: bool,
pub b: bool,
}
Walk! {
pub struct Demo {
a: bool,
b: bool,
}
}
#[cfg(test)]
mod test {
use crate::effect::{BlockOn, Blocking, Spin};
use super::*;
use macro_rules_attribute::derive;
#[derive(Walk!)]
struct Demo {
a: bool,
b: bool,
other: Other,
}
#[derive(Walk!)]
struct Other {
value: bool,
}
#[test]
fn demo() {
let value = Demo {
a: true,
b: false,
other: Other { value: true },
};
let walker = Walk::<DefaultMode, Blocking>::into_walker(&value);
let mut visitor = builders::debug::Visitor::<Blocking>::new();
dbg!(Spin::block_on(Walker::<Blocking>::walk(
walker,
&mut visitor
)));
todo!();
}
}