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
use bc_envelope::prelude::*;
use anyhow::{Error, Result};

use crate::{RECOVERY_METHOD_PARAM, START_RECOVERY_FUNCTION, util::{Abbrev, FlankedFunction}};

//
// Request
//

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StartRecovery (String);

impl StartRecovery {
    pub fn new(recovery: String) -> Self {
        Self(recovery)
    }

    pub fn recovery(&self) -> &str {
        self.0.as_ref()
    }
}

impl From<StartRecovery> for Expression {
    fn from(value: StartRecovery) -> Self {
        Expression::new(START_RECOVERY_FUNCTION)
            .with_parameter(RECOVERY_METHOD_PARAM, value.0.clone())
    }
}

impl TryFrom<Expression> for StartRecovery {
    type Error = Error;

    fn try_from(expression: Expression) -> Result<Self> {
        Ok(Self::new(expression.extract_object_for_parameter(RECOVERY_METHOD_PARAM)?))
    }
}

impl std::fmt::Display for StartRecovery {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("{} {}",
            "startRecovery".flanked_function(),
            self.recovery().abbrev(),
        ))
    }
}

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

    use super::*;

    #[test]
    fn test_request() {
        let recovery = "recovery".to_string();
        let request = StartRecovery::new(recovery);
        let expression: Expression = request.clone().into();
        let request_envelope = expression.to_envelope();
        // println!("{}", request_envelope.format());
        assert_eq!(request_envelope.format(), indoc! {r#"
        «"startRecovery"» [
            ❰"recoveryMethod"❱: "recovery"
        ]
        "#}.trim());
        let decoded_expression = Expression::try_from(request_envelope).unwrap();
        let decoded = StartRecovery::try_from(decoded_expression).unwrap();
        assert_eq!(request, decoded);
    }
}