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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//! The table roads: how a binding is married, and how the authored world and a staged view are closed.
use crate::descriptor::types::{
AuthoredTable, AuthoredTableName, AuthoredTableRefusal, Binding, BindingRefusal,
ExecutableAttachment, Origin, Provenance, Row, StagedTableRefusal, StagedTableView,
TablePosture, TableView, TrialKey,
};
use std::collections::BTreeSet;
impl<Invocation, Conclusion> Binding<Invocation, Conclusion> {
/// One row married to the attachment that executes it.
///
/// # Errors
///
/// Refuses a row and an attachment naming different subjects, then different checks — the marriage is what closes the seam a hidden row-to-function registry would open.
/// Refuses, last, a row carrying producer facts inside a binding that names no schema the producer emitted against.
pub fn bound(
row: Row,
attachment: ExecutableAttachment<Invocation, Conclusion>,
provenance: Provenance,
) -> Result<Self, BindingRefusal> {
if row.subject() != attachment.subject() {
return Err(BindingRefusal::SubjectMismatch {
row: row.subject(),
attachment: attachment.subject(),
});
}
if row.check() != attachment.check() {
return Err(BindingRefusal::CheckMismatch {
row: row.check(),
attachment: attachment.check(),
});
}
if let (Origin::Generated(_), Provenance::Unproduced) = (row.origin(), provenance) {
return Err(BindingRefusal::GeneratedWithoutSchemaPin);
}
Ok(Self {
row,
attachment,
provenance,
})
}
/// The row this binding carries.
#[must_use]
pub const fn row(&self) -> &Row {
&self.row
}
/// The attachment that executes it.
#[must_use]
pub const fn attachment(&self) -> &ExecutableAttachment<Invocation, Conclusion> {
&self.attachment
}
/// Whether a producer stands behind this binding, and which schema it emitted against.
#[must_use]
pub const fn provenance(&self) -> Provenance {
self.provenance
}
/// The identity that decides whether two bindings are one trial.
#[must_use]
pub const fn trial_key(&self) -> TrialKey {
self.row.trial_key()
}
}
/// Cloning copies the row, the attachment, and the provenance, for the reason the attachment's realization states.
impl<Invocation, Conclusion> Clone for Binding<Invocation, Conclusion> {
fn clone(&self) -> Self {
Self {
row: self.row.clone(),
attachment: self.attachment.clone(),
provenance: self.provenance,
}
}
}
impl<Invocation, Conclusion> AuthoredTable<Invocation, Conclusion> {
/// The complete authored world, over the bindings authored into it.
///
/// # Errors
///
/// Refuses a binding carrying the candidate origin arm, so a candidate joins the authored world only through a human's admission.
/// Refuses two bindings stating one trial, so a denominator can never read two where one thing is measured.
pub fn authored(
name: AuthoredTableName,
provenance: Provenance,
bindings: Vec<Binding<Invocation, Conclusion>>,
) -> Result<Self, AuthoredTableRefusal> {
let mut trials = BTreeSet::new();
for binding in &bindings {
let key = binding.trial_key();
if let Origin::Candidate(_) = binding.row().origin() {
return Err(AuthoredTableRefusal::CandidateOrigin(key));
}
if !trials.insert(key) {
return Err(AuthoredTableRefusal::DuplicateTrial(key));
}
}
Ok(Self {
name,
provenance,
bindings,
})
}
/// The name this world is known by.
#[must_use]
pub const fn name(&self) -> AuthoredTableName {
self.name
}
/// Whether a producer stands behind this table, and which schema it emitted against.
#[must_use]
pub const fn provenance(&self) -> Provenance {
self.provenance
}
/// Every binding this world holds, in authored order.
#[must_use]
pub fn bindings(&self) -> &[Binding<Invocation, Conclusion>] {
&self.bindings
}
/// This world as the one sealed read surface.
#[must_use]
pub const fn view(&self) -> TableView<'_, Invocation, Conclusion> {
TableView::Authored(self)
}
}
impl<'parent, Invocation, Conclusion> StagedTableView<'parent, Invocation, Conclusion> {
/// A complete authored world with candidates overlaid on it.
///
/// # Errors
///
/// Refuses an overlaid binding that does not carry the candidate origin arm, so the staging door cannot be an authoring door.
/// Refuses a candidate stating a trial the parent or another candidate already states, so uniqueness holds across both worlds at once.
pub fn staged(
parent: &'parent AuthoredTable<Invocation, Conclusion>,
candidates: Vec<Binding<Invocation, Conclusion>>,
) -> Result<Self, StagedTableRefusal> {
let mut trials: BTreeSet<TrialKey> =
parent.bindings().iter().map(Binding::trial_key).collect();
for candidate in &candidates {
let key = candidate.trial_key();
match candidate.row().origin() {
Origin::Candidate(_) => {}
Origin::HandWritten
| Origin::Generated(_)
| Origin::AdmittedReplay(_)
| Origin::AdmittedDischarge(_) => {
return Err(StagedTableRefusal::NotACandidate(key));
}
}
if !trials.insert(key) {
return Err(StagedTableRefusal::DuplicateTrial(key));
}
}
Ok(Self { parent, candidates })
}
/// The authored world this view is overlaid on.
#[must_use]
pub const fn parent(&self) -> &'parent AuthoredTable<Invocation, Conclusion> {
self.parent
}
/// The candidates overlaid, in staged order.
#[must_use]
pub fn candidates(&self) -> &[Binding<Invocation, Conclusion>] {
&self.candidates
}
/// This staged world as the one sealed read surface.
#[must_use]
pub const fn view(&self) -> TableView<'_, Invocation, Conclusion> {
TableView::Staged(self)
}
}
impl<Invocation, Conclusion> TableView<'_, Invocation, Conclusion> {
/// Every binding this view presents: the authored world in authored order, then the overlay in staged order.
pub fn bindings(&self) -> impl Iterator<Item = &Binding<Invocation, Conclusion>> {
let (authored, overlay) = match self {
Self::Authored(table) => (table.bindings(), &[][..]),
Self::Staged(staged) => (staged.parent().bindings(), staged.candidates()),
};
authored.iter().chain(overlay.iter())
}
/// Which world this view presents, and — when it is staged — the authored parent it was overlaid on.
#[must_use]
pub fn posture(&self) -> TablePosture {
match self {
Self::Authored(_) => TablePosture::Authored,
Self::Staged(staged) => TablePosture::Staged {
parent: staged.parent().name(),
},
}
}
}