depo_api/request/
update_xid_document.rs

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