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 SNIPPETS: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/instance/snippets");
23pub static METHOD: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/method");
25pub static SKILLS: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/skills");
27pub static SKILL_SHARED: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/skill-shared");
29
30pub static LICENSE: &str = include_str!("../LICENSE");
32pub static LICENSE_MIT: &str = include_str!("../LICENSE-MIT");
34pub static LICENSE_CC_BY: &str = include_str!("../LICENSE-CC-BY-4.0");
36
37const EMBEDDED_ROOTS: &[(&str, &Dir<'static>)] = &[
42 ("_docs/specs", &SPECS),
43 ("templates", &TEMPLATES),
44 (".markdownlint", &MARKDOWNLINT),
45 ("instance/snippets", &SNIPPETS),
46 ("method", &METHOD),
47 ("skills", &SKILLS),
48 ("skill-shared", &SKILL_SHARED),
49];
50
51#[must_use]
53pub fn skill_names() -> Vec<&'static str> {
54 let mut names: Vec<&'static str> = SKILLS
55 .dirs()
56 .filter_map(|dir| dir.path().as_os_str().to_str())
57 .collect();
58 names.sort_unstable();
59 names
60}
61
62#[must_use]
64pub fn skill(name: &str) -> Option<&'static str> {
65 SKILLS
66 .get_file(format!("{name}/SKILL.md"))
67 .and_then(include_dir::File::contents_utf8)
68}
69
70#[must_use]
77pub fn shared_artifacts() -> Vec<(String, &'static [u8])> {
78 fn walk(dir: &Dir<'static>, out: &mut Vec<(String, &'static [u8])>) {
79 for file in dir.files() {
80 if let Some(path) = file.path().to_str() {
81 out.push((path.to_string(), file.contents()));
82 }
83 }
84 for sub in dir.dirs() {
85 walk(sub, out);
86 }
87 }
88 let mut out = Vec::new();
89 walk(&SKILL_SHARED, &mut out);
90 out.sort_by(|a, b| a.0.cmp(&b.0));
91 out
92}
93
94#[must_use]
97pub fn asset(source: &str) -> Option<&'static [u8]> {
98 EMBEDDED_ROOTS.iter().find_map(|(root, dir)| {
99 let rest = source.strip_prefix(root)?.strip_prefix('/')?;
100 dir.get_file(rest).map(include_dir::File::contents)
101 })
102}
103
104#[must_use]
106pub fn spec_rule_ids() -> BTreeSet<String> {
107 let mut ids = BTreeSet::new();
108 for file in SPECS.files() {
109 let Some(text) = file.contents_utf8() else {
110 continue;
111 };
112 ids.extend(rule_ids_in(text));
113 }
114 ids
115}
116
117pub fn rule_ids_in(text: &str) -> impl Iterator<Item = String> + '_ {
119 text.lines().filter_map(|line| {
120 let candidate = line.strip_prefix("### `")?;
121 let (id, _) = candidate.split_once('`')?;
122 let (domain, rule) = id.split_once(':')?;
123 let is_slug = |part: &str| {
124 !part.is_empty()
125 && part
126 .bytes()
127 .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-')
128 };
129 (is_slug(domain) && is_slug(rule)).then(|| id.to_string())
130 })
131}
132
133#[cfg(test)]
134mod tests {
135 use super::*;
136 use crate::domain::profile::ProfileId;
137 use crate::domain::rule_id::RuleId;
138
139 #[test]
140 fn rule_id_enum_matches_the_embedded_specs() {
141 let from_specs = spec_rule_ids();
142 let from_enum: BTreeSet<String> = RuleId::ALL
143 .iter()
144 .map(|rule| rule.as_str().to_string())
145 .collect();
146 assert_eq!(
147 from_specs, from_enum,
148 "RuleId and the specs disagree; update the enum and the specs together"
149 );
150 }
151
152 #[test]
153 fn the_embedded_roots_are_the_declared_payload_roots() {
154 let embedded: Vec<&str> = EMBEDDED_ROOTS.iter().map(|(root, _)| *root).collect();
155 assert_eq!(
156 embedded,
157 PAYLOAD_ROOTS.to_vec(),
158 "payload_roots.rs and the embedded statics disagree; a root missing from the declaration ships unscanned"
159 );
160 }
161
162 #[test]
163 fn every_profile_projection_resolves_to_an_embedded_asset() {
164 for id in [ProfileId::Codebase, ProfileId::KnowledgeBase] {
165 let profile = id.profile();
166 for entry in profile.managed.iter().chain(profile.adopted) {
167 assert!(
168 asset(entry.source).is_some(),
169 "{id}: {} is not embedded",
170 entry.source
171 );
172 }
173 }
174 }
175
176 #[test]
177 fn the_method_and_licenses_are_carried() {
178 assert!(METHOD.get_file("glossary.md").is_some());
179 assert!(METHOD.files().count() >= 15);
180 assert!(LICENSE.contains("LICENSE-MIT") && LICENSE.contains("LICENSE-CC-BY-4.0"));
181 }
182
183 #[test]
184 fn rule_id_parser_matches_heading_shape_only() {
185 let text = "### `a-b:c-d` — Title\n### `Bad:Id`\nplain ### `x:y`\n### `no-colon`\n";
186 let ids: Vec<String> = rule_ids_in(text).collect();
187 assert_eq!(ids, vec!["a-b:c-d".to_string()]);
188 }
189}