depo_api/request/
store_share.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::{STORE_SHARE_FUNCTION, DATA_PARAM, receipt::Receipt, util::{Abbrev, FlankedFunction}};

//
// Request
//

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

impl StoreShare {
    pub fn new(data: impl Into<ByteString>) -> Self {
        Self(data.into())
    }

    pub fn data(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl From<StoreShare> for Expression {
    fn from(value: StoreShare) -> Self {
        Expression::new(STORE_SHARE_FUNCTION)
            .with_parameter(DATA_PARAM, value.0)
    }
}

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

    fn try_from(expression: Expression) -> Result<Self> {
        Ok(Self::new(expression.extract_object_for_parameter::<ByteString>(DATA_PARAM)?))
    }
}

impl std::fmt::Display for StoreShare {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("{} {}",
            "storeShare".flanked_function(),
            ByteString::from(self.data()).abbrev(),
        ))
    }
}

//
// Response
//

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

impl StoreShareResult {
    pub fn new(receipt: Receipt) -> Self {
        Self(receipt)
    }

    pub fn receipt(&self) -> &Receipt {
        &self.0
    }
}

impl From<StoreShareResult> for Envelope {
    fn from(value: StoreShareResult) -> Self {
        value.0.into_envelope()
    }
}

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

    fn try_from(envelope: Envelope) -> Result<Self> {
        Ok(Self::new(Receipt::try_from(envelope)?))
    }
}

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

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

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

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

    use super::*;

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

        let data = b"data";
        let request = StoreShare::new(data);
        let expression: Expression = request.clone().into();
        let request_envelope = expression.to_envelope();
        // println!("{}", request_envelope.format());
        assert_eq!(request_envelope.format(),
        indoc! {r#"
        «"storeShare"» [
            ❰"data"❱: Bytes(4)
        ]
        "#}.trim());
        let decoded_expression = Expression::try_from(request_envelope).unwrap();
        let decoded = StoreShare::try_from(decoded_expression).unwrap();
        assert_eq!(request, decoded);
    }

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

        let user_id = XID::from_data_ref(hex_literal::hex!("8712dfac3d0ebfa910736b2a9ee39d4b68f64222a77bcc0074f3f5f1c9216d30")).unwrap();
        let data = b"data";
        let receipt = Receipt::new(&user_id, data);
        let result = StoreShareResult::new(receipt);
        let result_envelope = result.to_envelope();
        // println!("{}", result_envelope.format());
        assert_eq!(result_envelope.format(),
        indoc! {r#"
        Bytes(32) [
            'isA': "Receipt"
        ]
        "#}.trim());
        let decoded =StoreShareResult::try_from(result_envelope).unwrap();
        assert_eq!(result, decoded);
    }
}