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
use std::any::TypeId;
use crate::any::*;
use crate::{ as_trait, as_boxed, boxed_value_trait };

pub mod default;

pub trait RawTypeConverter: Value + Sync + Send {

    fn get_source_type(&self) -> TypeId;

    fn get_target_type(&self) -> TypeId;

    fn convert(&self, source: &dyn Value) -> Result<Box<dyn Value>, Box<dyn Value>>;

as_trait!(RawTypeConverter);
as_boxed!(RawTypeConverter);
}

pub trait TypeConverter<S: ?Sized + Value, T: ?Sized + Value> : RawTypeConverter {

    fn convert(&self, source: &S) -> Result<Box<T>, Box<dyn Value>>;

}

#[macro_export]
macro_rules! raw_type_converter {
    (inner $type: ty, $source_type: ty, $target_type: ty) => {
    fn get_source_type(&self) -> TypeId {
        TypeId::of::<$source_type>()
    }

    fn get_target_type(&self) -> TypeId {
        TypeId::of::<$target_type>()
    }

    fn convert(&self, source: &dyn Value) -> Result<Box<dyn Value>, Box<dyn Value>> {
        match source.as_any_ref().downcast_ref::<$source_type>() {
            Some(s) => {
                match crate::convert::TypeConverter::convert(self, s) {
                    Ok(t) => Ok(Value::to_boxed(*t)),
                    Err(err) => Err(err)
                }
            },
            None => Err(Box::new(format!("source value {:?} is of an unsupported type: {:?}, only support: {:?}",
                source, source.type_name(), std::any::type_name::<$source_type>())))
        }
    }

as_trait!(impl RawTypeConverter);
as_boxed!(impl RawTypeConverter);
    };

    ($type: ty, $source_type: ty, $target_type: ty) => {
impl crate::convert::RawTypeConverter for $type {
raw_type_converter!(inner $type, $source_type, $target_type);
}

unsafe impl Sync for $type { }
unsafe impl Send for $type { }

    };

    ($generic_type: tt; $source_type: tt; $target_type: tt) => {
impl<$source_type: ?Sized + ValueConstraint, $target_type: ?Sized + ValueConstraint>
    crate::convert::RawTypeConverter for $generic_type<$source_type, $target_type> {
raw_type_converter!(inner $generic_type, $source_type, $target_type);
}

unsafe impl<$source_type: ?Sized + ValueConstraint, $target_type: ?Sized + ValueConstraint> Sync
    for $generic_type<$source_type, $target_type> { }
unsafe impl<$source_type: ?Sized + ValueConstraint, $target_type: ?Sized + ValueConstraint>
    Send for $generic_type<$source_type, $target_type> { }

    };
}

boxed_value_trait!(RawTypeConverter);

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

    #[derive(Hash, PartialEq, Eq, Debug, Clone)]
    struct C;

    impl TypeConverter<i32, String> for C {

        fn convert(&self, source: &i32) -> Result<Box<String>, Box<dyn Value>> {
            Ok(Box::new(source.to_string()))
        }

    }

    raw_type_converter!(C, i32, String);

    #[test]
    fn type_convert() {
        match TypeConverter::convert(&C, &9) {
            Ok(s) => {
                println!("{}", s);
                assert_eq!("9".to_string(), *s);
            },
            Err(err) => {
                println!("{:?}", err);
                assert!(false);
            }
        }

        match RawTypeConverter::convert(RawTypeConverter::as_trait_ref(&C{}), Value::as_trait_ref(&9)) {
            Ok(s) => {
                println!("{:?}", s);
                assert_eq!("9".to_string(), *s.as_ref().as_any_ref().downcast_ref::<String>().unwrap());
            },
            Err(err) => {
                println!("{:?}", err);
                assert!(false);
            }
        }

        match RawTypeConverter::convert(RawTypeConverter::as_trait_ref(&C{}), Value::as_trait_ref(&"ok")) {
            Ok(s) => {
                println!("{:?}", s);
                assert!(false);
            },
            Err(err) => {
                println!("{:?}", err);
            }
        }
    }
}