spec_driven_docs/
embedded.rs1use std::collections::BTreeSet;
10
11use include_dir::{Dir, include_dir};
12
13pub use crate::payload_roots::PAYLOAD_ROOTS;
14
15pub static SPECS: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/_docs/specs");
17pub static TEMPLATES: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/templates");
19pub static MARKDOWNLINT: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/.markdownlint");
21pub static SEEDS: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/instance/seeds");
23pub static SNIPPETS: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/instance/snippets");
25pub static METHOD: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/method");
27pub static SKILLS: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/skills");
29pub static SKILL_SHARED: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/skill-shared");
31
32pub static LICENSE: &str = include_str!("../LICENSE");
34pub static LICENSE_MIT: &str = include_str!("../LICENSE-MIT");
36pub static LICENSE_CC_BY: &str = include_str!("../LICENSE-CC-BY-4.0");
38pub static THIRD_PARTY_NOTICES: &str = include_str!("../THIRD_PARTY_NOTICES.md");
40
41const 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#[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#[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#[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#[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#[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
122pub 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}