Skip to main content

spec_driven_docs/
embedded.rs

1//! Compile-time embedded assets: the payload and the method, in the binary.
2//!
3//! Every `include_dir!`/`include_str!` in the crate lives here, and each
4//! embeds from its canonical authored path, so the repository file and the
5//! shipped copy cannot diverge — the build reads the real thing. This module
6//! only holds bytes and typed accessors; deciding where an asset lands in an
7//! instance is the profiles' and installer's business.
8
9use std::collections::BTreeSet;
10
11use include_dir::{Dir, include_dir};
12
13pub use crate::payload_roots::PAYLOAD_ROOTS;
14
15/// The spec seeds an instance adopts, and the canon-only specs beside them.
16pub static SPECS: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/_docs/specs");
17/// The stable document templates.
18pub static TEMPLATES: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/templates");
19/// The markdownlint configurations the instance receives managed.
20pub static MARKDOWNLINT: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/.markdownlint");
21/// The files an instance is seeded with once and then owns.
22pub static SEEDS: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/instance/seeds");
23/// Integration snippets a consumer copies into their own files.
24pub static SNIPPETS: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/instance/snippets");
25/// The method chapters and glossary.
26pub static METHOD: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/method");
27/// The cross-agent skills, one `SKILL.md` per directory.
28pub static SKILLS: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/skills");
29/// The artifacts every skill shares, installed once outside the skill roots.
30pub static SKILL_SHARED: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/skill-shared");
31
32/// The combined license statement naming both halves.
33pub static LICENSE: &str = include_str!("../LICENSE");
34/// The MIT license covering the distribution.
35pub static LICENSE_MIT: &str = include_str!("../LICENSE-MIT");
36/// The CC BY 4.0 license covering the method.
37pub static LICENSE_CC_BY: &str = include_str!("../LICENSE-CC-BY-4.0");
38/// The attribution notice for every third-party source the payload derives from.
39pub static THIRD_PARTY_NOTICES: &str = include_str!("../THIRD_PARTY_NOTICES.md");
40
41/// Every embedded root paired with the authored path it came from, in
42/// [`PAYLOAD_ROOTS`] order. A unit test holds the two equal, so a root
43/// embedded here but missing from the declaration — or the reverse — fails
44/// the build rather than shipping unscanned.
45const EMBEDDED_ROOTS: &[(&str, &Dir<'static>)] = &[
46    ("_docs/specs", &SPECS),
47    ("templates", &TEMPLATES),
48    (".markdownlint", &MARKDOWNLINT),
49    ("instance/seeds", &SEEDS),
50    ("instance/snippets", &SNIPPETS),
51    ("method", &METHOD),
52    ("skills", &SKILLS),
53    ("skill-shared", &SKILL_SHARED),
54];
55
56/// Every skill name, sorted; a name is the skill's directory.
57#[must_use]
58pub fn skill_names() -> Vec<&'static str> {
59    let mut names: Vec<&'static str> = SKILLS
60        .dirs()
61        .filter_map(|dir| dir.path().as_os_str().to_str())
62        .collect();
63    names.sort_unstable();
64    names
65}
66
67/// One skill's `SKILL.md` text, by skill name.
68#[must_use]
69pub fn skill(name: &str) -> Option<&'static str> {
70    SKILLS
71        .get_file(format!("{name}/SKILL.md"))
72        .and_then(include_dir::File::contents_utf8)
73}
74
75/// Every artifact the skills share, as `(path under the root, bytes)`,
76/// sorted by path.
77///
78/// These land once, outside the agent skill roots, because every skill names
79/// the same absolute path for them. A copy per skill would be one file to
80/// correct per agent root per skill; one copy is one.
81#[must_use]
82pub fn shared_artifacts() -> Vec<(String, &'static [u8])> {
83    fn walk(dir: &Dir<'static>, out: &mut Vec<(String, &'static [u8])>) {
84        for file in dir.files() {
85            if let Some(path) = file.path().to_str() {
86                out.push((path.to_string(), file.contents()));
87            }
88        }
89        for sub in dir.dirs() {
90            walk(sub, out);
91        }
92    }
93    let mut out = Vec::new();
94    walk(&SKILL_SHARED, &mut out);
95    out.sort_by(|a, b| a.0.cmp(&b.0));
96    out
97}
98
99/// Resolve a payload source path — as a profile projection names it — to its
100/// embedded bytes.
101#[must_use]
102pub fn asset(source: &str) -> Option<&'static [u8]> {
103    EMBEDDED_ROOTS.iter().find_map(|(root, dir)| {
104        let rest = source.strip_prefix(root)?.strip_prefix('/')?;
105        dir.get_file(rest).map(include_dir::File::contents)
106    })
107}
108
109/// Every `` ### `domain:rule` `` requirement address the embedded specs define.
110#[must_use]
111pub fn spec_rule_ids() -> BTreeSet<String> {
112    let mut ids = BTreeSet::new();
113    for file in SPECS.files() {
114        let Some(text) = file.contents_utf8() else {
115            continue;
116        };
117        ids.extend(rule_ids_in(text));
118    }
119    ids
120}
121
122/// The requirement addresses one spec document defines.
123pub fn rule_ids_in(text: &str) -> impl Iterator<Item = String> + '_ {
124    text.lines().filter_map(|line| {
125        let candidate = line.strip_prefix("### `")?;
126        let (id, _) = candidate.split_once('`')?;
127        let (domain, rule) = id.split_once(':')?;
128        let is_slug = |part: &str| {
129            !part.is_empty()
130                && part
131                    .bytes()
132                    .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-')
133        };
134        (is_slug(domain) && is_slug(rule)).then(|| id.to_string())
135    })
136}
137
138#[cfg(test)]
139mod tests {
140    use super::*;
141    use crate::domain::profile::ProfileId;
142    use crate::domain::rule_id::RuleId;
143
144    #[test]
145    fn rule_id_enum_matches_the_embedded_specs() {
146        let from_specs = spec_rule_ids();
147        let from_enum: BTreeSet<String> = RuleId::ALL
148            .iter()
149            .map(|rule| rule.as_str().to_string())
150            .collect();
151        assert_eq!(
152            from_specs, from_enum,
153            "RuleId and the specs disagree; update the enum and the specs together"
154        );
155    }
156
157    #[test]
158    fn the_embedded_roots_are_the_declared_payload_roots() {
159        let embedded: Vec<&str> = EMBEDDED_ROOTS.iter().map(|(root, _)| *root).collect();
160        assert_eq!(
161            embedded,
162            PAYLOAD_ROOTS.to_vec(),
163            "payload_roots.rs and the embedded statics disagree; a root missing from the declaration ships unscanned"
164        );
165    }
166
167    #[test]
168    fn every_profile_projection_resolves_to_an_embedded_asset() {
169        for id in [ProfileId::Codebase, ProfileId::KnowledgeBase] {
170            let profile = id.profile();
171            for entry in profile.managed.iter().chain(profile.adopted) {
172                assert!(
173                    asset(entry.source).is_some(),
174                    "{id}: {} is not embedded",
175                    entry.source
176                );
177            }
178        }
179    }
180
181    #[test]
182    fn the_method_and_licenses_are_carried() {
183        assert!(METHOD.get_file("glossary.md").is_some());
184        assert!(METHOD.files().count() >= 15);
185        assert!(LICENSE.contains("LICENSE-MIT") && LICENSE.contains("LICENSE-CC-BY-4.0"));
186    }
187
188    #[test]
189    fn rule_id_parser_matches_heading_shape_only() {
190        let text = "### `a-b:c-d` — Title\n### `Bad:Id`\nplain ### `x:y`\n### `no-colon`\n";
191        let ids: Vec<String> = rule_ids_in(text).collect();
192        assert_eq!(ids, vec!["a-b:c-d".to_string()]);
193    }
194}