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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use super::{Number, NumberLike, NumberType};

use std::{
    cmp::Ordering,
    convert::TryFrom,
    hash::{Hash, Hasher},
};
use strum::ParseError;

/// Represents a primitive value
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub enum Primitive {
    Bool(bool),
    Char(char),
    Number(Number),
    Unit,
}

impl Default for PrimitiveType {
    /// Returns default primitive value type of unit
    fn default() -> Self {
        Self::Unit
    }
}

impl Primitive {
    /// Returns true if this value is of the specified type
    #[inline]
    pub fn is_type(&self, r#type: PrimitiveType) -> bool {
        self.to_type() == r#type
    }

    /// Returns the type of this value
    #[inline]
    pub fn to_type(&self) -> PrimitiveType {
        PrimitiveType::from(self)
    }

    /// Returns true if this value and the other value are of the same type
    #[inline]
    pub fn has_same_type(&self, other: &Primitive) -> bool {
        self.to_type() == other.to_type()
    }
}

impl Hash for Primitive {
    fn hash<H: Hasher>(&self, state: &mut H) {
        match self {
            Self::Bool(x) => x.hash(state),
            Self::Char(x) => x.hash(state),
            Self::Number(x) => x.hash(state),
            Self::Unit => Self::Unit.hash(state),
        }
    }
}

/// Value is considered equal, ignoring the fact that NaN != NaN for floats
impl Eq for Primitive {}

impl PartialEq for Primitive {
    /// Compares two primitive values of same type for equality, otherwise
    /// returns false
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Bool(a), Self::Bool(b)) => a == b,
            (Self::Char(a), Self::Char(b)) => a == b,
            (Self::Number(a), Self::Number(b)) => a == b,
            (Self::Unit, Self::Unit) => true,
            _ => false,
        }
    }
}

impl PartialOrd for Primitive {
    /// Compares same variants of same type for ordering, otherwise returns none
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        match (self, other) {
            (Self::Bool(a), Self::Bool(b)) => a.partial_cmp(b),
            (Self::Char(a), Self::Char(b)) => a.partial_cmp(b),
            (Self::Number(a), Self::Number(b)) => a.partial_cmp(b),
            (Self::Unit, Self::Unit) => Some(Ordering::Equal),
            _ => None,
        }
    }
}

/// Represents some data that can be converted to and from a [`Primitive`]
pub trait PrimitiveLike: Sized {
    /// Consumes this data, converting it into an abstract [`Primitive`]
    fn into_primitive(self) -> Primitive;

    /// Attempts to convert an abstract [`Primitive`] into this data, returning
    /// the owned value back if unable to convert
    fn try_from_primitive(primitive: Primitive) -> Result<Self, Primitive>;
}

impl PrimitiveLike for Primitive {
    fn into_primitive(self) -> Primitive {
        self
    }

    fn try_from_primitive(primitive: Primitive) -> Result<Self, Primitive> {
        Ok(primitive)
    }
}

impl PrimitiveLike for bool {
    fn into_primitive(self) -> Primitive {
        Primitive::Bool(self)
    }

    fn try_from_primitive(primitive: Primitive) -> Result<Self, Primitive> {
        match primitive {
            Primitive::Bool(x) => Ok(x),
            x => Err(x),
        }
    }
}

impl PrimitiveLike for char {
    fn into_primitive(self) -> Primitive {
        Primitive::Char(self)
    }

    fn try_from_primitive(primitive: Primitive) -> Result<Self, Primitive> {
        match primitive {
            Primitive::Char(x) => Ok(x),
            x => Err(x),
        }
    }
}

impl<T: NumberLike> PrimitiveLike for T {
    fn into_primitive(self) -> Primitive {
        Primitive::Number(self.into_number())
    }

    fn try_from_primitive(primitive: Primitive) -> Result<Self, Primitive> {
        match primitive {
            Primitive::Number(x) => T::try_from_number(x).map_err(Primitive::Number),
            x => Err(x),
        }
    }
}

impl PrimitiveLike for () {
    fn into_primitive(self) -> Primitive {
        Primitive::Unit
    }

    fn try_from_primitive(primitive: Primitive) -> Result<Self, Primitive> {
        match primitive {
            Primitive::Unit => Ok(()),
            x => Err(x),
        }
    }
}

impl From<()> for Primitive {
    fn from(_: ()) -> Self {
        Self::Unit
    }
}

impl TryFrom<Primitive> for () {
    type Error = Primitive;

    fn try_from(x: Primitive) -> Result<Self, Self::Error> {
        PrimitiveLike::try_from_primitive(x)
    }
}

macro_rules! impl_conv {
    ($($type:ty)+) => {$(
        impl From<$type> for Primitive {
            fn from(x: $type) -> Self {
                <$type as PrimitiveLike>::into_primitive(x)
            }
        }

        impl TryFrom<Primitive> for $type {
            type Error = Primitive;

            fn try_from(x: Primitive) -> Result<Self, Self::Error> {
                <$type as PrimitiveLike>::try_from_primitive(x)
            }
        }
    )+};
}

impl_conv!(bool char f32 f64 i128 i16 i32 i64 i8 isize u128 u16 u32 u64 u8 usize);

/// Represents primitive value types
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub enum PrimitiveType {
    Bool,
    Char,
    Number(NumberType),
    Unit,
}

impl PrimitiveType {
    pub fn is_bool(&self) -> bool {
        matches!(self, Self::Bool)
    }

    pub fn is_char(&self) -> bool {
        matches!(self, Self::Char)
    }

    pub fn is_number(&self) -> bool {
        matches!(self, Self::Number(_))
    }

    pub fn is_unit(&self) -> bool {
        matches!(self, Self::Unit)
    }

