|
1 | 1 | //! Catalyst documents signing crate |
| 2 | +use std::{convert::TryFrom, sync::Arc}; |
| 3 | + |
| 4 | +/// Keep all the contents private. |
| 5 | +/// Better even to use a structure like this. Wrapping in an Arc means we don't have to |
| 6 | +/// manage the Arc anywhere else. These are likely to be large, best to have the Arc be |
| 7 | +/// non-optional. |
| 8 | +pub struct CatalystSignedDocument { |
| 9 | + /// Catalyst Signed Document metadata, raw doc, with content errors. |
| 10 | + inner: Arc<InnerCatalystSignedDocument>, |
| 11 | + /// Content Errors found when parsing the Document |
| 12 | + content_errors: Vec<String>, |
| 13 | +} |
| 14 | + |
| 15 | +/// Inner type that holds the Catalyst Signed Document with parsing errors. |
| 16 | +struct InnerCatalystSignedDocument { |
| 17 | + /// Document Metadata |
| 18 | + _metadata: Metadata, |
| 19 | + /// Raw payload |
| 20 | + _raw_doc: Vec<u8>, |
| 21 | +} |
| 22 | + |
| 23 | +/// Document Metadata. |
| 24 | +#[derive(Debug, serde::Deserialize)] |
| 25 | +pub struct Metadata { |
| 26 | + /// Document Type `UUIDv7`. |
| 27 | + pub r#type: uuid::Uuid, |
| 28 | + /// Document ID `UUIDv7`. |
| 29 | + pub id: uuid::Uuid, |
| 30 | + /// Document Version `UUIDv7`. |
| 31 | + pub ver: uuid::Uuid, |
| 32 | + /// Reference to the latest document. |
| 33 | + pub r#ref: Option<DocumentRef>, |
| 34 | + /// Reference to the document template. |
| 35 | + pub template: Option<DocumentRef>, |
| 36 | + /// Reference to the document reply. |
| 37 | + pub reply: Option<DocumentRef>, |
| 38 | + /// Reference to the document section. |
| 39 | + pub section: Option<String>, |
| 40 | +} |
| 41 | + |
| 42 | +/// Reference to a Document. |
| 43 | +#[derive(Debug, serde::Deserialize)] |
| 44 | +#[serde(untagged)] |
| 45 | +pub enum DocumentRef { |
| 46 | + /// Reference to the latest document |
| 47 | + Latest { |
| 48 | + /// Document ID UUID |
| 49 | + id: uuid::Uuid, |
| 50 | + }, |
| 51 | + /// Reference to the specific document version |
| 52 | + WithVer { |
| 53 | + /// Document ID UUID |
| 54 | + id: uuid::Uuid, |
| 55 | + /// Document Version UUID |
| 56 | + ver: uuid::Uuid, |
| 57 | + }, |
| 58 | +} |
| 59 | + |
| 60 | +// Do this instead of `new` if we are converting a single parameter into a struct/type we |
| 61 | +// should use either `From` or `TryFrom` and reserve `new` for cases where we need |
| 62 | +// multiple parameters to actually create the type. This is much more elegant to use this |
| 63 | +// way, in code. |
| 64 | +impl TryFrom<Vec<u8>> for CatalystSignedDocument { |
| 65 | + type Error = &'static str; |
| 66 | + |
| 67 | + fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> { |
| 68 | + todo!(); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl CatalystSignedDocument { |
| 73 | + /// Invalid Doc Type UUID |
| 74 | + const INVALID_UUID: uuid::Uuid = uuid::Uuid::from_bytes([0x00; 16]); |
| 75 | + |
| 76 | + // A bunch of getters to access the contents, or reason through the document, such as. |
| 77 | + |
| 78 | + /// Are there any validation errors (as opposed to structural errors. |
| 79 | + #[must_use] |
| 80 | + pub fn has_error(&self) -> bool { |
| 81 | + !self.content_errors.is_empty() |
| 82 | + } |
| 83 | + |
| 84 | + /// Return Document Type UUID. |
| 85 | + #[must_use] |
| 86 | + pub fn doc_type(&self) -> uuid::Uuid { |
| 87 | + INVALID_UUID |
| 88 | + } // Can compare it against INVALID_DOC_TYPE to see if its valid or not. |
| 89 | +} |
0 commit comments