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
use types::RispType;
use types::RispType::*;
use types::RispError;
use types::*;
use std::collections::HashMap;


impl Into<Result<RispType, RispError>> for RispType {
    fn into(self) -> Result<RispType, RispError> {
        Ok(self)
    }
}

impl Into<Result<bool, RispError>> for RispType {
    fn into(self) -> Result<bool, RispError> {
        match self {
            Bool(b) => Ok(b),
            _ => Err(error(format!("Expected Bool but got {:?}", self))),
        }
    }
}


impl Into<Result<i64, RispError>> for RispType {
    fn into(self) -> Result<i64, RispError> {
        match self {
            Int(int) => Ok(int),
            _ => Err(error(format!("Expected Int but got {:?}", self))),
        }
    }
}

impl Into<Result<String, RispError>> for RispType {
    fn into(self) -> Result<String, RispError> {
        match self {
            Str(s) => Ok(s),
            _ => Err(error(format!("Expected String but got {:?}", self))),
        }
    }
}

impl Into<Result<HashMap<String, RispType>, RispError>> for RispType {
    fn into(self) -> Result<HashMap<String, RispType>, RispError> {
        match self {
            Map(map) => Ok(map),
            _ => Err(error(format!("Expected Map but got {:?}", self)))
        }
    }
}

impl Into<Result<Vec<RispType>, RispError>> for RispType {
    fn into(self) -> Result<Vec<RispType>, RispError> {
        match self {
            Vector(vector) => Ok(vector),
            _ => Err(error(format!("Expected Vector but got {:?}", self)))
        }
    }
}

impl Into<Result<Vec<i64>, RispError>> for RispType {
    fn into(self) -> Result<Vec<i64>, RispError> {
        let vec_of_risp: Result<Vec<RispType>, _> = self.into();
        vec_of_risp?.iter().cloned()
            .map(|el| el.into())
            .collect::<Result<Vec<i64>, _>>()
    }
}


impl RispType {
    pub fn get<T>(&self, key: &str) -> Result<Option<T>, RispError> where RispType: Into<Result<T, RispError>> {
        match *self {
            Map(ref map) => {
                match map.get(key).cloned() {
                    Some(risp_value) => {
                        Ok(Some(risp_value.into()?))
                    }
                    None => Ok(None)
                }
            }
            _ => Err(error(format!("Expected Map but got {:?}", self)))
        }
    }
}


pub fn flatten_into<T>(risp_vec_input: RispType) -> Result<Vec<T>, RispError>
    where RispType: Into<Result<T, RispError>> {
    match risp_vec_input {
        Vector(vector) => {
            let flat = flatten_vec(vector);
            flat.iter().cloned()
                .map(|el| el.into())
                .collect()
        }
        _ => Err(error(format!("Expected Vector but got {:?}", risp_vec_input)))
    }
}

pub fn flatten_vec(risp_vec: Vec<RispType>) -> Vec<RispType> {
    let mut result = vec![];
    for el in risp_vec {
        if let RispType::Vector(vector) = el {
            for child_el in flatten_vec(vector) {
                result.push(child_el)
            }
        } else {
            result.push(el);
        }
    }
    result
}


/* ------------------------------ Tests ----------------------------------------------- */


#[test]
fn test_convert_int() {
    let result: Result<i64, _> = Int(3).into();
    assert_eq!(result, Ok(3));
}

#[test]
fn test_convert_int_error() {
    let result: Result<i64, RispError> = List(vec![]).into();
    assert!(result.is_err());
}


#[test]
fn test_convert_string() {
    let result: Result<String, _> = string("string").into();
    assert_eq!(result, Ok("string".to_string()));
}

#[test]
fn test_convert_string_error() {
    let result: Result<String, RispError> = List(vec![]).into();
    assert!(result.is_err());
}

#[test]
fn test_convert_vector() {
    let result: Result<Vec<_>, RispError> = Vector(vec![Int(23)]).into();
    assert_eq!(result, Ok(vec![Int(23)]));
}

#[test]
fn test_convert_vector_error() {
    let result: Result<Vec<RispType>, RispError> = Int(1).into();
    assert!(result.is_err());
}

#[test]
fn test_convert_vector_int() {
    let result: Result<Vec<i64>, RispError> = Vector(vec![Int(23)]).into();
    assert_eq!(result, Ok(vec![23]));
}

#[test]
fn test_convert_map() {
    let input_map = map(vec![
        ("key", Int(23))
    ]);
    let result: Result<HashMap<String, RispType>, RispError> = input_map.into();
    assert_eq!(result.unwrap().get("key").unwrap().clone(), Int(23));
}

#[test]
fn test_convert_map_error() {
    let result: Result<HashMap<String, RispType>, RispError> = List(vec![]).into();
    assert_eq!(result, Err(error(format!("Expected Map but got List([])"))));
}

#[test]
fn test_get() {
    let input_map = map(vec![
        ("key", Int(23))
    ]);
    let int_option = input_map.get("key").unwrap();
    assert_eq!(int_option, Some(23));
}

#[test]
fn test_get_risptype() {
    let input_map = map(vec![
        ("key", Int(23))
    ]);
    let int_option: Option<RispType> = input_map.get("key").unwrap();
    assert_eq!(int_option, Some(Int(23)));
}

#[test]
fn test_get_none() {
    let input_map = map(vec![
        ("key", Int(23))
    ]);
    let int_option: Option<i64> = input_map.get("unknown_key").unwrap();
    assert_eq!(int_option, None);
}

#[test]
fn test_get_error_expected_int() {
    let input_map = map(vec![
        ("key", string("string"))
    ]);
    let int_result: Result<Option<i64>, _> = input_map.get("key");
    assert_eq!(int_result, Err(error(format!("Expected Int but got {:?}", string("string")))));
}

#[test]
fn test_get_error_expected_map() {
    let input = Int(123);
    let int_result: Result<Option<i64>, _> = input.get("key");
    assert_eq!(int_result, Err(error(format!("Expected Map but got Int(123)"))));
}


#[test]
fn test_flatten_vec() {
    let input = vec![
        Int(1),
        Vector(vec![Int(2), Int(3)])
    ];
    let flat_result = flatten_vec(input);
    assert_eq!(flat_result, vec![Int(1), Int(2), Int(3)] );
}

#[test]
fn test_flatten_into() {
    let input = Vector(vec![
        Int(1),
        Vector(vec![Int(2), Int(3)])
    ]);
    let flat_result = flatten_into(input);
    assert_eq!(flat_result, Ok(vec![1, 2, 3]));
}

#[test]
fn test_flatten_error_outer() {
    let flat_result: Result<Vec<i64>, _> = flatten_into(Int(1));
    assert_eq!(flat_result, Err(error("Expected Vector but got Int(1)")));
}

#[test]
fn test_flatten_error_inner() {
    let input = Vector(vec![
        Int(1),
        Vector(vec![string("string"), Int(3)])
    ]);
    let flat_result: Result<Vec<i64>, _> = flatten_into(input);
    assert_eq!(flat_result, Err(error("Expected Int but got Str(\"string\")")));
}