teo_runtime/value/convert/from/
error.rs

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
use indexmap::indexmap;
use teo_result::Error;
use crate::value::Value;

impl From<Error> for Value {

    fn from(value: Error) -> Value {
        Value::from(&value)
    }
}

impl From<&Error> for Value {

    fn from(value: &Error) -> Value {
        let errors = value.errors.as_ref().map(|f| {
            let mut result = indexmap! {};
            for (k, v) in f {
                result.insert(k.to_string(), Value::String(v.to_string()));
            }
            Value::Dictionary(result)
        }) ;
        let mut retval = Value::Dictionary(indexmap! {
            "type".to_string() => Value::String(value.inferred_title().to_string()),
            "message".to_string() => Value::String(value.message.clone()),
        });
        if errors.is_some() {
            retval.as_dictionary_mut().unwrap().insert("errors".to_owned(), errors.unwrap());
        }
        retval
    }
}