depo_api/request/
get_recovery.rs1use bc_envelope::prelude::*;
2use gstp::prelude::*;
3
4use crate::{
5 Error, GET_RECOVERY_FUNCTION, Result,
6 util::{Abbrev, FlankedFunction},
7};
8
9#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct GetRecovery();
15
16impl GetRecovery {
17 pub fn new() -> Self { Self() }
18}
19
20impl Default for GetRecovery {
21 fn default() -> Self { Self::new() }
22}
23
24impl From<GetRecovery> for Expression {
25 fn from(_: GetRecovery) -> Self { Expression::new(GET_RECOVERY_FUNCTION) }
26}
27
28impl TryFrom<Expression> for GetRecovery {
29 type Error = Error;
30
31 fn try_from(_: Expression) -> Result<Self> { Ok(Self::new()) }
32}
33
34impl std::fmt::Display for GetRecovery {
35 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36 f.write_fmt(format_args!("{}", "getRecovery".flanked_function()))
37 }
38}
39
40#[derive(Debug, Clone, PartialEq, Eq)]
45pub struct GetRecoveryResult(Option<String>);
46
47impl GetRecoveryResult {
48 pub fn new(recovery: Option<String>) -> Self { Self(recovery) }
49
50 pub fn recovery(&self) -> Option<&str> { self.0.as_deref() }
51}
52
53impl From<GetRecoveryResult> for Envelope {
54 fn from(value: GetRecoveryResult) -> Self {
55 value.recovery().map_or_else(Envelope::null, Envelope::new)
56 }
57}
58
59impl TryFrom<Envelope> for GetRecoveryResult {
60 type Error = Error;
61
62 fn try_from(envelope: Envelope) -> Result<Self> {
63 let recovery = if envelope.is_null() {
64 None
65 } else {
66 Some(envelope.extract_subject().map_err(|e| {
67 Error::InvalidEnvelope {
68 message: format!(
69 "failed to extract recovery subject: {}",
70 e
71 ),
72 }
73 })?)
74 };
75 Ok(Self::new(recovery))
76 }
77}
78
79impl TryFrom<SealedResponse> for GetRecoveryResult {
80 type Error = Error;
81
82 fn try_from(response: SealedResponse) -> Result<Self> {
83 response.result()?.clone().try_into()
84 }
85}
86
87impl std::fmt::Display for GetRecoveryResult {
88 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89 f.write_fmt(format_args!(
90 "{} OK: {}",
91 "getRecovery".flanked_function(),
92 self.recovery().abbrev()
93 ))
94 }
95}
96
97#[cfg(test)]
98mod tests {
99 use indoc::indoc;
100
101 use super::*;
102
103 #[test]
104 fn test_request() {
105 bc_envelope::register_tags();
106
107 let request = GetRecovery::new();
108 let expression: Expression = request.clone().into();
109 let request_envelope = expression.to_envelope();
110 #[rustfmt::skip]
111 assert_eq!(request_envelope.format(), indoc! {r#"
112 «"getRecovery"»
113 "#}.trim());
114 let decoded_expression =
115 Expression::try_from(request_envelope).unwrap();
116 let decoded = GetRecovery::try_from(decoded_expression).unwrap();
117 assert_eq!(request, decoded);
118 }
119
120 #[test]
121 fn test_response() {
122 bc_envelope::register_tags();
123
124 let response = GetRecoveryResult::new(Some("Recovery Method".into()));
125 let response_envelope = response.to_envelope();
126 assert_eq!(
127 response_envelope.format(),
128 (indoc! {
129 r#"
130 "Recovery Method"
131 "#
132 })
133 .trim()
134 );
135 let decoded = GetRecoveryResult::try_from(response_envelope).unwrap();
136 assert_eq!(response, decoded);
137
138 let response = GetRecoveryResult::new(None);
139 let response_envelope = response.to_envelope();
140 assert_eq!(
141 response_envelope.format(),
142 (indoc! {
143 r#"
144 null
145 "#
146 })
147 .trim()
148 );
149 let decoded = GetRecoveryResult::try_from(response_envelope).unwrap();
150 assert_eq!(response, decoded);
151 }
152}