requirements.references.book_badges
Requirements index · View definition
Requirement
In a book chapter, each valid
{impl},{cite}, or{test}reference, inline or fenced, MUST render as a Shields.io static badge image with meaningful alt text that displays its role and full name and links to the badge at the corresponding{req}or{example}definition. Links MUST work across chapters, including nested chapters. A fenced reference MUST render its badge followed by its body as Markdown. Each fenced{req}or{example}directive MUST render as a Shields.io definition badge image identifying its kind; text outside a directive MUST remain literal. Badge text MUST remain legible regardless of whether its link has been visited. A badge link MUST show a visible indicator when it has keyboard focus and MAY brighten on hover. Badges MAY be laid out inline with the surrounding text, a fenced reference’s badge and body MAY be visually set apart from the surrounding text as one block, and a built-in’s unlinked badge MAY show a help cursor to signal its hover text. These badges use the syntax defined by.
Rationale
The reviewed tests cover each obligation of requirements.references.book_badges that needs a test. The MAY clauses (hover brightening, inline layout, block layout, help cursor) impose nothing to check. The one doubt is that a fenced {impl} or {test} reference is never rendered in a test. I rate that a minor residual risk, not a gap. replace_references handles all roles through one code path, with no role-specific branch for fenced forms. Inline {impl} and {test} are exercised in the nested-chapter test.
Obligations mapped to tests
- Shields.io static badge with alt text of role and full name:
shields_badge_urls_escape_requirement_namesasserts theimg.shields.io/badge/...URL andalt="req requirements.foo_bar-baz".references_in_nested_chapters_link_to_the_requirement_definitionasserts theimg.shields.io/badge/{kind}-requirements.ready-{color}URL for each ofimpl,citeandtest.fenced_references_render_their_badge_and_body_and_built_ins_are_unlinkedassertsalt="cite rules.other".
- Link to the badge at the definition, across chapters and nested chapters:
references_in_nested_chapters_link_to_the_requirement_definitionputs the references inguides/deep.md. It assertshref="../requirements.html#habeas-req-requirements.ready"for all three roles.examples_are_distinct_definitions_and_tests_link_to_themasserts the{example}link../requirements.html#habeas-example-requirements.something.foo.- The fenced test asserts the same-chapter link
rules.html#habeas-req-rules.other.
- Fenced reference renders its badge, then its body as Markdown:
- The fenced test checks the
habeas-fenced-refwrapper. - It checks that the badge precedes the body text.
- It checks that the rendered HTML contains
<p>The <em>other</em> rule.</p>. - It checks that the raw fence line is gone.
- It checks that a nested fenced reference inside a
{req}keeps the outer requirement intact.
- The fenced test checks the
- Definition badge identifying kind: The tests assert
alt="req rules.ready"andalt="example requirements.something.foo". - Text outside a directive stays literal:
colon_directives_render_markdown_bodies_and_legacy_roles_stay_literalcovers an inline{req}role and the rendering of a body with a heading and a code block.examples_are_distinct_definitions_and_tests_link_to_themcovers{example} bad nameand an{example}inside a code fence.- The fenced test covers an invalid built-in
habeas:none, an unknown namerules.missing, and a fence inside a code block. - These all stay literal.
- Visited legibility:
badge_stylesheet_keeps_visited_links_legible_and_shows_focusasserts thata.habeas-req-badge:visitedhas the same declarations asa.habeas-req-badge, withcolor: inherit. It makes the same check for the detail icon. - Visible keyboard focus: The same test asserts that
a.habeas-req-badge:focus-visiblehas anoutline:declaration that is neithernonenor zero-width. - Unlinked built-in badge: The fenced test asserts a
habeas-builtinspan with title text and nohref="habeas, for both inline and fenced use.
Inputs
Implementation
Input 9: outline src/lib.rs fn[replace_references]
- File:
src/lib.rs - Selector:
fn[replace_references] - Kind:
fn src/lib.rs:406–445
/// Renders fenced references as their badge followed by their body, and
/// inline references as badges. A fenced reference inside a definition needs
/// a longer outer fence, so its closing fence does not end the definition.
// {impl}`requirements.references.book_badges`
// {impl}`requirements.references.builtin_definitions`
fn replace_references(
content: &str,
current_path: &Path,
definitions: &BTreeMap<String, Definition>,
) -> String {
let mut result = String::with_capacity(content.len());
let mut cursor = 0;
for fence in fenced::markdown_fences(content, true) {
if fence.start < cursor || !fence.closed {
continue;
}
let body = &content[fence.body.clone()];
let Some(badge) = reference_badge(fence.kind, &fence.name, current_path, definitions)
else {
continue;
};
result.push_str(&replace_inline_references(
&content[cursor..fence.start],
current_path,
definitions,
));
result.push_str("<div class=\"habeas-fenced-ref\">\n\n<p>");
result.push_str(&badge);
result.push_str("</p>\n\n");
result.push_str(&replace_references(body, current_path, definitions));
result.push_str("\n\n</div>\n\n");
cursor = fence.end;
}
result.push_str(&replace_inline_references(
&content[cursor..],
current_path,
definitions,
));
result
}
Tests
Input 3: outline src/lib.rs mod[tests]/fn[fenced_references_render_their_badge_and_body_and_built_ins_are_unlinked]
- File:
src/lib.rs - Selector:
mod[tests]/fn[fenced_references_render_their_badge_and_body_and_built_ins_are_unlinked] - Kind:
fn src/lib.rs:915–1004
// {test}`requirements.references.book_badges`
// {test}`requirements.references.builtin_definitions`
// {test}`requirements.test-coverage.check.exceptions.anchor`
#[test]
fn fenced_references_render_their_badge_and_body_and_built_ins_are_unlinked() {
let mut book = Book::new();
let content = [
"::::{req} ready\nReady MUST hold.\n\n:::{cite} rules.other\nThe *other* rule.\n:::\n::::\n\n",
":::{req} other\nOther.\n:::\n\n",
":::{cite} habeas:util\nServes its caller.\n:::\n\n",
":::{cite} habeas:coverage\nA substantive coverage explanation.\n:::\n\n",
":::{cite} habeas:none\nRemoved.\n:::\n\n",
"Inline {cite}`habeas:none` stays literal.\n\n",
"Inline {cite}`habeas:util` is a badge.\n\n",
":::{cite} rules.missing\nUnknown.\n:::\n\n",
"```text\n:::{cite} rules.other\n:::\n```\n",
]
.concat();
book.push_item(Chapter::new("Rules", content, "rules.md", Vec::new()));
let root = book_root();
preprocess_book(&mut book, root.path()).unwrap();
let rendered = book.chapters().next().unwrap().content.clone();
// The project reference links to its definition, then its body.
let mut html = String::new();
pulldown_cmark::html::push_html(&mut html, pulldown_cmark::Parser::new(&rendered));
assert!(html.contains("<p>The <em>other</em> rule.</p>"), "{html}");
assert!(html.contains("alt=\"req rules.ready\""), "{html}");
let linked = "<div class=\"habeas-fenced-ref\">\n\n<p><a class=\"habeas-req-badge habeas-req-ref-cite\" href=\"rules.html#habeas-req-rules.other\">";
let start = rendered.find(linked).expect(&rendered);
let after = &rendered[start..];
assert!(
after.find("alt=\"cite rules.other\"").unwrap()
< after.find("The *other* rule.").unwrap(),
"{rendered}"
);
assert!(
!rendered.contains(":::{cite} rules.other\nThe"),
"{rendered}"
);
// The outer requirement still contains it and closes at its own fence.
assert!(!rendered.contains("See evidence"), "{rendered}");
// A built-in has no link; its hover text is its full text.
let util = crate::builtins::builtin("habeas:util").unwrap();
let coverage = crate::builtins::builtin("habeas:coverage").unwrap();
assert!(rendered.contains(&format!(
"<span class=\"habeas-req-badge habeas-req-ref-cite habeas-builtin\" title=\"{}\"><img",
super::escape_html(coverage.text())
)), "{rendered}");
assert!(
rendered.contains(&format!(
"<span class=\"habeas-req-badge habeas-req-ref-cite habeas-builtin\" title=\"{}\"><img",
super::escape_html(util.text())
)),
"{rendered}"
);
let inline = rendered
.split("Inline ")
.find(|part| part.contains(" is a badge."))
.unwrap();
let inline = inline.split(" is a badge.").next().unwrap();
assert!(inline.starts_with(&format!("<span class=\"habeas-req-badge habeas-req-ref-cite habeas-builtin\" title=\"{}\"><img", super::escape_html(util.text()))), "{inline}");
assert!(
inline.contains("habeas:util") && inline.ends_with("</span>"),
"{inline}"
);
assert!(!inline.contains("<a "), "{inline}");
let badge = "<p><span class=\"habeas-req-badge habeas-req-ref-cite habeas-builtin\"";
let fenced = rendered.find(badge).expect(&rendered);
assert!(
rendered[fenced..].find("Serves its caller.").is_some(),
"{rendered}"
);
assert!(!rendered.contains("href=\"habeas"), "{rendered}");
// Invalid forms, unknown names, and code examples remain literal.
assert!(
rendered.contains(":::{cite} habeas:none\nRemoved.\n:::"),
"{rendered}"
);
assert!(rendered.contains("Inline {cite}`habeas:none` stays literal."));
assert!(
rendered.contains(":::{cite} rules.missing\nUnknown.\n:::"),
"{rendered}"
);
assert!(
rendered.contains("```text\n:::{cite} rules.other\n:::\n```"),
"{rendered}"
);
}
Input 4: outline src/lib.rs mod[tests]/fn[badge_stylesheet_keeps_visited_links_legible_and_shows_focus]
- File:
src/lib.rs - Selector:
mod[tests]/fn[badge_stylesheet_keeps_visited_links_legible_and_shows_focus] - Kind:
fn src/lib.rs:1677–1739
// {test}`requirements.references.book_badges`
#[test]
fn badge_stylesheet_keeps_visited_links_legible_and_shows_focus() {
let rules = stylesheet_rules();
let declarations = |selector: &str| {
rules
.iter()
.filter(|(selectors, _)| selectors.iter().any(|candidate| candidate == selector))
.flat_map(|(_, declarations)| declarations.iter().map(String::as_str))
.collect::<Vec<_>>()
};
// A visited badge link looks exactly like an unvisited one.
assert_eq!(
declarations("a.habeas-req-badge:visited"),
declarations("a.habeas-req-badge")
);
assert!(declarations("a.habeas-req-badge").contains(&"color: inherit"));
assert_eq!(
declarations("a.habeas-detail-icon:link"),
declarations("a.habeas-detail-icon:visited")
);
assert!(declarations("a.habeas-detail-icon:link").contains(&"color: white"));
// Keyboard focus shows an outline.
let focus = declarations("a.habeas-req-badge:focus-visible");
assert!(
focus
.iter()
.any(|declaration| declaration.starts_with("outline:")
&& !declaration.contains("none")
&& !declaration.contains(" 0 ")),
"{focus:?}"
);
// Hover only brightens.
assert_eq!(
declarations("a.habeas-req-badge:hover"),
["filter: brightness(1.08)"]
);
// Every rule styles a Habeas badge or fenced reference; nothing else in
// the book is restyled.
let specified = [
".habeas-req-badge",
".habeas-shield",
".habeas-definition-badge",
".habeas-definition-badge > a",
"a.habeas-detail-icon",
"a.habeas-detail-icon:link",
"a.habeas-detail-icon:visited",
"a.habeas-detail-icon:hover",
"a.habeas-detail-icon:focus-visible",
".habeas-req-badge img.habeas-shield",
"a.habeas-req-badge",
"a.habeas-req-badge:visited",
"a.habeas-req-badge:hover",
"a.habeas-req-badge:focus-visible",
".habeas-builtin",
".habeas-fenced-ref",
];
for (selectors, _) in &rules {
for selector in selectors {
assert!(specified.contains(&selector.as_str()), "{selector}");
}
}
}
Input 5: outline src/lib.rs mod[tests]/fn[shields_badge_urls_escape_requirement_names]
- File:
src/lib.rs - Selector:
mod[tests]/fn[shields_badge_urls_escape_requirement_names] - Kind:
fn src/lib.rs:1741–1753
// {test}`requirements.references.book_badges`
// {test}`requirements.references.safe_badges`
#[test]
fn shields_badge_urls_escape_requirement_names() {
let badge = shield_badge("req", "requirements.foo_bar-baz", "006ba8");
assert!(badge.contains(
"https://img.shields.io/badge/req-requirements.foo__bar--baz-006ba8?style=flat-square&labelColor=555"
));
assert!(badge.contains("alt=\"req requirements.foo_bar-baz\""));
let status = shield_badge("evidence", "Determination false", "ae5d14");
assert!(status.contains("badge/evidence-Determination_false-ae5d14"));
}
Input 6: outline src/lib.rs mod[tests]/fn[references_in_nested_chapters_link_to_the_requirement_definition]
- File:
src/lib.rs - Selector:
mod[tests]/fn[references_in_nested_chapters_link_to_the_requirement_definition] - Kind:
fn src/lib.rs:1755–1798
// {test}`requirements.references.book_badges`
// {test}`requirements.index.navigation`
#[test]
fn references_in_nested_chapters_link_to_the_requirement_definition() {
let mut book = Book::new();
book.push_item(Chapter::new(
"Requirements",
":::{req} ready\nReady.\n:::".to_owned(),
"requirements.md",
Vec::new(),
));
let references = ["impl", "cite", "test"]
.iter()
.map(|kind| format!("{{{kind}}}`requirements.ready`"))
.collect::<Vec<_>>()
.join(" ");
book.push_item(Chapter::new(
"Guide",
references,
"guides/deep.md",
Vec::new(),
));
let root = book_root();
preprocess_book(&mut book, root.path()).unwrap();
// Generated detail pages are titled by full name.
let chapters = book
.chapters()
.filter(|chapter| !chapter.name.contains('.'))
.collect::<Vec<_>>();
assert!(
chapters[0]
.content
.contains("id=\"habeas-req-requirements.ready\"")
);
for (kind, color) in [("impl", "237346"), ("cite", "7151a1"), ("test", "ae5d14")] {
assert!(chapters[1].content.contains(&format!(
"class=\"habeas-req-badge habeas-req-ref-{kind}\" href=\"../requirements.html#habeas-req-requirements.ready\""
)));
assert!(chapters[1].content.contains(&format!(
"img.shields.io/badge/{kind}-requirements.ready-{color}"
)));
}
}
Input 7: outline src/lib.rs mod[tests]/fn[colon_directives_render_markdown_bodies_and_legacy_roles_stay_literal]
- File:
src/lib.rs - Selector:
mod[tests]/fn[colon_directives_render_markdown_bodies_and_legacy_roles_stay_literal] - Kind:
fn src/lib.rs:1845–1858
// {test}`requirements.references.book_badges`
// {test}`requirements.references.markdown_literals`
#[test]
fn colon_directives_render_markdown_bodies_and_legacy_roles_stay_literal() {
let source = "An inline {req}`inline` role.\n\n:::{req} defined\nDefinition.\n\n## Details\n\n```text\nexample\n```\n:::\n\nAfter.";
let rendered = replace_definitions(source, "requirements", None, None);
assert!(rendered.contains("An inline {req}`inline` role."));
assert!(!rendered.contains("habeas-req-requirements.inline"));
assert!(rendered.contains("id=\"habeas-req-requirements.defined\""));
assert!(rendered.contains("## Details"));
assert!(rendered.contains("```text\nexample\n```"));
assert!(rendered.contains("After."));
assert!(!rendered.contains(":::{req} defined"));
}
Input 8: outline src/lib.rs mod[tests]/fn[examples_are_distinct_definitions_and_tests_link_to_them]
- File:
src/lib.rs - Selector:
mod[tests]/fn[examples_are_distinct_definitions_and_tests_link_to_them] - Kind:
fn src/lib.rs:1941–1995
// {test}`requirements.examples.definition`
// {test}`requirements.references.book_badges`
// {test}`requirements.ci.identifiers`
// {test}`requirements.examples.index`
#[test]
fn examples_are_distinct_definitions_and_tests_link_to_them() {
let source = "::::{req} ready\nReady.\n\n:::{example} something.foo\nGiven ready, it succeeds. {cite}`requirements.ready`\n:::\n::::\n\n:::{example} bad name\nInvalid.\n:::\n\n```text\n:::{example} hidden\nLiteral.\n:::\n```".to_owned();
let blocks = definition_blocks(&source);
assert_eq!(blocks.len(), 2);
assert_eq!(blocks[0].kind, DefinitionKind::Requirement);
assert_eq!(blocks[1].kind, DefinitionKind::Example);
let mut book = Book::new();
book.push_item(Chapter::new(
"Requirements",
source,
"requirements.md",
Vec::new(),
));
book.push_item(Chapter::new(
"Guide",
"{test}`requirements.something.foo`".to_owned(),
"guides/deep.md",
Vec::new(),
));
let root = book_root();
preprocess_book(&mut book, root.path()).unwrap();
// Generated detail pages are titled by full name.
let chapters = book
.chapters()
.filter(|chapter| !chapter.name.contains('.'))
.collect::<Vec<_>>();
assert!(
chapters[0]
.content
.contains("id=\"habeas-req-requirements.ready\"")
);
assert!(
chapters[0]
.content
.contains("id=\"habeas-example-requirements.something.foo\"")
);
assert!(
chapters[0]
.content
.contains("alt=\"example requirements.something.foo\"")
);
assert!(chapters[0].content.contains(":::{example} bad name"));
assert!(chapters[0].content.contains(":::{example} hidden"));
assert!(
chapters[1].content.contains(
"href=\"../requirements.html#habeas-example-requirements.something.foo\""
)
);
}
Referenced definitions
Input 2: requirements selection requirements.references.roles
requirements.references.roles:md/requirements/references.md:3–49
:::{req} roles
Project text files MAY use the `{impl}`, `{cite}`, and `{test}` roles with a backtick-delimited full requirement or example name, or with a built-in name under {cite}`requirements.references.builtin_definitions`, and MAY use the `{fixme}` role in fenced form. A reference MAY instead be written in fenced form, `:::{role} name` on one line, a body, and a closing fence, following the fence rules of {cite}`requirements.ci.span` and {cite}`requirements.ci.fence_lengths`; the fenced form is the same edge with attached commentary, and the inline form carries none. In a source comment, fence lines MUST be recognized after removing each line's comment delimiters within one comment run under {cite}`requirements.references.outline_comments`, and a fence that does not close within its comment run MUST be a validation error. Each reference is a directed edge between its source and the named definition. A source is a project source item or a requirement or example declaration. The roles MUST mean:
- `{cite}`: the source relies on, supports, or refers to the definition. It is an
edge from the source to the definition.
- `{impl}`: the source implements part of the definition: it makes a decision,
performs a check, produces an output, or handles a case that the definition
specifies. A requirement MAY implement part of another requirement by refining
one of its obligations. Calling code that implements a definition, or
producing a result a caller uses to meet it, MUST NOT by itself count as
implementing it. A check MUST count only if the definition states it, or the
source could not meet one of the definition's obligations without it; source
that checks it is consistent with a definition, such as recognizing a form the
definition specifies in order to decide whether to run other behavior, relies
on that definition and calls for `{cite}`. A requirement stating a convention
for every output of a kind, such as every validation error, is implemented by
the source that carries out the convention as a whole, such as the step that
collects and reports those outputs.
- `{test}`: the source is designed to verify behavior the definition specifies:
a test through its scenario and the assertions about that scenario's outcome,
or a named example through the concrete case it describes.
- `{fixme}`: the source acknowledges a known gap in fulfilling the definition. It
MUST use the fenced form, and its body MUST describe the gap. The body MAY
begin with option lines naming issues that track the gap, one per line, in the
form `:github: <owner>/<repo>#<number>`, such as
`:github: rust-lang/rust#123`. `github` MUST be the only recognized option.
A named `{fixme}` MAY appear only in sources outside the book. Validation MUST report
any other option line, a `:github:` value not in that form, an inline
`{fixme}`, a `{fixme}` in a requirement or example declaration, and a
`{fixme}` whose body, without option lines and comment delimiters, is empty.
An unnamed `{fixme}`, a fence of `:::{fixme}` with no name, MAY appear in any
project text, including a requirement or example body, to tie that place to
the issues it names. It MUST have at least one `:github:` option line and MAY
have a description; it is not a reference edge, acknowledges no gap, needs no
coverage, and does not count as a reference for Rust item coverage. Its
issues are checked under {cite}`requirements.cli.fixme_issues`.
`{impl}`, `{test}`, and `{fixme}` are edges from the definition to the source.
Only requirements and examples are assessed; code is reviewed as part of the
definitions it implements or tests.
```text
{impl}`requirements.references.roles`
{cite}`requirements.references.roles`
{test}`requirements.references.roles`
```
:::
Other sources
Input 1: requirements selection requirements.references.book_badges
requirements.references.book_badges:md/requirements/references.md:205–208
:::{req} book_badges
In a book chapter, each valid `{impl}`, `{cite}`, or `{test}` reference, inline or fenced, MUST render as a Shields.io static badge image with meaningful alt text that displays its role and full name and links to the badge at the corresponding `{req}` or `{example}` definition. Links MUST work across chapters, including nested chapters. A fenced reference MUST render its badge followed by its body as Markdown. Each fenced `{req}` or `{example}` directive MUST render as a Shields.io definition badge image identifying its kind; text outside a directive MUST remain literal. Badge text MUST remain legible regardless of whether its link has been visited. A badge link MUST show a visible indicator when it has keyboard focus and MAY brighten on hover. Badges MAY be laid out inline with the surrounding text, a fenced reference's badge and body MAY be visually set apart from the surrounding text as one block, and a built-in's unlinked badge MAY show a help cursor to signal its hover text. These badges use the syntax defined by {cite}`requirements.references.roles`.
:::