    pub fn to_number_type(&self) -> Option<NumberType> {
        match self {
            Self::Number(x) => Some(*x),
            _ => None,
        }
    }

    /// Constructs a primitive value type from a Rust-based type string similar
    /// to what you would find from `std::any::type_name`
    ///
    /// ## Examples
    ///
    /// ```
    /// use entity::{PrimitiveType as PVT, NumberType as NT};
    ///
    /// assert_eq!(
    ///     PVT::from_type_name("bool").unwrap(),
    ///     PVT::Bool,
    /// );
    ///
    /// assert_eq!(
    ///     PVT::from_type_name("char").unwrap(),
    ///     PVT::Char,
    /// );
    ///
    /// assert_eq!(
    ///     PVT::from_type_name("u8").unwrap(),
    ///     PVT::Number(NT::U8),
    /// );
    ///
    /// assert_eq!(
    ///     PVT::from_type_name("()").unwrap(),
    ///     PVT::Unit,
    /// );
    /// ```
    pub fn from_type_name(tname: &str) -> Result<Self, ParseError> {
        use std::str::FromStr;

        // Translate any Rust-specific types to our custom format, passing
        // anything that is the same to our FromStr implementation
        match tname {
            "()" => Self::from_str("unit"),
            x => Self::from_str(x),
        }
    }
}

impl std::fmt::Display for PrimitiveType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Bool => write!(f, "bool"),
            Self::Char => write!(f, "char"),
            Self::Number(t) => write!(f, "number:{}", t),
            Self::Unit => write!(f, "unit"),
        }
    }
}

impl From<Primitive> for PrimitiveType {
    fn from(v: Primitive) -> Self {
        Self::from(&v)
    }
}

impl<'a> From<&'a Primitive> for PrimitiveType {
    fn from(v: &'a Primitive) -> Self {
        match v {
            Primitive::Bool(_) => Self::Bool,
            Primitive::Char(_) => Self::Char,
            Primitive::Number(x) => Self::Number(x.to_type()),
            Primitive::Unit => Self::Unit,
        }
    }
}

impl std::str::FromStr for PrimitiveType {
    type Err = ParseError;

    /// Parses a primitive value type
    ///
    /// ## Examples
    ///
    /// ```
    /// use entity::{PrimitiveType as PVT, NumberType as NT};
    /// use strum::ParseError;
    /// use std::str::FromStr;
    ///
    /// assert_eq!(PVT::from_str("bool").unwrap(), PVT::Bool);
    /// assert_eq!(PVT::from_str("char").unwrap(), PVT::Char);
    /// assert_eq!(PVT::from_str("u32").unwrap(), PVT::Number(NT::U32));
    /// assert_eq!(PVT::from_str("number:u32").unwrap(), PVT::Number(NT::U32));
    /// assert_eq!(PVT::from_str("unit").unwrap(), PVT::Unit);
    /// assert_eq!(PVT::from_str("unknown").unwrap_err(), ParseError::VariantNotFound);
    /// ```
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut s_it = s.split(':');
        let primary = s_it.next();
        let secondary = s_it.next();
        let has_more = s_it.next().is_some();

        // If has too many values, we exit
        if has_more {
            return Err(ParseError::VariantNotFound);
        }

        match (primary, secondary) {
            (Some("bool"), None) => Ok(Self::Bool),
            (Some("char"), None) => Ok(Self::Char),
            (Some("number"), Some(x)) => Ok(Self::Number(NumberType::from_str(x)?)),
            (Some("unit"), None) => Ok(Self::Unit),
            (Some(x), None) => Ok(Self::Number(NumberType::from_str(x)?)),
            _ => Err(ParseError::VariantNotFound),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn primitive_like_can_convert_number_like_to_primitive() {
        assert!(matches!(
            1u8.into_primitive(),
            Primitive::Number(Number::U8(1)),
        ));
    }

    #[test]
    fn primitive_like_can_convert_primitive_to_number_like() {
        assert!(matches!(
            Number::try_from_primitive(Primitive::Number(Number::U8(1))),
            Ok(Number::U8(1)),
        ));

        assert!(matches!(
            Number::try_from_primitive(Primitive::Char('c')),
            Err(Primitive::Char('c')),
        ));
    }

    #[test]
    fn primitive_like_can_convert_bool_to_primitive() {
        assert!(matches!(true.into_primitive(), Primitive::Bool(true),));
    }

    #[test]
    fn primitive_like_can_convert_primitive_to_bool() {
        assert!(matches!(
            bool::try_from_primitive(Primitive::Bool(true)),
            Ok(true)
        ));

        assert!(matches!(
            bool::try_from_primitive(Primitive::Char('c')),
            Err(Primitive::Char('c')),
        ));
    }

    #[test]
    fn primitive_like_can_convert_char_to_primitive() {
        assert!(matches!('c'.into_primitive(), Primitive::Char('c')));
    }

    #[test]
    fn primitive_like_can_convert_primitive_to_char() {
        assert!(matches!(
            char::try_from_primitive(Primitive::Char('c')),
            Ok('c')
        ));

        assert!(matches!(
            char::try_from_primitive(Primitive::Bool(true)),
            Err(Primitive::Bool(true)),
        ));
    }

    #[test]
    fn primitive_like_can_convert_unit_to_primitive() {
        assert!(matches!(().into_primitive(), Primitive::Unit));
    }

    #[test]
    fn primitive_like_can_convert_primitive_to_unit() {
        assert!(matches!(<()>::try_from_primitive(Primitive::Unit), Ok(())));

        assert!(matches!(
            <()>::try_from_primitive(Primitive::Bool(true)),
            Err(Primitive::Bool(true)),
        ));
    }
}