Skip to main content

spec_driven_docs/services/
agents_render.rs

1//! Render the managed documentation block for a root `AGENTS.md`.
2//!
3//! The block is the canonical documentation-routing section, wrapped in the
4//! `AGENTS.md` markers with the profile's documentation root substituted. The
5//! installer places it; this only produces the bytes. The content lives in
6//! the embedded snippet, so the block and the snippet cannot drift.
7
8use crate::domain::marker::{AGENTS_BEGIN, AGENTS_END};
9
10/// The embedded documentation snippet, with `{docs_root}` unresolved.
11#[must_use]
12pub fn snippet() -> &'static str {
13    crate::embedded::SNIPPETS
14        .get_file("AGENTS-docs.md")
15        .and_then(include_dir::File::contents_utf8)
16        .unwrap_or_default()
17}
18
19/// The complete marked block for the given documentation root, newline-
20/// terminated.
21#[must_use]
22// sdd: permanent the braces are the block template's placeholder, not a formatting argument
23#[allow(clippy::literal_string_with_formatting_args)]
24pub fn render_block(docs_root: &str) -> String {
25    let body = snippet().replace("{docs_root}", docs_root);
26    let body = body.trim_end_matches('\n');
27    format!("{AGENTS_BEGIN}\n{body}\n{AGENTS_END}\n")
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn the_block_carries_the_markers_and_the_root() {
36        let block = render_block("docs");
37        assert!(block.starts_with("<!-- BEGIN spec-driven-docs docs -->\n"));
38        assert!(block.ends_with("<!-- END spec-driven-docs docs -->\n"));
39        assert!(block.contains(
40            "Read the writing style before you author or edit prose: `sdd method writing-style`."
41        ));
42        assert!(!block.contains("{docs_root}"));
43        assert!(!block.contains("simple-english"));
44    }
45
46    #[test]
47    fn the_underscore_root_reaches_the_block() {
48        let block = render_block("_docs");
49        assert!(block.contains(
50            "Read the writing style before you author or edit prose: `sdd method writing-style`."
51        ));
52        assert!(!block.contains("simple-english"));
53    }
54}