@@ -183,26 +183,28 @@ fn build_signature(
183183 Ok ( Signature :: new ( kid, sign_bytes) )
184184}
185185
186- impl TryFrom < & CatalystSignedDocument > for SignaturesBuilder {
187- type Error = anyhow:: Error ;
188-
189- fn try_from ( value : & CatalystSignedDocument ) -> Result < Self , Self :: Error > {
190- Ok ( Self {
186+ impl From < & CatalystSignedDocument > for SignaturesBuilder {
187+ fn from ( value : & CatalystSignedDocument ) -> Self {
188+ Self {
191189 metadata : value. inner . metadata . clone ( ) ,
192190 content : value. inner . content . clone ( ) ,
193191 signatures : value. inner . signatures . clone ( ) ,
194- } )
192+ }
195193 }
196194}
197195
198- #[ cfg( test) ]
199- pub ( crate ) mod tests {
196+ pub mod tests {
197+ //! Catalyst Signed Document Builder for testing purposes.
198+
200199 use cbork_utils:: with_cbor_bytes:: WithCborBytes ;
201200
201+ use super :: { CatalystSignedDocument , Content } ;
202+ use crate :: Signatures ;
203+
202204 /// A test version of the builder, which allows to build a not fully valid catalyst
203205 /// signed document
204206 #[ derive( Default ) ]
205- pub ( crate ) struct Builder {
207+ pub struct Builder {
206208 /// metadata
207209 metadata : super :: Metadata ,
208210 /// content
@@ -211,36 +213,64 @@ pub(crate) mod tests {
211213 signatures : super :: Signatures ,
212214 }
213215
216+ impl From < & CatalystSignedDocument > for Builder {
217+ fn from ( value : & CatalystSignedDocument ) -> Self {
218+ Self {
219+ metadata : value. inner . metadata . clone ( ) . inner ( ) ,
220+ content : value. inner . content . clone ( ) ,
221+ signatures : value. inner . signatures . clone ( ) ,
222+ }
223+ }
224+ }
225+
214226 impl Builder {
215227 /// Start building a signed document
216228 #[ must_use]
217- pub ( crate ) fn new ( ) -> Self {
229+ pub fn new ( ) -> Self {
218230 Self :: default ( )
219231 }
220232
221233 /// Add provided `SupportedField` into the `Metadata`.
222- pub ( crate ) fn with_metadata_field (
234+ pub fn with_metadata_field (
223235 mut self ,
224236 field : crate :: metadata:: SupportedField ,
225237 ) -> Self {
226238 self . metadata . add_field ( field) ;
227239 self
228240 }
229241
242+ /// Add provided `SupportedField` into the `Metadata`.
243+ pub fn remove_metadata_field (
244+ mut self ,
245+ field_label : crate :: metadata:: SupportedLabel ,
246+ ) -> Self {
247+ self . metadata . remove_field ( field_label) ;
248+ self
249+ }
250+
230251 /// Set the content (COSE payload) to the document builder.
231252 /// It will set the content as its provided, make sure by yourself that
232253 /// `content-type` and `content-encoding` fields are aligned with the
233254 /// provided content bytes.
234- pub ( crate ) fn with_content (
255+ pub fn with_content (
235256 mut self ,
236257 content : Vec < u8 > ,
237258 ) -> Self {
238259 self . content = content. into ( ) ;
239260 self
240261 }
241262
263+ /// Set the content (COSE payload) to CBOR nil to the document builder.
264+ /// It will set the content as its provided, make sure by yourself that
265+ /// `content-type` and `content-encoding` fields are aligned with the
266+ /// provided content bytes.
267+ pub fn with_empty_content ( mut self ) -> Self {
268+ self . content = Content :: default ( ) ;
269+ self
270+ }
271+
242272 /// Add a signature to the document
243- pub ( crate ) fn add_signature (
273+ pub fn add_signature (
244274 mut self ,
245275 sign_fn : impl FnOnce ( Vec < u8 > ) -> Vec < u8 > ,
246276 kid : super :: CatalystId ,
@@ -256,9 +286,15 @@ pub(crate) mod tests {
256286 Ok ( self )
257287 }
258288
289+ /// Clear all existing signatures.
290+ pub fn clear_signatures ( mut self ) -> anyhow:: Result < Self > {
291+ self . signatures = Signatures :: default ( ) ;
292+ Ok ( self )
293+ }
294+
259295 /// Build a signed document with the collected error report.
260296 /// Could provide an invalid document.
261- pub ( crate ) fn build ( self ) -> super :: CatalystSignedDocument {
297+ pub fn build ( self ) -> super :: CatalystSignedDocument {
262298 let metadata_bytes = minicbor:: to_vec ( self . metadata ) . unwrap ( ) ;
263299 let content_bytes = minicbor:: to_vec ( self . content ) . unwrap ( ) ;
264300 let signature_bytes = minicbor:: to_vec ( self . signatures ) . unwrap ( ) ;
0 commit comments