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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! One dispatch over every portable artifact kind.
//!
//! [`read_kind`] reads the header of a portable document with the strict
//! portable cursor: `schema` first, then `kind` when the schema carries
//! payload kinds. [`inspect_artifact`] parses the selected format without
//! replay. [`verify_artifact`] runs the selected verifier and returns one
//! [`ArtifactVerification`]. Each kind keeps its own parser, verifier, and
//! error type. This module only selects them.
//!
//! Every kind registers in one place: [`ArtifactKind`] and the matches in
//! `artifact/kinds.rs`.
mod kinds;
use crate::control::ComputationControl;
use crate::derived_artifact::ArtifactVerificationCut;
use crate::portable::{Cursor, CursorLimits};
pub use kinds::{ArtifactKind, ArtifactLimits, VerifiedArtifactValue};
/// The longest `schema` or `kind` string the header reader accepts.
const HEADER_STRING_BYTES: usize = 256;
/// Headers that a current kind replaced: the schema, the payload kind when
/// the schema has payload kinds, and the current kind.
const OBSOLETE_KINDS: [(&str, Option<&str>, ArtifactKind); 2] = [
(
crate::batch_stream::HOMOLOGICAL_STREAM_PORTABLE_SCHEMA,
Some("homological-self-pair-stream-v1"),
ArtifactKind::HomologicalStream,
),
(
crate::derived_artifact::OBSOLETE_DERIVED_ARTIFACT_SCHEMA,
None,
ArtifactKind::DerivedEquivalence,
),
];
/// A header rejection, or a rejection from the verifier of one kind.
#[derive(Clone, Debug)]
pub enum PortableArtifactError {
/// The header breaks the strict grammar at byte `byte`.
Syntax { byte: usize, message: String },
/// A header string at `path` needs `used` bytes against `limit`.
ParseLimit {
path: String,
used: usize,
limit: usize,
},
/// No registered kind uses the schema `found`.
Schema { found: String },
/// The schema is known, but no registered kind has the payload kind `found`.
Kind { schema: String, found: String },
/// `found` is a payload kind, or a schema without payload kinds, that
/// `current` replaced.
ObsoleteKind {
found: String,
current: ArtifactKind,
},
/// The derived-equivalence parser or verifier rejected the artifact.
DerivedEquivalence(crate::derived_artifact::ArtifactError),
/// The census parser or verifier rejected the value.
Census(crate::census::CensusPortableError),
/// The homological stream parser or verifier rejected the checkpoint.
HomologicalStream(crate::batch_stream::HomologicalStreamPortableError),
/// The catalog atlas parser or verifier rejected the artifact.
CatalogAtlas(crate::atlas_artifact::CatalogAtlasArtifactError),
/// The self-Ext locus parser or verifier rejected the artifact.
SelfExtLocus(crate::theorem_artifact::SelfExtLocusArtifactError),
/// The derived atlas parser or verifier rejected the artifact.
DerivedAtlas(crate::derived_classification::DerivedAtlasError),
}
from_portable_error!(PortableArtifactError);
display_error! { error PortableArtifactError {
Self::Syntax { byte, message } => "invalid portable value JSON at byte {byte}: {message}";
Self::ParseLimit { path, used, limit } => "portable value field {path} needs {used} bytes, limit {limit}";
Self::Schema { found } => "unsupported portable value schema {found:?}";
Self::Kind { schema, found } => "unsupported portable value kind {found:?} for schema {schema:?}";
Self::ObsoleteKind { found, current } => "obsolete portable value kind {found:?}, current kind {:?}", current.name();
Self::DerivedEquivalence(error) => "{error}";
Self::Census(error) => "{error}";
Self::HomologicalStream(error) => "{error}";
Self::CatalogAtlas(error) => "{error}";
Self::SelfExtLocus(error) => "{error}";
Self::DerivedAtlas(error) => "{error}";
} }
impl PortableArtifactError {
/// The kind whose parser or verifier rejected the value, when the header
/// selected one.
pub fn kind(&self) -> Option<ArtifactKind> {
match self {
Self::DerivedEquivalence(_) => Some(ArtifactKind::DerivedEquivalence),
Self::Census(_) => Some(ArtifactKind::Census),
Self::HomologicalStream(_) => Some(ArtifactKind::HomologicalStream),
Self::CatalogAtlas(_) => Some(ArtifactKind::CatalogAtlas),
Self::SelfExtLocus(_) => Some(ArtifactKind::SelfExtLocus),
Self::DerivedAtlas(_) => Some(ArtifactKind::DerivedAtlas),
_ => None,
}
}
}
/// The display data of one parsed portable value.
///
/// `summary` holds short label and value pairs in a fixed order per kind.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ArtifactReport {
kind: ArtifactKind,
fingerprint: String,
canonical_json: String,
summary: Vec<(&'static str, String)>,
complete: bool,
}
impl ArtifactReport {
accessor_methods! {
/// The kind selected by the header.
pub kind() -> ArtifactKind = |this| this.kind;
/// The stored fingerprint.
pub fingerprint() -> &str = |this| &this.fingerprint;
/// The canonical JSON rebuilt from the parsed fields.
pub canonical_json() -> &str = |this| &this.canonical_json;
/// Label and value pairs for display.
pub summary() -> &[(&'static str, String)] = |this| &this.summary;
/// Whether the value claims a complete result, not a cut or active prefix.
pub is_complete() -> bool = |this| this.complete;
}
}
/// Whether a verified value claims a complete result or a prefix.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ArtifactStatus {
/// Every claim replayed, and the value claims a complete result.
Verified,
/// Every stored claim replayed, and the value is a cut or active prefix.
VerifiedCut,
}
impl ArtifactStatus {
/// `"verified"` or `"verified-cut"`.
pub fn as_str(self) -> &'static str {
match self {
Self::Verified => "verified",
Self::VerifiedCut => "verified-cut",
}
}
}
/// A portable value accepted by the verifier of its kind.
#[derive(Clone, Debug)]
pub struct VerifiedArtifact {
report: ArtifactReport,
value: VerifiedArtifactValue,
}
impl VerifiedArtifact {
accessor_methods! {
/// The kind, fingerprint, canonical JSON, and summary.
pub report() -> &ArtifactReport = |this| &this.report;
/// The verified value with its rebuilt live data.
pub value() -> &VerifiedArtifactValue = |this| &this.value;
}
/// [`ArtifactStatus::Verified`] for a complete claim, otherwise
/// [`ArtifactStatus::VerifiedCut`].
pub fn status(&self) -> ArtifactStatus {
if self.report.complete {
ArtifactStatus::Verified
} else {
ArtifactStatus::VerifiedCut
}
}
}
/// A verified value, or a typed stop before the verifier decided.
#[derive(Clone, Debug)]
pub enum ArtifactVerification {
/// The verifier accepted the value.
Verified(Box<VerifiedArtifact>),
/// Cancellation or a verifier ceiling stopped the run. The value is
/// neither accepted nor rejected. Only derived-equivalence and derived
/// atlas replay stop.
Stopped {
kind: ArtifactKind,
cut: ArtifactVerificationCut,
},
}
/// Reads the header of `text` and returns the registered kind it names.
///
/// The header is the object's first member `schema`, then `kind` when the
/// schema carries payload kinds. Nothing after the header is read.
pub fn read_kind(text: &str) -> Result<ArtifactKind, PortableArtifactError> {
let limits = CursorLimits {
string_bytes: HEADER_STRING_BYTES,
..CursorLimits::NONE
};
let mut c = Cursor::new(text, limits)?;
c.token(b'{')?;
c.key("schema")?;
let schema = c.string("$.schema")?;
let matching: Vec<ArtifactKind> = ArtifactKind::ALL
.into_iter()
.filter(|kind| kind.schema() == schema)
.collect();
match matching.as_slice() {
[] => Err(unknown_kind(schema, None)),
[kind] if kind.payload_kind().is_none() => Ok(*kind),
_ => read_payload_kind(&mut c, schema, &matching),
}
}
fn read_payload_kind(
c: &mut Cursor,
schema: String,
matching: &[ArtifactKind],
) -> Result<ArtifactKind, PortableArtifactError> {
let found = c.next("kind", |c| c.string("$.kind"))?;
let kind = matching
.iter()
.find(|kind| kind.payload_kind() == Some(found.as_str()));
kind.copied()
.ok_or_else(|| unknown_kind(schema, Some(found)))
}
/// The obsolete-kind error for a replaced header, or the unknown schema or
/// payload kind error.
fn unknown_kind(schema: String, found: Option<String>) -> PortableArtifactError {
let obsolete = OBSOLETE_KINDS
.iter()
.find(|(old_schema, old_kind, _)| *old_schema == schema && *old_kind == found.as_deref());
match (obsolete, found) {
(Some(&(_, _, current)), found) => PortableArtifactError::ObsoleteKind {
found: found.unwrap_or(schema),
current,
},
(None, Some(found)) => PortableArtifactError::Kind { schema, found },
(None, None) => PortableArtifactError::Schema { found: schema },
}
}
/// Parses one portable value of any registered kind without replay.
///
/// The parser of the selected kind applies its parse limits from `limits`.
/// The report carries the stored fingerprint, which is not checked here.
pub fn inspect_artifact(
text: &str,
limits: &ArtifactLimits,
) -> Result<ArtifactReport, PortableArtifactError> {
kinds::inspect(read_kind(text)?, text, limits)
}
/// Parses and verifies one portable value of any registered kind.
///
/// The verifier of the selected kind applies its limits from `limits`.
/// Only derived-equivalence and derived atlas replay observe `control`. A
/// derived artifact or atlas whose declared limits exceed the verifier
/// ceilings, or whose replay is cancelled, returns
/// [`ArtifactVerification::Stopped`].
pub fn verify_artifact(
text: &str,
limits: &ArtifactLimits,
control: &ComputationControl,
) -> Result<ArtifactVerification, PortableArtifactError> {
kinds::verify(read_kind(text)?, text, limits, control)
}
#[cfg(test)]
mod tests;