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
/*
    Appellation: binary <kinds>
    Contrib: FL03 <jo3mccain@icloud.com>
*/
use crate::states::StateKind;
use strum::{AsRefStr, Display, EnumCount, EnumIs, EnumIter, EnumString, VariantNames};

#[derive(
    AsRefStr,
    Clone,
    Copy,
    Debug,
    Default,
    Display,
    EnumCount,
    EnumIs,
    EnumIter,
    EnumString,
    Eq,
    Hash,
    Ord,
    PartialEq,
    PartialOrd,
    VariantNames,
)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Deserialize, serde::Serialize),
    serde(rename_all = "lowercase", untagged)
)]
#[repr(u8)]
#[strum(serialize_all = "lowercase")]
pub enum BinaryState {
    Invalid = 0,
    #[default]
    Valid = 1,
}

impl BinaryState {
    pub fn new(state: u8) -> Self {
        match state {
            1 => Self::Valid,
            _ => Self::Invalid,
        }
    }
    /// a functional variant constructor for [Invalid](BinaryState::Invalid)
    pub fn invalid() -> Self {
        Self::Invalid
    }
    /// a functional variant constructor for [Valid](BinaryState::Valid)
    pub fn valid() -> Self {
        Self::Valid
    }

    pub fn update(&mut self, state: Self) {
        *self = state;
    }
}

impl StateKind for BinaryState {}

macro_rules! impl_std_unary {
    ($($($p:ident)::*.$call:ident),*) => {
        $(impl_std_unary!(@impl $($p)::*.$call);)*
    };
    (std $($trait:ident.$call:ident),*) => {
        $(impl_std_unary!(@impl core::ops::$trait.$call);)*
    };
    (@impl $($p:ident)::*.$call:ident) => {
        impl $($p)::* for BinaryState {
            type Output = BinaryState;

            fn $call(self) -> Self::Output {
                BinaryState::new($($p)::*::$call(self as u8))
            }
        }
    }
}

macro_rules! impl_std_binary {
    ($($($p:ident)::*.$call:ident),*) => {
        $(impl_std_binary!(@impl $($p)::*.$call);)*
    };
    (std $($trait:ident.$call:ident),*) => {
        $(
            impl_std_binary!(@assign $trait.$call);
            impl_std_binary!(@impl core::ops::$trait.$call);
        )*
    };
    (@impl $($p:ident)::*.$call:ident) => {
        impl_std_binary!(@self $($p)::*.$call);
        impl_std_binary!(@primitive $($p)::*.$call);
    };

    (@self $($p:ident)::*.$call:ident) => {
        impl $($p)::* for BinaryState {
            type Output = BinaryState;

            fn $call(self, rhs: BinaryState) -> Self::Output {
                let res = $($p)::*::$call(self as u8, rhs as u8);
                BinaryState::new(res)
            }
        }

        impl<'a> $($p)::* for &'a BinaryState {
            type Output = BinaryState;

            fn $call(self, rhs: &'a BinaryState) -> Self::Output {
                let res = $($p)::*::$call(*self as u8, *rhs as u8);
                BinaryState::new(res)
            }
        }

        impl<'a> $($p)::*<BinaryState> for &'a BinaryState {
            type Output = BinaryState;

            fn $call(self, rhs: BinaryState) -> Self::Output {
                let res = $($p)::*::$call(*self as u8, rhs as u8);
                BinaryState::new(res)
            }
        }

        impl<'a> $($p)::*<&'a BinaryState> for BinaryState {
            type Output = BinaryState;

            fn $call(self, rhs: &'a BinaryState) -> Self::Output {
                let res = $($p)::*::$call(self as u8, *rhs as u8);
                BinaryState::new(res)
            }
        }
    };
    (@assign $trait:ident.$call:ident) => {
        paste::paste! {
            impl core::ops::[<$trait Assign>] for BinaryState {
                fn [<$call _assign>](&mut self, rhs: BinaryState) {
                    *self = core::ops::$trait::$call(*self, rhs);
                }
            }

            impl<'a> core::ops::[<$trait Assign>]<&'a BinaryState> for BinaryState {
                fn [<$call _assign>](&mut self, rhs: &'a BinaryState) {
                    *self = core::ops::$trait::$call(*self, rhs);
                }
            }
        }
    };

    (@primitive $($p:ident)::*.$call:ident) => {
        impl<T> $($p)::*<T> for BinaryState where u8: $($p)::*<T, Output = u8> {
            type Output = BinaryState;

            fn $call(self, rhs: T) -> Self::Output {
                let res = $($p)::*::$call(self as u8, rhs);
                BinaryState::new(res)
            }
        }

        impl<'a, T> $($p)::*<T> for &'a BinaryState where u8: $($p)::*<T, Output = u8> {
            type Output = BinaryState;

            fn $call(self, rhs: T) -> Self::Output {
                let res = $($p)::*::$call(*self as u8, rhs);
                BinaryState::new(res)
            }
        }
    };

}

macro_rules! impl_from {
    ($($t:ty),*) => {
        $(
            enum_from_primitive!(BinaryState(0: Invalid, 1: Valid)<$t>);
        )*
    };
}

impl_std_binary!(std Add.add, Div.div, Mul.mul, Rem.rem, Sub.sub);
impl_std_binary!(std BitAnd.bitand, BitOr.bitor, BitXor.bitxor, Shl.shl, Shr.shr);
impl_std_unary!(core::ops::Not.not);

impl_from!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize);