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
use crate::types::{FieldType, Primitive};

use super::TypeRegistry;

/// Exposes metadata about a type that can be used to generate other
/// versions of that type in other languages.
///
/// This is usually intended to be derived rather than manually implemented.
pub trait TypeMetadata {
    /// Populates `TypeRegistry` with this type and any of it's
    /// contained types and returns a `FieldType`
    fn metadata(registry: &mut TypeRegistry) -> FieldType;
}

impl<T> TypeMetadata for Vec<T>
where
    T: TypeMetadata,
{
    fn metadata(registry: &mut TypeRegistry) -> FieldType {
        FieldType::List(Box::new(T::metadata(registry)))
    }
}

impl<T> TypeMetadata for Option<T>
where
    T: TypeMetadata,
{
    fn metadata(registry: &mut TypeRegistry) -> FieldType {
        FieldType::Optional(Box::new(T::metadata(registry)))
    }
}

impl<K, V> TypeMetadata for std::collections::HashMap<K, V>
where
    K: TypeMetadata,
    V: TypeMetadata,
{
    fn metadata(registry: &mut TypeRegistry) -> FieldType {
        let key = Box::new(K::metadata(registry));
        let value = Box::new(V::metadata(registry));

        FieldType::Map { key, value }
    }
}

impl TypeMetadata for String {
    fn metadata(_: &mut TypeRegistry) -> FieldType {
        FieldType::Primitive(Primitive::String)
    }
}

impl TypeMetadata for str {
    fn metadata(_: &mut TypeRegistry) -> FieldType {
        FieldType::Primitive(Primitive::String)
    }
}

impl TypeMetadata for bool {
    fn metadata(_: &mut TypeRegistry) -> FieldType {
        FieldType::Primitive(Primitive::Bool)
    }
}

#[cfg(feature = "uuid")]
impl TypeMetadata for uuid::Uuid {
    fn metadata(_: &mut TypeRegistry) -> FieldType {
        FieldType::Primitive(Primitive::String)
    }
}

#[cfg(feature = "chrono")]
impl<Tz: chrono::offset::TimeZone> TypeMetadata for chrono::DateTime<Tz> {
    fn metadata(_: &mut TypeRegistry) -> FieldType {
        FieldType::Primitive(Primitive::Time)
    }
}

macro_rules! metadata_for_int {
    () => {};
    ($this:ty, $($tail:tt)*) => {
        metadata_for_int!($this);
        metadata_for_int!{$($tail)*}
    };
    ($int:ty) => {
        impl TypeMetadata for $int {
            fn metadata(_: &mut TypeRegistry) -> FieldType {
                FieldType::Primitive(Primitive::Int)
            }
        }
    };
}

metadata_for_int! {i8, i16, i32, i64, i128, u8, u16, u32, u64, u128}

macro_rules! metadata_for_float {
    () => {};
    ($this:ty, $($tail:tt)*) => {
        metadata_for_float!($this);
        metadata_for_float!{$($tail)*}
    };
    ($fl:ty) => {
        impl TypeMetadata for $fl {
            fn metadata(_: &mut TypeRegistry) -> FieldType {
                FieldType::Primitive(Primitive::Float)
            }
        }
    };
}

metadata_for_float! {f32, f64}