1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use crate::hlist::{Cons, HList, Nil, Shuffle};

pub use core::marker::PhantomData;

pub use macros::Transform;

mod deep_transform;
pub use deep_transform::{DeepTransform, DeepTransformFrom};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Named<T, Name: 'static>(pub T, PhantomData<Name>);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Unnamed<T>(pub T);

impl<T, Name: 'static> Named<T, Name> {
    pub const fn new(value: T) -> Self { Self(value, PhantomData) }
}

#[macro_export]
macro_rules! field {
    ($field_name:ident) => {
        $crate::macros::name! {$crate $field_name}
    };
}

#[macro_export]
macro_rules! Anon {
    (
        $($($field_name:ident: $field_ty:ty),+ $(, @$rest:ty)? $(,)?)?
    ) => {
        $crate::HList!($($($crate::anon::Named::<$field_ty, $crate::field!($field_name)>),* $(, @$rest)?)?)
    };
    (
        $($($field_ty:ty),+ $(, @$rest:ty)? $(,)?)?
    ) => {
        $crate::HList!($($($crate::anon::Unnamed::<$field_ty>),* $(, @$rest)?)?)
    };
}

#[macro_export]
macro_rules! anon {
    (
        $($($field_name:ident = $field_val:expr),+ $(,)?)?
    ) => {
        $crate::hlist!($($($crate::anon::Named::<_, $crate::field!($field_name)>::new($field_val)),*)?)
    };
    (
        $($($field_val:expr),+ $(,)?)?
    ) => {
        $crate::hlist!($($($crate::anon::Unnamed($field_val)),*)?)
    };
}

pub trait AnonType: HList {}
impl AnonType for Nil {}
impl<T, R: AnonType, Name> AnonType for Cons<Named<T, Name>, R> {}
impl<T, R: AnonType> AnonType for Cons<Unnamed<T>, R> {}

impl<T> Access for T {}
pub trait Access {
    fn field<N, T, Name: 'static>(&self) -> &T
    where
        Self: crate::hlist::Access<Named<T, Name>, N>,
    {
        &self.get().0
    }

    fn field_mut<N, T, Name: 'static>(&mut self) -> &mut T
    where
        Self: crate::hlist::Access<Named<T, Name>, N>,
    {
        &mut self.get_mut().0
    }

    fn take_field<N, T, Name>(self) -> (T, Self::Remainder)
    where
        Self: crate::hlist::Access<Named<T, Name>, N>,
    {
        let (field, rest) = self.take();
        (field.0, rest)
    }
}

pub trait RemoveField<N, I> {
    type Value;
    type Remainder;

    fn remove_field(self) -> (Self::Value, Self::Remainder);
}

impl<T, Name, R> RemoveField<Name, crate::peano::Zero> for Cons<Named<T, Name>, R> {
    type Value = T;
    type Remainder = R;

    fn remove_field(self) -> (Self::Value, Self::Remainder) { (self.value.0, self.rest) }
}

impl<T, Name, R: RemoveField<Name, N>, N> RemoveField<Name, crate::peano::Succ<N>> for Cons<T, R> {
    type Value = R::Value;
    type Remainder = Cons<T, R::Remainder>;

    fn remove_field(self) -> (Self::Value, Self::Remainder) {
        let (value, rest) = self.rest.remove_field();
        (value, Cons {
            value: self.value,
            rest,
        })
    }
}

pub trait Transform: Sized {
    type Canon: AnonType;

    fn from_canon(anon: Self::Canon) -> Self;

    fn into_canon(self) -> Self::Canon;

    fn transform<O: Transform, N>(self) -> O
    where
        Self::Canon: Shuffle<O::Canon, N>,
    {
        O::from_canon(self.into_canon().shuffle())
    }

    fn deep_transform<O: Transform, N>(self) -> O
    where
        Self::Canon: DeepTransform<O::Canon, N>,
    {
        O::from_canon(self.into_canon().deep_transform())
    }
}

impl Transform for Nil {
    type Canon = Self;

    fn from_canon(anon: Self::Canon) -> Self { anon }

    fn into_canon(self) -> Self::Canon { self }
}

impl<T, R> Transform for Cons<T, R>
where
    Self: AnonType,
{
    type Canon = Self;

    fn from_canon(anon: Self::Canon) -> Self { anon }

    fn into_canon(self) -> Self::Canon { self }
}

impl<T: AnonType> IntoNamed for T {}
pub trait IntoNamed: Sized + AnonType {
    fn into_named<T, N>(self) -> T
    where
        Self: Shuffle<T::Canon, N>,
        T: Transform,
    {
        T::from_canon(self.shuffle())
    }
}

#[doc(hidden)]
#[macro_export]
macro_rules! character {
    ($($c:ident,)*) => {
        $crate::HList!($($crate::anon::character::$c),*)
    };
}

#[allow(non_camel_case_types)]
pub mod character {
    macro_rules! build_character {
        ($($c:ident)*) => {$(
            #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
            pub enum $c {}
        )*};
    }

    build_character! {
        a b c d e f g h i j k l m n o p q r s t u v w x y z
        A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
        _0 _1 _2 _3 _4 _5 _6 _7 _8 _9 __
    }
}