arma_rs/
ext_result.rs

1use crate::value::{IntoArma, Value};
2
3/// Convert a type to a successful or failed extension result
4pub trait IntoExtResult {
5    /// Convert a type to a successful or failed extension result
6    fn to_ext_result(&self) -> Result<Value, Value>;
7}
8
9impl IntoExtResult for Value {
10    fn to_ext_result(&self) -> Result<Value, Value> {
11        Ok(self.to_owned())
12    }
13}
14
15impl<T> IntoExtResult for T
16where
17    T: IntoArma,
18{
19    fn to_ext_result(&self) -> Result<Value, Value> {
20        self.to_arma().to_ext_result()
21    }
22}
23
24impl IntoExtResult for Result<Value, Value> {
25    fn to_ext_result(&self) -> Result<Value, Value> {
26        self.to_owned()
27    }
28}
29
30impl<T, E> IntoExtResult for Result<T, E>
31where
32    T: IntoArma,
33    E: IntoArma,
34{
35    fn to_ext_result(&self) -> Result<Value, Value> {
36        match self {
37            Ok(v) => Ok(v.to_arma()),
38            Err(e) => Err(e.to_arma()),
39        }
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn value() {
49        assert_eq!(
50            Ok(Value::Boolean(true)),
51            Value::Boolean(true).to_ext_result()
52        );
53    }
54
55    #[test]
56    fn option_none() {
57        assert_eq!(Ok(Value::Null), None::<&str>.to_ext_result());
58    }
59
60    #[test]
61    fn option_some() {
62        assert_eq!(
63            Ok(Value::String("Hello".into())),
64            Some("Hello".to_string()).to_ext_result()
65        );
66    }
67
68    #[test]
69    fn result_ok() {
70        assert_eq!(
71            Ok(Value::Number(42.0)),
72            Ok(Value::Number(42.0)).to_ext_result()
73        );
74    }
75
76    #[test]
77    fn result_err() {
78        assert_eq!(
79            Err(Value::String("Hello".into())),
80            Err(Value::String("Hello".into())).to_ext_result()
81        );
82    }
83
84    #[test]
85    fn result_unit_ok() {
86        assert_eq!(Ok(Value::Null), Ok::<(), String>(()).to_ext_result());
87    }
88
89    #[test]
90    fn result_unit_err() {
91        assert_eq!(Err(Value::Null), Err::<String, ()>(()).to_ext_result());
92    }
93
94    #[test]
95    fn result_unit_both() {
96        assert_eq!(Ok(Value::Null), Ok::<(), ()>(()).to_ext_result());
97    }
98}