depo_api/request/
get_recovery.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
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
use bc_envelope::prelude::*;
use anyhow::{Error, Result};
use gstp::prelude::*;

use crate::{GET_RECOVERY_FUNCTION, util::{Abbrev, FlankedFunction}};

//
// Request
//

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

impl GetRecovery {
    pub fn new() -> Self {
        Self ()
    }
}

impl Default for GetRecovery {
    fn default() -> Self {
        Self::new()
    }
}

impl From<GetRecovery> for Expression {
    fn from(_: GetRecovery) -> Self {
        Expression::new(GET_RECOVERY_FUNCTION)
    }
}

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

    fn try_from(_: Expression) -> Result<Self> {
        Ok(Self::new())
    }
}

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

//
// Response
//

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GetRecoveryResult(Option<String>);

impl GetRecoveryResult {
    pub fn new(recovery: Option<String>) -> Self {
        Self(recovery)
    }

    pub fn recovery(&self) -> Option<&str> {
        self.0.as_deref()
    }
}

impl From<GetRecoveryResult> for Envelope {
    fn from(value: GetRecoveryResult) -> Self {
        value.recovery().map_or_else(Envelope::null, Envelope::new)
    }
}

impl TryFrom<Envelope> for GetRecoveryResult {
    type Error = Error;

    fn try_from(envelope: Envelope) -> Result<Self> {
        let recovery = if envelope.is_null() {
            None
        } else {
            Some(envelope.extract_subject()?)
        };
        Ok(Self::new(recovery))
    }
}

impl TryFrom<SealedResponse> for GetRecoveryResult {
    type Error = Error;

    fn try_from(response: SealedResponse) -> Result<Self> {
        response.result()?.clone().try_into()
    }
}

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

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

    use super::*;

    #[test]
    fn test_request() {
        bc_envelope::register_tags();

        let request = GetRecovery::new();
        let expression: Expression = request.clone().into();
        let request_envelope = expression.to_envelope();
        assert_eq!(request_envelope.format(), indoc! {r#"
        «"getRecovery"»
        "#}.trim());
        let decoded_expression = Expression::try_from(request_envelope).unwrap();
        let decoded = GetRecovery::try_from(decoded_expression).unwrap();
        assert_eq!(request, decoded);
    }

    #[test]
    fn test_response() {
        bc_envelope::register_tags();

        let response = GetRecoveryResult::new(Some("Recovery Method".into()));
        let response_envelope = response.to_envelope();
        assert_eq!(response_envelope.format(), indoc! {r#"
        "Recovery Method"
        "#}.trim());
        let decoded = GetRecoveryResult::try_from(response_envelope).unwrap();
        assert_eq!(response, decoded);

        let response = GetRecoveryResult::new(None);
        let response_envelope = response.to_envelope();
        assert_eq!(response_envelope.format(), indoc! {r#"
        null
        "#}.trim());
        let decoded = GetRecoveryResult::try_from(response_envelope).unwrap();
        assert_eq!(response, decoded);
    }
}