Skip to main content

spec_driven_docs/release/
embedded.rs

1//! The release this binary carries, behind the bundle interface.
2//!
3//! Every landing verb reads through this in the ordinary case, so the whole
4//! suite exercises the boundary rather than a path only another release
5//! takes.
6
7use std::collections::BTreeMap;
8use std::sync::LazyLock;
9
10use crate::domain::ownership::Sha256;
11use crate::domain::projection::PAYLOAD_SCHEMA;
12use crate::domain::version::CanonVersion;
13use crate::error::AppError;
14use crate::release::{
15    Provenance, ReleaseBundle, ReleaseManifest, Version, blob_from, manifest_from,
16};
17
18/// This binary's own version, in the form the registry speaks.
19#[expect(
20    clippy::expect_used,
21    reason = "the crate version is a released triple; a build that says otherwise cannot ship"
22)]
23fn version() -> Version {
24    CanonVersion::current()
25        .to_string()
26        .parse()
27        .expect("the crate version parses as a semantic version")
28}
29
30/// Every embedded payload file, by the logical path the projection names.
31static FILES: LazyLock<BTreeMap<String, Vec<u8>>> = LazyLock::new(|| {
32    let mut files = BTreeMap::new();
33    for (root, dir) in crate::embedded::roots() {
34        collect(root, dir, &mut files);
35    }
36    files
37});
38
39fn collect(
40    root: &str,
41    dir: &'static include_dir::Dir<'static>,
42    files: &mut BTreeMap<String, Vec<u8>>,
43) {
44    for file in dir.files() {
45        if let Some(rest) = file.path().to_str() {
46            files.insert(format!("{root}/{rest}"), file.contents().to_vec());
47        }
48    }
49    for sub in dir.dirs() {
50        collect(root, sub, files);
51    }
52}
53
54/// The release this binary carries.
55#[derive(Debug, Clone, Copy, Default)]
56pub struct EmbeddedReleaseBundle;
57
58impl EmbeddedReleaseBundle {
59    /// The bundle this binary is.
60    #[must_use]
61    pub const fn new() -> Self {
62        Self
63    }
64}
65
66impl ReleaseBundle for EmbeddedReleaseBundle {
67    fn manifest(&self) -> Result<ReleaseManifest, AppError> {
68        Ok(manifest_from(
69            version(),
70            PAYLOAD_SCHEMA,
71            Provenance::Native,
72            None,
73            &FILES,
74            &BTreeMap::new(),
75        ))
76    }
77
78    fn blob(&self, digest: &Sha256) -> Result<Vec<u8>, AppError> {
79        blob_from(&FILES, &BTreeMap::new(), digest)
80    }
81}
82
83#[cfg(test)]
84mod tests {
85    #![allow(
86        clippy::unwrap_used,
87        reason = "a test panics as its failure signal, not as control flow"
88    )]
89
90    use super::*;
91
92    #[test]
93    fn the_embedded_bundle_carries_every_payload_root() {
94        let manifest = EmbeddedReleaseBundle::new().manifest().unwrap();
95        for root in crate::embedded::PAYLOAD_ROOTS {
96            assert!(
97                manifest
98                    .artifacts
99                    .iter()
100                    .any(|artifact| artifact.path.starts_with(&format!("{root}/"))),
101                "{root} is absent from the bundle"
102            );
103        }
104        assert_eq!(manifest.version, version());
105        assert_eq!(manifest.provenance, Provenance::Native);
106    }
107
108    #[test]
109    fn every_artifact_resolves_to_its_own_bytes() {
110        let bundle = EmbeddedReleaseBundle::new();
111        let manifest = bundle.manifest().unwrap();
112        for artifact in &manifest.artifacts {
113            let bytes = bundle.blob(&artifact.sha256).unwrap();
114            assert_eq!(Sha256::of(&bytes), artifact.sha256);
115            assert_eq!(bytes.len() as u64, artifact.bytes);
116        }
117    }
118
119    #[test]
120    fn the_declaration_reads_through_the_seam() {
121        let declaration = EmbeddedReleaseBundle::new().declaration().unwrap();
122        assert_eq!(declaration.payload_schema, PAYLOAD_SCHEMA);
123        assert_eq!(
124            declaration.managed,
125            crate::domain::profile::DECLARATION.managed
126        );
127    }
128
129    #[test]
130    fn every_projected_source_is_an_artifact() {
131        let manifest = EmbeddedReleaseBundle::new().manifest().unwrap();
132        let declaration = &crate::domain::profile::DECLARATION;
133        for entry in declaration.managed.iter().chain(&declaration.adopted) {
134            assert!(
135                manifest.digest_of(&entry.source).is_some(),
136                "{} is projected but not carried",
137                entry.source
138            );
139        }
140    }
141}