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
///ViperusValue encaspule data values of type String,i32 and bool
///
///implements bidirectional conversion to respective  values via Into<T> and From<T>
/// # Example
/// ```
/// use viperus::ViperusValue;
/// let x:i32=ViperusValue::I32(42).into();
/// ```
///
#[derive(Debug, PartialEq, Clone)]
pub enum ViperusValue {
    Empty,
    Str(String),
    I32(i32),
    BOOL(bool),
}

impl Into<bool> for &ViperusValue {
    fn into(self) -> bool {
        match self {
        ViperusValue::BOOL(i) => *i,
        ViperusValue::Str(s) => s.parse().expect("not a bool"),
        _=> panic!("not a bool")
        }
    }
}

impl Into<bool> for ViperusValue {
    fn into(self) -> bool {
        match self {
        ViperusValue::BOOL(i) => i,
        ViperusValue::Str(s) => s.parse().expect("not a bool"),

        _ =>  panic!("not a bool {:?}",self),
        
    }
}
}

impl From<bool> for ViperusValue {
    fn from(src: bool) -> ViperusValue {
        ViperusValue::BOOL(src)
    }
}

impl From<i32> for ViperusValue {
    fn from(src: i32) -> ViperusValue {
        ViperusValue::I32(src)
    }
}

impl Into<i32> for &ViperusValue {
    fn into(self) -> i32 {
        match self {
            ViperusValue::I32(i) => *i,
            ViperusValue::Str(s) => s.parse().expect("not an i32"),
            _ => panic!("not an i32"),
        }
    }
}

impl Into<i32> for ViperusValue {
    fn into(self) -> i32 {
        match self {
            ViperusValue::I32(i) => i,
           
            ViperusValue::Str(s) => s.parse().expect("not an i32"),
            _ => panic!("not an i32"),
        }
    }
}

impl From<String> for ViperusValue {
    fn from(src: String) -> ViperusValue {
        ViperusValue::Str(src)
    }
}

impl<'a> From<&'a String> for ViperusValue {
    fn from(src: &'a String) -> ViperusValue {
        ViperusValue::Str(src.clone())
    }
}

impl From<&str> for ViperusValue {
    fn from(src: &str) -> ViperusValue {
        ViperusValue::Str(src.to_owned())
    }
}

impl<'a> Into<&'a str> for &'a ViperusValue {
    fn into(self) -> &'a str {
        match self { 
        ViperusValue::Str(i) => i,
        _ => panic!("not an str")
        }
    }
}

impl<'a> Into<String> for &'a ViperusValue {
    fn into(self) -> String {
        match self {
        ViperusValue::Str(i) => i.clone(),
            _ => panic!("not an str")
        }
    
    }
}

impl Into<String> for ViperusValue {
    fn into(self) -> String {
        match self  {
        ViperusValue::Str(i) => i,
        _=> panic!("not a string")
        }
    }
}

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

    fn init() {
        let _ = env_logger::builder().is_test(true).try_init();
    }

    #[test]
    #[should_panic]
    fn invalid_cast_mv2bool() {
        init();

        let mv = ViperusValue::Empty;
        let _b: bool = mv.into();
    }

    #[test]
    #[should_panic]
    fn invalid_cast_refmv2bool() {
        init();

        let mv = &ViperusValue::Empty;
        let _b: bool = mv.into();
    }

    #[test]
    #[should_panic]
    fn invalid_cast_mv2i32() {
        init();

        let mv = &ViperusValue::Empty;
        let _b: i32 = mv.into();
    }
    #[test]
    fn valid_cast_mv2bool() {
        init();

        let mv = ViperusValue::BOOL(true);
        let b: bool = mv.into();
        assert!(b);
    }

    #[test]
    fn valid_cast_str2mv() {
        init();

        let mv = ViperusValue::from("hello world!");
        match mv {
        ViperusValue::Str(s) =>  assert_eq!(s, "hello world!"),
        _ => panic!("something very wrong"),
        }

        let refmv = ViperusValue::from(&("hello world!".to_owned()));
        match refmv {
        ViperusValue::Str(s) => assert_eq!(s, "hello world!"),
        _ => panic!("something very wrong")
        }
    }

    #[test]
    #[should_panic]
    fn invalid_cast_mv2string() {
        init();

        let mv = &ViperusValue::Empty;
        let _b: String = mv.into();
    }
}