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#[allow(
23    clippy::literal_string_with_formatting_args,
24    reason = "the braces are the block template's placeholder, not a formatting argument"
25)]
26pub fn render_block(docs_root: &str) -> String {
27    let body = snippet().replace("{docs_root}", docs_root);
28    let body = body.trim_end_matches('\n');
29    format!("{AGENTS_BEGIN}\n{body}\n{AGENTS_END}\n")
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn the_block_carries_the_markers_and_the_root() {
38        let block = render_block("docs");
39        assert!(block.starts_with("<!-- BEGIN spec-driven-docs docs -->\n"));
40        assert!(block.ends_with("<!-- END spec-driven-docs docs -->\n"));
41        assert!(block.contains(
42            "Read the writing style before you author or edit prose: `sdd method writing-style`."
43        ));
44        assert!(!block.contains("{docs_root}"));
45        assert!(!block.contains("simple-english"));
46    }
47
48    #[test]
49    fn the_underscore_root_reaches_the_block() {
50        let block = render_block("_docs");
51        assert!(block.contains(
52            "Read the writing style before you author or edit prose: `sdd method writing-style`."
53        ));
54        assert!(!block.contains("simple-english"));
55    }
56}