dubp_documents/
certification.rs

1//  Copyright (C) 2020  Éloïs SANCHEZ.
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU Affero General Public License as
5// published by the Free Software Foundation, either version 3 of the
6// License, or (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11// GNU Affero General Public License for more details.
12//
13// You should have received a copy of the GNU Affero General Public License
14// along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
16//! Wrappers around Certification documents.
17
18pub mod v10;
19
20pub use v10::{
21    CertificationDocumentV10, CertificationDocumentV10Stringified, CompactCertificationDocumentV10,
22};
23
24use crate::*;
25
26/// Wrap an Certification document.
27///
28/// Must be created by parsing a text document or using a builder.
29#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
30pub enum CertificationDocument {
31    /// Certification document v10
32    V10(CertificationDocumentV10),
33}
34
35/// Wrap an Compact certification document (in block content)
36#[derive(Debug, Copy, Clone, Deserialize, Serialize, PartialEq, Eq)]
37pub enum CompactCertificationDocument {
38    /// Compact certification document v10
39    V10(CompactCertificationDocumentV10),
40}
41
42impl Document for CertificationDocument {
43    type PublicKey = PubKeyEnum;
44
45    #[inline]
46    fn version(&self) -> usize {
47        match self {
48            CertificationDocument::V10(cert_v10) => cert_v10.version(),
49        }
50    }
51
52    #[inline]
53    fn currency(&self) -> &str {
54        match self {
55            CertificationDocument::V10(cert_v10) => cert_v10.currency(),
56        }
57    }
58
59    #[inline]
60    fn blockstamp(&self) -> Blockstamp {
61        match self {
62            CertificationDocument::V10(cert_v10) => cert_v10.blockstamp(),
63        }
64    }
65
66    #[inline]
67    fn issuers(&self) -> SmallVec<[Self::PublicKey; 1]> {
68        match self {
69            CertificationDocument::V10(cert_v10) => {
70                svec![PubKeyEnum::Ed25519(cert_v10.issuers()[0])]
71            }
72        }
73    }
74
75    #[inline]
76    fn signatures(&self) -> SmallVec<[<Self::PublicKey as PublicKey>::Signature; 1]> {
77        match self {
78            CertificationDocument::V10(cert_v10) => svec![Sig::Ed25519(cert_v10.signatures()[0])],
79        }
80    }
81
82    #[inline]
83    fn as_bytes(&self) -> BeefCow<[u8]> {
84        match self {
85            CertificationDocument::V10(cert_v10) => cert_v10.as_bytes(),
86        }
87    }
88}
89
90impl TextDocument for CertificationDocument {
91    type CompactTextDocument_ = CompactCertificationDocument;
92
93    fn as_text(&self) -> &str {
94        match self {
95            CertificationDocument::V10(cert_v10) => cert_v10.as_text(),
96        }
97    }
98
99    fn to_compact_document(&self) -> Cow<Self::CompactTextDocument_> {
100        match self {
101            CertificationDocument::V10(cert_v10) => Cow::Owned(CompactCertificationDocument::V10(
102                cert_v10.to_compact_document().into_owned(),
103            )),
104        }
105    }
106}
107
108impl CompactTextDocument for CompactCertificationDocument {
109    fn as_compact_text(&self) -> String {
110        match self {
111            Self::V10(compact_cert_v10) => compact_cert_v10.as_compact_text(),
112        }
113    }
114}
115
116#[derive(Clone, Debug, Deserialize, Serialize)]
117pub enum CertificationDocumentStringified {
118    V10(CertificationDocumentV10Stringified),
119}
120
121impl ToStringObject for CertificationDocument {
122    type StringObject = CertificationDocumentStringified;
123
124    fn to_string_object(&self) -> Self::StringObject {
125        match self {
126            CertificationDocument::V10(idty) => {
127                CertificationDocumentStringified::V10(idty.to_string_object())
128            }
129        }
130    }
131}