|
| 1 | +//! Catalyst Signed Document Builder. |
| 2 | +use crate::{CatalystSignedDocument, Content, InnerCatalystSignedDocument, Metadata, Signatures}; |
| 3 | + |
| 4 | +/// Catalyst Signed Document Builder. |
| 5 | +#[derive(Debug, Default)] |
| 6 | +#[allow(dead_code)] |
| 7 | +pub struct Builder { |
| 8 | + /// Document Metadata |
| 9 | + metadata: Option<Metadata>, |
| 10 | + /// Document Content |
| 11 | + content: Option<Content>, |
| 12 | + /// Signatures |
| 13 | + signatures: Option<Signatures>, |
| 14 | +} |
| 15 | + |
| 16 | +impl Builder { |
| 17 | + /// Start building a signed document |
| 18 | + #[must_use] |
| 19 | + pub fn new() -> Self { |
| 20 | + Self::default() |
| 21 | + } |
| 22 | + |
| 23 | + /// Set document metadata |
| 24 | + #[must_use] |
| 25 | + pub fn metadata(mut self, metadata: Metadata) -> Self { |
| 26 | + self.metadata = Some(metadata); |
| 27 | + self |
| 28 | + } |
| 29 | + |
| 30 | + /// Set document content |
| 31 | + #[must_use] |
| 32 | + pub fn content(mut self, content: Content) -> Self { |
| 33 | + self.content = Some(content); |
| 34 | + self |
| 35 | + } |
| 36 | + |
| 37 | + /// Set document signatures |
| 38 | + #[must_use] |
| 39 | + pub fn signatures(mut self, signatures: Signatures) -> Self { |
| 40 | + self.signatures = Some(signatures); |
| 41 | + self |
| 42 | + } |
| 43 | + |
| 44 | + /// Build a signed document |
| 45 | + /// |
| 46 | + /// ## Errors |
| 47 | + /// |
| 48 | + /// Returns |
| 49 | + pub fn build(self) -> anyhow::Result<CatalystSignedDocument> { |
| 50 | + match (self.metadata, self.content, self.signatures) { |
| 51 | + (Some(metadata), Some(content), Some(signatures)) => { |
| 52 | + Ok(CatalystSignedDocument { |
| 53 | + inner: InnerCatalystSignedDocument { |
| 54 | + metadata, |
| 55 | + content, |
| 56 | + signatures, |
| 57 | + } |
| 58 | + .into(), |
| 59 | + }) |
| 60 | + }, |
| 61 | + _ => Err(anyhow::anyhow!("Failed to build Catalyst Signed Document")), |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments