depo_api/request/
start_recovery.rs1use bc_envelope::prelude::*;
2
3use crate::{
4 Error, RECOVERY_METHOD_PARAM, RECOVERY_METHOD_PARAM_NAME, Result,
5 START_RECOVERY_FUNCTION,
6 util::{Abbrev, FlankedFunction},
7};
8
9#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct StartRecovery(String);
15
16impl StartRecovery {
17 pub fn new(recovery: String) -> Self { Self(recovery) }
18
19 pub fn recovery(&self) -> &str { self.0.as_ref() }
20}
21
22impl From<StartRecovery> for Expression {
23 fn from(value: StartRecovery) -> Self {
24 Expression::new(START_RECOVERY_FUNCTION)
25 .with_parameter(RECOVERY_METHOD_PARAM, value.0.clone())
26 }
27}
28
29impl TryFrom<Expression> for StartRecovery {
30 type Error = Error;
31
32 fn try_from(expression: Expression) -> Result<Self> {
33 Ok(Self::new(
34 expression
35 .extract_object_for_parameter(RECOVERY_METHOD_PARAM)
36 .map_err(|_e| Error::MissingParameter {
37 parameter: RECOVERY_METHOD_PARAM_NAME.to_string(),
38 })?,
39 ))
40 }
41}
42
43impl std::fmt::Display for StartRecovery {
44 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45 f.write_fmt(format_args!(
46 "{} {}",
47 "startRecovery".flanked_function(),
48 self.recovery().abbrev()
49 ))
50 }
51}
52
53#[cfg(test)]
54mod tests {
55 use indoc::indoc;
56
57 use super::*;
58
59 #[test]
60 fn test_request() {
61 bc_envelope::register_tags();
62
63 let recovery = "recovery".to_string();
64 let request = StartRecovery::new(recovery);
65 let expression: Expression = request.clone().into();
66 let request_envelope = expression.to_envelope();
67 #[rustfmt::skip]
69 assert_eq!(request_envelope.format(), indoc! {r#"
70 «"startRecovery"» [
71 ❰"recoveryMethod"❱: "recovery"
72 ]
73 "#}.trim());
74 let decoded_expression =
75 Expression::try_from(request_envelope).unwrap();
76 let decoded = StartRecovery::try_from(decoded_expression).unwrap();
77 assert_eq!(request, decoded);
78 }
79}