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
use crate::value::{IntoArma, Value};

/// Convert a type to a successful or failed extension result
pub trait IntoExtResult {
    /// Convert a type to a successful or failed extension result
    fn to_ext_result(&self) -> Result<Value, Value>;
}

impl IntoExtResult for Value {
    fn to_ext_result(&self) -> Result<Value, Value> {
        Ok(self.to_owned())
    }
}

impl<T> IntoExtResult for T
where
    T: IntoArma,
{
    fn to_ext_result(&self) -> Result<Value, Value> {
        self.to_arma().to_ext_result()
    }
}

impl IntoExtResult for Result<Value, Value> {
    fn to_ext_result(&self) -> Result<Value, Value> {
        self.to_owned()
    }
}

impl<T, E> IntoExtResult for Result<T, E>
where
    T: IntoArma,
    E: IntoArma,
{
    fn to_ext_result(&self) -> Result<Value, Value> {
        match self {
            Ok(v) => Ok(v.to_arma()),
            Err(e) => Err(e.to_arma()),
        }
    }
}

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

    #[test]
    fn value() {
        assert_eq!(
            Ok(Value::Boolean(true)),
            Value::Boolean(true).to_ext_result()
        );
    }

    #[test]
    fn option_none() {
        assert_eq!(Ok(Value::Null), None::<&str>.to_ext_result());
    }

    #[test]
    fn option_some() {
        assert_eq!(
            Ok(Value::String("Hello".into())),
            Some("Hello".to_string()).to_ext_result()
        );
    }

    #[test]
    fn result_ok() {
        assert_eq!(
            Ok(Value::Number(42.0)),
            Ok(Value::Number(42.0)).to_ext_result()
        );
    }

    #[test]
    fn result_err() {
        assert_eq!(
            Err(Value::String("Hello".into())),
            Err(Value::String("Hello".into())).to_ext_result()
        );
    }

    #[test]
    fn result_unit_ok() {
        assert_eq!(Ok(Value::Null), Ok::<(), String>(()).to_ext_result());
    }

    #[test]
    fn result_unit_err() {
        assert_eq!(Err(Value::Null), Err::<String, ()>(()).to_ext_result());
    }

    #[test]
    fn result_unit_both() {
        assert_eq!(Ok(Value::Null), Ok::<(), ()>(()).to_ext_result());
    }
}