dubp_documents/
membership.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 Membership documents.
17
18pub mod v10;
19
20pub use v10::{
21    MembershipDocumentV10, MembershipDocumentV10Builder, MembershipDocumentV10Stringified,
22    MembershipType,
23};
24
25use crate::*;
26
27/// Wrap an Membership document.
28///
29/// Must be created by parsing a text document or using a builder.
30#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
31pub enum MembershipDocument {
32    V10(MembershipDocumentV10),
33}
34
35impl Document for MembershipDocument {
36    type PublicKey = PubKeyEnum;
37
38    #[inline]
39    fn version(&self) -> usize {
40        match self {
41            MembershipDocument::V10(ms_v10) => ms_v10.version(),
42        }
43    }
44
45    #[inline]
46    fn currency(&self) -> &str {
47        match self {
48            MembershipDocument::V10(ms_v10) => ms_v10.currency(),
49        }
50    }
51
52    #[inline]
53    fn blockstamp(&self) -> Blockstamp {
54        match self {
55            MembershipDocument::V10(ms_v10) => ms_v10.blockstamp(),
56        }
57    }
58
59    #[inline]
60    fn issuers(&self) -> SmallVec<[Self::PublicKey; 1]> {
61        match self {
62            MembershipDocument::V10(ms_v10) => svec![PubKeyEnum::Ed25519(ms_v10.issuers()[0])],
63        }
64    }
65
66    #[inline]
67    fn signatures(&self) -> SmallVec<[<Self::PublicKey as PublicKey>::Signature; 1]> {
68        match self {
69            MembershipDocument::V10(ms_v10) => svec![Sig::Ed25519(ms_v10.signatures()[0])],
70        }
71    }
72
73    #[inline]
74    fn as_bytes(&self) -> BeefCow<[u8]> {
75        match self {
76            MembershipDocument::V10(ms_v10) => ms_v10.as_bytes(),
77        }
78    }
79}
80
81impl CompactTextDocument for MembershipDocument {
82    fn as_compact_text(&self) -> String {
83        match self {
84            MembershipDocument::V10(ms_v10) => ms_v10.as_compact_text(),
85        }
86    }
87}
88
89impl TextDocument for MembershipDocument {
90    type CompactTextDocument_ = MembershipDocument;
91
92    fn as_text(&self) -> &str {
93        match self {
94            MembershipDocument::V10(ms_v10) => ms_v10.as_text(),
95        }
96    }
97
98    fn to_compact_document(&self) -> Cow<Self::CompactTextDocument_> {
99        Cow::Borrowed(self)
100    }
101}
102
103#[derive(Clone, Debug, Deserialize, Serialize)]
104pub enum MembershipDocumentStringified {
105    V10(MembershipDocumentV10Stringified),
106}
107
108impl ToStringObject for MembershipDocument {
109    type StringObject = MembershipDocumentStringified;
110
111    fn to_string_object(&self) -> Self::StringObject {
112        match self {
113            MembershipDocument::V10(idty) => {
114                MembershipDocumentStringified::V10(idty.to_string_object())
115            }
116        }
117    }
118}