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
//! Payload kinds (REQ-KIND-001) — what a layer entry *is*.
//!
//! A tool binary is just bytes with an exec bit; a crate, a WIT package, a
//! Zephyr module, an SDK, a wasm component are also just bytes. The kind
//! selects which export adapter and store layout apply — it does NOT change
//! how bytes are verified (every kind is a signed digest checked against the
//! trust root, exactly as a tool binary is; DD-003).
//!
//! Back-compat: an entry with no kind annotation is a `tool` (pre-kind layers,
//! as an unstamped platform means any-platform). An *unknown* kind is a hard
//! error WHERE THE KIND IS ACTED ON — `collect_verified_crates` refuses to
//! export a payload it cannot classify, rather than mishandle it.
//!
//! Scope, stated precisely because an earlier version of this comment claimed
//! more than the code does: `install` and `verify_installed` do NOT consult the
//! kind. They check the signed digest of every entry, which is what makes the
//! bytes trustworthy, and that check is kind-independent by design (DD-003). So
//! a layer deposited by a newer varve, carrying a kind this build has never
//! heard of, installs and verifies normally; only the adapters that must DO
//! something kind-specific refuse it. Consumers of `kind()` must therefore
//! handle `Err` on real installed layers — `sbom` labels such an entry rather
//! than dropping it. Whether install should refuse outright is tracked in
//! varve#49.
use std::fmt;
use std::str::FromStr;
/// The annotation carrying an entry's payload kind.
pub const ANN_KIND: &str = "eu.pulseengine.varve.kind";
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum PayloadKind {
/// An executable dispatched by `varve run` / shims (the original kind).
#[default]
Tool,
/// A Rust `.crate` tarball, consumed via `export-cargo`.
Crate,
/// A WIT interface package (`wit/` + `wit/deps/`).
Wit,
/// A Zephyr module directory (`zephyr/module.yml`).
ZephyrModule,
/// A C/C++ SDK tree (headers + libs + a cmake package).
Sdk,
/// A language toolchain — a compiler and the libraries it needs, as the
/// upstream publishes them (REQ-TOOLCHAIN-001).
///
/// NOT an `sdk`, and the difference is the one thing that matters about
/// it: an `sdk` is relocated to a prefix, and a toolchain must never be
/// patched. rustc derives its sysroot from its own executable path, which
/// is why `rustup toolchain link` works against an arbitrary directory —
/// so a toolchain is laid down and POINTED AT. Carrying the prefix rule
/// into this kind would be rewriting a compiler that did not need it.
///
/// HELD, never dispatched: `varve run rustc` would dispatch a binary out
/// of a tree whose siblings it needs, and a toolchain is reached through
/// whatever the consumer-facing path turns out to be (DD-035), not by
/// name.
Toolchain,
/// A WebAssembly component.
WasmComponent,
/// A VS Code extension package (`.vsix`), consumed via `export-vsix`
/// (REQ-VSIX-001). A `.vsix` is a single zip file, so it needs no
/// tree-shaped store — and it is DATA handed to `code`, never executed
/// by varve, so it is not dispatchable and carries no execute bit.
Vsix,
/// Documentation for the versions this layer pins (REQ-LAYERDOCS-001),
/// consumed via `export-docs`. HELD, never dispatched: it is data handed
/// to a reader, never executed, so it carries no execute bit.
///
/// What it IS — html, rustdoc, pdf, markdown, reqif — travels beside it
/// as a signed annotation rather than being inferred from the asset's
/// name, because that is what decides how it is opened.
Docs,
/// Another LAYER, composed into this one (REQ-COMPOSE-001). The digest is
/// that layer's signed manifest; it is not a blob to lay down.
Layer,
}
impl PayloadKind {
/// Every kind, so a caller that must cover all of them cannot fall
/// behind the enum.
///
/// The docs gate used to hard-code its own list of seven, which meant a
/// new kind could reach the parser and never reach the reference with
/// nothing noticing — the same shape the `ALL_KINDS` test below exists to
/// stop inside this module. Deriving beats restating.
pub const ALL: &'static [PayloadKind] = &[
PayloadKind::Tool,
PayloadKind::Crate,
PayloadKind::Wit,
PayloadKind::ZephyrModule,
PayloadKind::Sdk,
PayloadKind::Toolchain,
PayloadKind::WasmComponent,
PayloadKind::Vsix,
PayloadKind::Docs,
PayloadKind::Layer,
];
/// Is a payload of this kind dispatched BY NAME (REQ-STORE-002 clause 1)?
///
/// Only a `tool` is: `varve which`, `varve run` and the argv[0] shims all
/// resolve a bare name, so a name must resolve to exactly one binary and
/// the identity of a tool is (name, platform). Every other kind is held,
/// not dispatched — its identity is (name, version, platform), because
/// several versions of one crate is the ordinary shape of a dependency
/// graph. A `layer` is not laid down at all; it answers `false` because it
/// is certainly not dispatched by name.
pub fn is_dispatchable(self) -> bool {
matches!(self, PayloadKind::Tool)
}
/// The canonical wire string, as written in the signed annotation.
pub fn as_str(self) -> &'static str {
match self {
PayloadKind::Tool => "tool",
PayloadKind::Crate => "crate",
PayloadKind::Wit => "wit",
PayloadKind::ZephyrModule => "zephyr-module",
PayloadKind::Sdk => "sdk",
PayloadKind::Toolchain => "toolchain",
PayloadKind::WasmComponent => "wasm-component",
PayloadKind::Vsix => "vsix",
PayloadKind::Docs => "docs",
PayloadKind::Layer => "layer",
}
}
}
impl fmt::Display for PayloadKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
/// An unrecognised payload kind — varve refuses it rather than guess.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error(
"unknown payload kind '{0}': this varve does not know how to handle it \
(expected one of tool, crate, wit, zephyr-module, sdk, wasm-component, vsix)"
)]
pub struct UnknownKind(pub String);
impl FromStr for PayloadKind {
type Err = UnknownKind;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"tool" => Ok(PayloadKind::Tool),
"crate" => Ok(PayloadKind::Crate),
"wit" => Ok(PayloadKind::Wit),
"zephyr-module" => Ok(PayloadKind::ZephyrModule),
"sdk" => Ok(PayloadKind::Sdk),
"toolchain" => Ok(PayloadKind::Toolchain),
"wasm-component" => Ok(PayloadKind::WasmComponent),
"vsix" => Ok(PayloadKind::Vsix),
"docs" => Ok(PayloadKind::Docs),
"layer" => Ok(PayloadKind::Layer),
other => Err(UnknownKind(other.to_string())),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Every variant, in one place, so the tests below cannot silently skip a
/// newly added kind — which is exactly how `layer` and `vsix` reached the
/// enum with the round-trip test still listing six.
const ALL_KINDS: &[PayloadKind] = &[
PayloadKind::Tool,
PayloadKind::Crate,
PayloadKind::Wit,
PayloadKind::ZephyrModule,
PayloadKind::Sdk,
PayloadKind::Toolchain,
PayloadKind::WasmComponent,
PayloadKind::Vsix,
PayloadKind::Docs,
PayloadKind::Layer,
];
/// Position of a kind in `ALL_KINDS`. The match is EXHAUSTIVE on purpose:
/// a new variant that is not added to `ALL_KINDS` fails to COMPILE here,
/// so the round-trip and dispatchability tests always cover every kind.
fn index_in_all_kinds(k: PayloadKind) -> usize {
match k {
PayloadKind::Tool => 0,
PayloadKind::Crate => 1,
PayloadKind::Wit => 2,
PayloadKind::ZephyrModule => 3,
PayloadKind::Sdk => 4,
PayloadKind::Toolchain => 5,
PayloadKind::WasmComponent => 6,
PayloadKind::Vsix => 7,
PayloadKind::Docs => 8,
PayloadKind::Layer => 9,
}
}
// rivet: verifies REQ-KIND-001
#[test]
fn the_kind_list_the_other_tests_iterate_holds_every_variant() {
for (i, k) in ALL_KINDS.iter().enumerate() {
assert_eq!(
index_in_all_kinds(*k),
i,
"ALL_KINDS is out of step with the enum at {k}"
);
}
}
// rivet: verifies REQ-KIND-001, REQ-VSIX-001
#[test]
fn every_kind_round_trips_through_its_wire_string() {
for k in ALL_KINDS {
assert_eq!(k.as_str().parse::<PayloadKind>().unwrap(), *k);
}
// Clause 1: the wire string a deposit spec writes is `vsix`, spelled
// out rather than left to whatever `as_str` happens to return — the
// annotation is SIGNED, so renaming it silently breaks every layer
// already deposited.
assert_eq!(PayloadKind::Vsix.as_str(), "vsix");
assert_eq!("vsix".parse::<PayloadKind>().unwrap(), PayloadKind::Vsix);
assert_eq!(PayloadKind::Vsix.to_string(), "vsix");
}
// rivet: verifies REQ-KIND-001
#[test]
fn an_unknown_kind_is_refused_not_guessed() {
let err = "quantum-blob".parse::<PayloadKind>().unwrap_err();
assert_eq!(err, UnknownKind("quantum-blob".into()));
// The refusal has to say what WOULD have worked, or the depositor who
// wrote `kind = "vscode"` has nothing to correct it to.
assert!(
err.to_string().contains("vsix"),
"the hint must list every kind this varve accepts: {err}"
);
}
// rivet: verifies REQ-KIND-001
#[test]
fn the_default_kind_is_tool_for_back_compat() {
assert_eq!(PayloadKind::default(), PayloadKind::Tool);
}
// rivet: verifies REQ-STORE-002, REQ-VSIX-001
#[test]
fn only_a_tool_is_dispatched_by_name() {
// Clause 1: the identity rule follows dispatchability. A tool resolves
// by bare name through `varve run`/`which`/the shims, so one name must
// mean one binary. Nothing else is dispatched, so nothing else may be
// keyed by name alone — that is what let two versions of one crate
// overwrite each other.
assert!(PayloadKind::Tool.is_dispatchable());
for held in ALL_KINDS.iter().filter(|k| **k != PayloadKind::Tool) {
assert!(
!held.is_dispatchable(),
"{held} is not dispatched by name and must not be keyed by one"
);
}
// REQ-VSIX-001 clauses 2 and 4 both hang off this one answer: it is
// what denies a `.vsix` the execute bit in `lay_down_payloads` and
// what gives it a (name, version) identity, so two versions of one
// extension coexist. Asserted by name, not only through the loop.
assert!(
!PayloadKind::Vsix.is_dispatchable(),
"a .vsix is data handed to `code`, never a binary varve dispatches"
);
}
}