21
21
22
22
use std:: str:: FromStr ;
23
23
24
- use rgb:: { AttachId , ContractId , State , StateData } ;
25
- use strict_encoding:: { FieldName , StrictSerialize , TypeName } ;
24
+ use rgb:: { ContractId , StateData } ;
25
+ use strict_encoding:: { FieldName , SerializeError , StrictSerialize , TypeName } ;
26
26
27
- use crate :: invoice:: { Beneficiary , InvoiceState , RgbInvoice , RgbTransport , XChainNet } ;
28
- use crate :: TransportParseError ;
27
+ use crate :: { Beneficiary , RgbInvoice , RgbTransport , TransportParseError , XChainNet } ;
29
28
30
29
#[ derive( Clone , Eq , PartialEq , Debug ) ]
31
30
pub struct RgbInvoiceBuilder ( RgbInvoice ) ;
@@ -40,7 +39,7 @@ impl RgbInvoiceBuilder {
40
39
operation : None ,
41
40
assignment : None ,
42
41
beneficiary : beneficiary. into ( ) ,
43
- owned_state : InvoiceState :: Any ,
42
+ state : None ,
44
43
expiry : None ,
45
44
unknown_query : none ! ( ) ,
46
45
} )
@@ -72,106 +71,26 @@ impl RgbInvoiceBuilder {
72
71
self
73
72
}
74
73
75
- /// Set the invoiced state, which includes both state data and an optional attachment
76
- /// information.
77
- ///
78
- /// # Panics
79
- ///
80
- /// If any state information or attachment requirements are already present in the invoice.
81
- ///
82
- /// # See also
83
- ///
84
- /// - [`Self::add_state_data`], adding just state data information, not affecting attachment
85
- /// requirements;
86
- /// - [`Self::serialize_state_data`], for adding state data by serializing them from a state
87
- /// object;
88
- /// - [`Self::add_attachment`], for adding attachment requirement to an existing invoiced state
89
- /// information.
90
- pub fn set_state ( mut self , state : State ) -> Self {
91
- if !self . 0 . owned_state . is_any ( ) {
92
- panic ! ( "invoice already has state information" ) ;
93
- }
94
- self . 0 . owned_state = InvoiceState :: Specific ( state) ;
95
- self
96
- }
97
-
98
74
/// Add state data to the invoice.
99
75
///
100
- /// NB: This keeps existing attachment requirements.
101
- ///
102
- /// # Panics
103
- ///
104
- /// If the invoice already have any state information (excluding attachment requirements).
105
- ///
106
- /// # See also
107
- ///
108
- /// - [`Self::set_state`], for redefining the whole of the invoiced state, including attachment
109
- /// requirements;
110
- /// - [`Self::serialize_state_data`], for adding state data by serializing them from a state
111
- /// object;
112
- /// - [`Self::add_attachment`], for adding attachment requirement to an existing invoiced state
113
- /// information.
114
- pub fn add_state_data ( mut self , data : StateData ) -> Self {
115
- self . 0 . owned_state = match self . 0 . owned_state {
116
- InvoiceState :: Any => InvoiceState :: Specific ( State :: from ( data) ) ,
117
- InvoiceState :: Specific ( _) => panic ! ( "invoice already has state information" ) ,
118
- InvoiceState :: Attach ( attach_id) => InvoiceState :: Specific ( State :: with ( data, attach_id) ) ,
119
- } ;
76
+ /// See also [`Self::serialize_state_data`], which adds state data by serializing them from a
77
+ /// state object.
78
+ pub fn set_state ( mut self , data : StateData ) -> Self {
79
+ self . 0 . state = Some ( data) ;
120
80
self
121
81
}
122
82
123
83
/// Add state data to the invoice by strict-serializing the provided object.
124
84
///
125
- /// NB: This keeps existing attachment requirements.
126
- ///
127
85
/// Use the function carefully, since the common pitfall here is to perform double serialization
128
86
/// of an already serialized data type, like `SmallBlob`. This produces an invalid state object
129
- /// which can't be properly parsed later.
130
- ///
131
- /// # Panics
132
- ///
133
- /// If the invoice already has any state information (excluding attachment requirements).
134
- ///
135
- /// # See also
136
- ///
137
- /// - [`Self::set_state`], for redefining the whole of the invoiced state, including attachment
138
- /// requirements;
139
- /// - [`Self::add_state_data`], adding just state data information, not affecting attachment
140
- /// requirements;
141
- /// - [`Self::add_attachment`], for adding attachment requirement to an existing invoiced state
142
- /// information.
143
- pub fn serialize_state_data ( mut self , data : impl StrictSerialize ) -> Self {
144
- self . 0 . owned_state = InvoiceState :: Specific ( State :: from_serialized ( data) ) ;
145
- self
146
- }
147
-
148
- /// Add attachment requirements to an invoice, keeping the rest of the invoice state information
149
- /// unchanged.
150
- ///
151
- /// # Panics
152
- ///
153
- /// If the invoice already has attachment requirements defined.
154
- ///
155
- /// # See also
156
- ///
157
- /// - [`Self::set_state`], for redefining the whole of the invoiced state, including attachment
158
- /// requirements;
159
- /// - [`Self::add_state_data`], adding just state data information, not affecting attachment
160
- /// requirements;
161
- /// - [`Self::serialize_state_data`], for adding state data by serializing them from a state
162
- /// object;
163
- pub fn add_attachment ( mut self , attach_id : AttachId ) -> Result < Self , Self > {
164
- self . 0 . owned_state = match self . 0 . owned_state {
165
- InvoiceState :: Any => InvoiceState :: Attach ( attach_id) ,
166
- InvoiceState :: Attach ( _)
167
- | InvoiceState :: Specific ( State {
168
- attach : Some ( _) , ..
169
- } ) => panic ! ( "invoice already has attachment requirements" ) ,
170
- InvoiceState :: Specific ( mut state) => {
171
- state. attach = Some ( attach_id) ;
172
- InvoiceState :: Specific ( state)
173
- }
174
- } ;
87
+ /// which can't be properly parsed later. See also [`Self::set_state`], which sets state data
88
+ /// directly with no serialization.
89
+ pub fn serialize_state_data (
90
+ mut self ,
91
+ data : & impl StrictSerialize ,
92
+ ) -> Result < Self , SerializeError > {
93
+ self . 0 . state = Some ( StateData :: from_serialized ( data) ?) ;
175
94
Ok ( self )
176
95
}
177
96
0 commit comments