1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use crate::*;
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum TextDocumentFormat<D: TextDocument> {
Complete(D),
Compact(D::CompactTextDocument_),
}
impl<D: TextDocument> TextDocumentFormat<D> {
pub fn to_compact_document(&self) -> Cow<D::CompactTextDocument_> {
match *self {
TextDocumentFormat::Complete(ref doc) => doc.to_compact_document(),
TextDocumentFormat::Compact(ref compact_doc) => Cow::Borrowed(compact_doc),
}
}
}
pub trait CompactTextDocument: Sized + Clone + PartialEq {
fn as_compact_text(&self) -> String;
}
impl<D: TextDocument> CompactTextDocument for TextDocumentFormat<D> {
fn as_compact_text(&self) -> String {
match *self {
TextDocumentFormat::Complete(ref doc) => doc.generate_compact_text(),
TextDocumentFormat::Compact(ref doc) => doc.as_compact_text(),
}
}
}
pub trait TextDocument: Document {
type CompactTextDocument_: CompactTextDocument;
fn as_text(&self) -> &str;
fn as_text_with_signatures(&self) -> String {
let mut text = self.as_text().to_string();
for sig in self.signatures() {
text.push_str(&sig.to_base64());
text.push('\n');
}
text.pop();
text
}
fn to_compact_document(&self) -> Cow<Self::CompactTextDocument_>;
fn generate_compact_text(&self) -> String {
self.to_compact_document().as_compact_text()
}
}
pub type StringAndSmallVec1<T> = (String, SmallVec<[T; 1]>);
pub trait TextDocumentBuilder {
type Document: Document;
type Signator: Signator<
Signature = <<Self::Document as Document>::PublicKey as PublicKey>::Signature,
>;
fn generate_text(&self) -> String;
fn build_signed_text(
&self,
signators: Vec<Self::Signator>,
) -> StringAndSmallVec1<<<Self::Document as Document>::PublicKey as PublicKey>::Signature> {
let text = self.generate_text();
let signatures: SmallVec<_> = {
let text_bytes = text.as_bytes();
signators
.iter()
.map(|signator| signator.sign(text_bytes))
.collect()
};
(text, signatures)
}
fn build_with_text_and_sigs(
self,
text: String,
signatures: SmallVec<
[<<Self::Document as Document>::PublicKey as PublicKey>::Signature; 1],
>,
) -> Self::Document;
}
impl<T> DocumentBuilder for T
where
T: TextDocumentBuilder,
{
type Document = <Self as TextDocumentBuilder>::Document;
type Signator = <Self as TextDocumentBuilder>::Signator;
fn build_and_sign(self, signators: Vec<Self::Signator>) -> Self::Document {
let (text, signatures) = self.build_signed_text(signators);
self.build_with_text_and_sigs(text, signatures)
}
fn build_with_signature(
self,
signatures: SmallVec<
[<<Self::Document as Document>::PublicKey as PublicKey>::Signature; 1],
>,
) -> Self::Document {
let text = self.generate_text();
self.build_with_text_and_sigs(text, signatures)
}
}