Skip to main content

spec_driven_docs/domain/
profile.rs

1//! Installation profiles: what a target repository receives.
2//!
3//! A profile declares the documentation root and the payload projection —
4//! which embedded files land managed and which land adopted, and where.
5//! The declarations are code so a profile referencing an asset the payload
6//! does not carry fails a test instead of an install. Copying bytes and
7//! recording hashes is the installer's work.
8
9use std::fmt;
10
11use camino::Utf8PathBuf;
12use clap::ValueEnum;
13use serde::{Deserialize, Serialize};
14
15/// The two installable profiles.
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, ValueEnum, Serialize, Deserialize)]
17#[value(rename_all = "kebab-case")]
18#[serde(rename_all = "kebab-case")]
19pub enum ProfileId {
20    /// A codebase whose records live under `docs/`.
21    Codebase,
22    /// A knowledge base whose records live under `_docs/`.
23    KnowledgeBase,
24}
25
26impl ProfileId {
27    /// The profile's declaration.
28    #[must_use]
29    pub const fn profile(self) -> &'static Profile {
30        match self {
31            Self::Codebase => &CODEBASE,
32            Self::KnowledgeBase => &KNOWLEDGE_BASE,
33        }
34    }
35
36    /// The kebab-case name used on the command line and in the manifest.
37    #[must_use]
38    pub const fn as_str(self) -> &'static str {
39        match self {
40            Self::Codebase => "codebase",
41            Self::KnowledgeBase => "knowledge-base",
42        }
43    }
44}
45
46impl fmt::Display for ProfileId {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        f.write_str(self.as_str())
49    }
50}
51
52/// Where an instance keeps the documents the gates read.
53#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
54pub enum DocsRoot {
55    /// `docs/` — the codebase convention.
56    #[serde(rename = "docs")]
57    Docs,
58    /// `_docs/` — the knowledge-base convention.
59    #[serde(rename = "_docs")]
60    UnderscoreDocs,
61}
62
63impl DocsRoot {
64    /// The directory name.
65    #[must_use]
66    pub const fn as_str(self) -> &'static str {
67        match self {
68            Self::Docs => "docs",
69            Self::UnderscoreDocs => "_docs",
70        }
71    }
72}
73
74impl fmt::Display for DocsRoot {
75    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76        f.write_str(self.as_str())
77    }
78}
79
80/// One payload projection: an embedded source and its instance destination.
81///
82/// An adopted destination may carry a `{docs_root}` placeholder, resolved
83/// per profile by [`resolve_destination`].
84#[derive(Debug, Clone, Copy, PartialEq, Eq)]
85pub struct Projection {
86    /// The embedded payload path.
87    pub source: &'static str,
88    /// The destination, relative to the instance root.
89    pub destination: &'static str,
90}
91
92const fn proj(source: &'static str, destination: &'static str) -> Projection {
93    Projection {
94        source,
95        destination,
96    }
97}
98
99/// Substitute the profile's documentation root into a destination template.
100#[must_use]
101// sdd: permanent the braces are the destination template's placeholder, not a formatting argument
102#[allow(clippy::literal_string_with_formatting_args)]
103pub fn resolve_destination(destination: &str, docs_root: DocsRoot) -> Utf8PathBuf {
104    Utf8PathBuf::from(destination.replace("{docs_root}", docs_root.as_str()))
105}
106
107/// What one profile installs.
108#[derive(Debug)]
109pub struct Profile {
110    /// The profile this declaration belongs to.
111    pub id: ProfileId,
112    /// The documentation root the instance uses.
113    pub docs_root: DocsRoot,
114    /// Byte projections the canon keeps owning.
115    pub managed: &'static [Projection],
116    /// Seeds the instance owns from the moment they land.
117    pub adopted: &'static [Projection],
118}
119
120/// Byte projections every profile installs.
121///
122/// No skill appears here. A skill name is what an agent's picker keys on,
123/// so an instance copy and the user-scope copy of one skill are two entries
124/// under one name in every session opened inside that instance. User scope
125/// owns them alone (ADR-give-every-skill-one-owner).
126const MANAGED: &[Projection] = &[
127    proj(
128        ".markdownlint/adr.markdownlint-cli2.jsonc",
129        ".spec-driven-docs/markdownlint/adr.markdownlint-cli2.jsonc",
130    ),
131    proj(
132        ".markdownlint/spec.markdownlint-cli2.jsonc",
133        ".spec-driven-docs/markdownlint/spec.markdownlint-cli2.jsonc",
134    ),
135    proj(
136        ".markdownlint/relative-links.markdownlint-cli2.jsonc",
137        ".spec-driven-docs/markdownlint/relative-links.markdownlint-cli2.jsonc",
138    ),
139];
140
141/// The template copies this repository keeps in its own documentation tree.
142///
143/// A canon-side copy exists only for a class this repository authors, so
144/// this list is shorter than the template projections above: the record
145/// generator writes these and the self-layout check expects them. One
146/// declaration is what stops the two from disagreeing, which is how a
147/// template once reached the tree that neither of them named.
148pub const CANON_TEMPLATES: &[&str] = &[
149    "_docs/decisions/TEMPLATE-adr.md",
150    "_docs/reference/TEMPLATE-agents-digest.md",
151];
152
153const ADOPTED: &[Projection] = &[
154    // What the project declares about the files its gates judge. Seeded
155    // once and then the project's, which is what adopted means. It is not
156    // under `{docs_root}`: it configures the tool rather than the corpus.
157    proj(
158        "instance/seeds/config.yaml",
159        ".spec-driven-docs/config.yaml",
160    ),
161    proj(
162        "_docs/specs/SPEC-decision-records.md",
163        "{docs_root}/specs/SPEC-decision-records.md",
164    ),
165    proj(
166        "_docs/specs/SPEC-instance.md",
167        "{docs_root}/specs/SPEC-instance.md",
168    ),
169    proj(
170        "_docs/specs/SPEC-docs-format.md",
171        "{docs_root}/specs/SPEC-docs-format.md",
172    ),
173    proj(
174        "_docs/specs/SPEC-docs-foundations.md",
175        "{docs_root}/specs/SPEC-docs-foundations.md",
176    ),
177    proj(
178        "_docs/specs/SPEC-docs-specs.md",
179        "{docs_root}/specs/SPEC-docs-specs.md",
180    ),
181    proj(
182        "_docs/specs/SPEC-comparison-docs.md",
183        "{docs_root}/specs/SPEC-comparison-docs.md",
184    ),
185    proj(
186        "_docs/specs/SPEC-known-issues.md",
187        "{docs_root}/specs/SPEC-known-issues.md",
188    ),
189    proj(
190        "_docs/specs/SPEC-spec-to-code.md",
191        "{docs_root}/specs/SPEC-spec-to-code.md",
192    ),
193    proj(
194        "_docs/specs/SPEC-guides.md",
195        "{docs_root}/specs/SPEC-guides.md",
196    ),
197    proj(
198        "_docs/specs/SPEC-writing-style.md",
199        "{docs_root}/specs/SPEC-writing-style.md",
200    ),
201    proj(
202        "_docs/specs/SPEC-tracking.md",
203        "{docs_root}/specs/SPEC-tracking.md",
204    ),
205    proj(
206        "_docs/specs/SPEC-tracking/tracking.schema.json",
207        "{docs_root}/specs/SPEC-tracking/tracking.schema.json",
208    ),
209    proj(
210        "templates/TEMPLATE-tracking.yaml",
211        "{docs_root}/reference/tracking.yaml",
212    ),
213    proj(
214        "templates/TEMPLATE-spec.md",
215        "{docs_root}/specs/TEMPLATE-spec.md",
216    ),
217    proj(
218        "templates/TEMPLATE-adr.md",
219        "{docs_root}/decisions/TEMPLATE-adr.md",
220    ),
221    proj(
222        "templates/TEMPLATE-agents-digest.md",
223        "{docs_root}/reference/TEMPLATE-agents-digest.md",
224    ),
225    proj(
226        "templates/TEMPLATE-guide.md",
227        "{docs_root}/guides/TEMPLATE-guide.md",
228    ),
229    proj(
230        "templates/TEMPLATE-known-issue.md",
231        "{docs_root}/reference/TEMPLATE-known-issue.md",
232    ),
233];
234
235static CODEBASE: Profile = Profile {
236    id: ProfileId::Codebase,
237    docs_root: DocsRoot::Docs,
238    managed: MANAGED,
239    adopted: ADOPTED,
240};
241
242static KNOWLEDGE_BASE: Profile = Profile {
243    id: ProfileId::KnowledgeBase,
244    docs_root: DocsRoot::UnderscoreDocs,
245    managed: MANAGED,
246    adopted: ADOPTED,
247};
248
249#[cfg(test)]
250mod tests {
251    use super::*;
252
253    #[test]
254    fn profiles_bind_their_roots() {
255        assert_eq!(ProfileId::Codebase.profile().docs_root, DocsRoot::Docs);
256        assert_eq!(
257            ProfileId::KnowledgeBase.profile().docs_root,
258            DocsRoot::UnderscoreDocs
259        );
260    }
261
262    #[test]
263    fn destinations_resolve_per_root() {
264        assert_eq!(
265            resolve_destination("{docs_root}/specs/SPEC-distribution.md", DocsRoot::Docs),
266            Utf8PathBuf::from("docs/specs/SPEC-distribution.md")
267        );
268        assert_eq!(
269            resolve_destination(
270                ".spec-driven-docs/markdownlint/x.jsonc",
271                DocsRoot::UnderscoreDocs
272            ),
273            Utf8PathBuf::from(".spec-driven-docs/markdownlint/x.jsonc")
274        );
275    }
276
277    #[test]
278    fn serde_uses_the_kebab_names() {
279        assert_eq!(
280            serde_json::to_string(&ProfileId::KnowledgeBase).unwrap(),
281            "\"knowledge-base\""
282        );
283        assert_eq!(
284            serde_json::to_string(&DocsRoot::UnderscoreDocs).unwrap(),
285            "\"_docs\""
286        );
287    }
288
289    #[test]
290    fn destination_templates_only_use_the_placeholder_in_adopted_paths() {
291        for entry in MANAGED {
292            assert!(
293                !entry.destination.contains('{'),
294                "{} is templated",
295                entry.destination
296            );
297        }
298        for entry in ADOPTED {
299            // The declaration is the one adopted file outside the corpus: it
300            // configures the tool rather than being documentation, so no
301            // documentation root names it.
302            if entry.destination == ".spec-driven-docs/config.yaml" {
303                continue;
304            }
305            assert!(
306                entry.destination.starts_with("{docs_root}/"),
307                "{} is not rooted",
308                entry.destination
309            );
310        }
311    }
312}