depo_api/request/
update_xid_document.rs

1use bc_components::XIDProvider;
2use bc_envelope::prelude::*;
3use bc_xid::XIDDocument;
4
5use crate::{
6    Error, NEW_XID_DOCUMENT_PARAM, NEW_XID_DOCUMENT_PARAM_NAME, Result,
7    UPDATE_XID_DOCUMENT_FUNCTION, util::FlankedFunction,
8};
9
10//
11// Request
12//
13
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct UpdateXIDDocument(XIDDocument);
16
17impl UpdateXIDDocument {
18    pub fn new(new_xid_document: XIDDocument) -> Self { Self(new_xid_document) }
19
20    pub fn new_xid_document(&self) -> &XIDDocument { &self.0 }
21}
22
23impl From<UpdateXIDDocument> for Expression {
24    fn from(value: UpdateXIDDocument) -> Self {
25        Expression::new(UPDATE_XID_DOCUMENT_FUNCTION)
26            .with_parameter(NEW_XID_DOCUMENT_PARAM, value.0)
27    }
28}
29
30impl TryFrom<Expression> for UpdateXIDDocument {
31    type Error = Error;
32
33    fn try_from(expression: Expression) -> Result<Self> {
34        let object = expression
35            .object_for_parameter(NEW_XID_DOCUMENT_PARAM)
36            .map_err(|_e| Error::MissingParameter {
37                parameter: NEW_XID_DOCUMENT_PARAM_NAME.to_string(),
38            })?;
39        let new_xid_document = XIDDocument::try_from(object).map_err(|e| {
40            Error::InvalidParameter {
41                parameter: NEW_XID_DOCUMENT_PARAM_NAME.to_string(),
42                message: format!("failed to convert to XIDDocument: {}", e),
43            }
44        })?;
45        Ok(Self::new(new_xid_document))
46    }
47}
48
49impl std::fmt::Display for UpdateXIDDocument {
50    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51        f.write_fmt(format_args!(
52            "{} new {}",
53            "updateXIDDocument".flanked_function(),
54            self.new_xid_document().xid()
55        ))
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use bc_components::{PrivateKeyBase, PublicKeysProvider};
62    use bc_rand::make_fake_random_number_generator;
63    use indoc::indoc;
64
65    use super::*;
66
67    #[test]
68    fn test_request() {
69        bc_envelope::register_tags();
70
71        let mut rng = make_fake_random_number_generator();
72        let new_xid_document: XIDDocument =
73            PrivateKeyBase::new_using(&mut rng).public_keys().into();
74
75        let request = UpdateXIDDocument::new(new_xid_document);
76        let expression: Expression = request.clone().into();
77        let request_envelope = expression.to_envelope();
78        // println!("{}", request_envelope.format());
79        #[rustfmt::skip]
80        assert_eq!(request_envelope.format(), indoc! {r#"
81            «"updateXIDDocument"» [
82                ❰"newXIDDocument"❱: XID(71274df1) [
83                    'key': PublicKeys(eb9b1cae) [
84                        'allow': 'All'
85                    ]
86                ]
87            ]
88        "#}.trim());
89        let decoded_expression =
90            Expression::try_from(request_envelope).unwrap();
91        let decoded = UpdateXIDDocument::try_from(decoded_expression).unwrap();
92        assert_eq!(request, decoded);
93    }
94}