depo_api/request/
store_share.rs1use bc_envelope::prelude::*;
2use anyhow::{ Error, Result };
3use gstp::prelude::*;
4
5use crate::{
6 STORE_SHARE_FUNCTION,
7 DATA_PARAM,
8 receipt::Receipt,
9 util::{ Abbrev, FlankedFunction },
10};
11
12#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct StoreShare(ByteString);
18
19impl StoreShare {
20 pub fn new(data: impl Into<ByteString>) -> Self {
21 Self(data.into())
22 }
23
24 pub fn data(&self) -> &[u8] {
25 self.0.as_ref()
26 }
27}
28
29impl From<StoreShare> for Expression {
30 fn from(value: StoreShare) -> Self {
31 Expression::new(STORE_SHARE_FUNCTION).with_parameter(DATA_PARAM, value.0)
32 }
33}
34
35impl TryFrom<Expression> for StoreShare {
36 type Error = Error;
37
38 fn try_from(expression: Expression) -> Result<Self> {
39 Ok(Self::new(expression.extract_object_for_parameter::<ByteString>(DATA_PARAM)?))
40 }
41}
42
43impl std::fmt::Display for StoreShare {
44 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45 f.write_fmt(
46 format_args!(
47 "{} {}",
48 "storeShare".flanked_function(),
49 ByteString::from(self.data()).abbrev()
50 )
51 )
52 }
53}
54
55#[derive(Debug, Clone, PartialEq, Eq)]
60pub struct StoreShareResult(Receipt);
61
62impl StoreShareResult {
63 pub fn new(receipt: Receipt) -> Self {
64 Self(receipt)
65 }
66
67 pub fn receipt(&self) -> &Receipt {
68 &self.0
69 }
70}
71
72impl From<StoreShareResult> for Envelope {
73 fn from(value: StoreShareResult) -> Self {
74 value.0.into_envelope()
75 }
76}
77
78impl TryFrom<Envelope> for StoreShareResult {
79 type Error = Error;
80
81 fn try_from(envelope: Envelope) -> Result<Self> {
82 Ok(Self::new(Receipt::try_from(envelope)?))
83 }
84}
85
86impl TryFrom<SealedResponse> for StoreShareResult {
87 type Error = Error;
88
89 fn try_from(response: SealedResponse) -> Result<Self> {
90 response.result()?.clone().try_into()
91 }
92}
93
94impl std::fmt::Display for StoreShareResult {
95 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
96 f.write_fmt(
97 format_args!(
98 "{} OK receipt {}",
99 "storeShare".flanked_function(),
100 self.receipt().abbrev()
101 )
102 )
103 }
104}
105
106#[cfg(test)]
107mod tests {
108 use bc_components::XID;
109 use indoc::indoc;
110
111 use super::*;
112
113 #[test]
114 fn test_request() {
115 bc_envelope::register_tags();
116
117 let data = b"data";
118 let request = StoreShare::new(data);
119 let expression: Expression = request.clone().into();
120 let request_envelope = expression.to_envelope();
121 #[rustfmt::skip]
123 assert_eq!(request_envelope.format(), indoc! {r#"
124 «"storeShare"» [
125 ❰"data"❱: Bytes(4)
126 ]
127 "#}.trim());
128 let decoded_expression = Expression::try_from(request_envelope).unwrap();
129 let decoded = StoreShare::try_from(decoded_expression).unwrap();
130 assert_eq!(request, decoded);
131 }
132
133 #[test]
134 fn test_response() {
135 bc_envelope::register_tags();
136
137 let user_id = XID::from_data_ref(
138 hex_literal::hex!("8712dfac3d0ebfa910736b2a9ee39d4b68f64222a77bcc0074f3f5f1c9216d30")
139 ).unwrap();
140 let data = b"data";
141 let receipt = Receipt::new(user_id, data);
142 let result = StoreShareResult::new(receipt);
143 let result_envelope = result.to_envelope();
144 #[rustfmt::skip]
146 assert_eq!(result_envelope.format(), indoc! {r#"
147 Bytes(32) [
148 'isA': "Receipt"
149 ]
150 "#}.trim());
151 let decoded = StoreShareResult::try_from(result_envelope).unwrap();
152 assert_eq!(result, decoded);
153 }
154}