Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

requirements.index.path_conflicts

Requirements index · View definition

evidence Valid

Requirement

The mdBook preprocessor MUST report an error when a generated requirements index or evidence detail page would use the path of an existing book chapter.

Rationale

The requirement has one obligation: the preprocessor MUST report an error when a generated requirements index or evidence detail page would use the path of an existing book chapter. I split it into two cases, the generated index and the generated detail pages.

Implementation: src/index.rs#impl[DetailPlan]/fn[find] carries the {impl} reference.

  • Generated index. When the book has no {requirements-index} marker, generated_index is true and the index path defaults to requirements-index.md. If any existing chapter already uses that path, find returns an invalid_index error: “generated requirements index … conflicts with a book chapter”. When a marker exists, the index path is the marker chapter’s own path. That is not a generated page, so no error is expected.
  • Detail pages. Each detail path comes from detail_path, which is the index path without its extension, joined with {name}.md. find returns an error (“generated evidence page … conflicts with a book chapter”) if a detail path is already used by a book chapter. It also errors if two detail paths are the same.
  • Reporting. preprocess_book in src/lib.rs calls index::DetailPlan::find(...)?, so the error is reported to the caller.

Tests:

  • mod[tests]/fn[default_index_path_cannot_replace_an_existing_chapter] runs preprocess_book on a book with an existing requirements-index.md chapter. It asserts the index-conflict error and that the chapter content is unchanged.
  • mod[tests]/fn[generated_detail_path_cannot_replace_a_chapter] runs DetailPlan::find with and without a marker chapter. It uses an existing chapter at requirements-index/requirements.ready.md and asserts the “conflicts with a book chapter” error in both cases.

I found no unspecified behavior and no gaps.

Inputs

Implementation

Input 2: outline src/index.rs impl[DetailPlan]/fn[find]
    // {impl}`requirements.index.chapter`
    // {impl}`requirements.index.chapter.default`
    // {impl}`requirements.examples.index`
    // {impl}`requirements.index.detail_pages`
    // {impl}`requirements.index.path_conflicts`
    pub(crate) fn find(
        book: &Book,
        definitions: &BTreeMap<String, Definition>,
        order: &[String],
    ) -> io::Result<Self> {
        let mut index_path = None;
        for chapter in book.chapters() {
            for _ in marker_ranges(&chapter.content)? {
                if index_path.is_some() {
                    return Err(invalid_index(
                        "a book may contain only one {requirements-index} directive",
                    ));
                }
                index_path = chapter.path.clone();
            }
        }
        let generated_index = index_path.is_none();
        let index_path = index_path.unwrap_or_else(|| PathBuf::from("requirements-index.md"));
        let names = order
            .iter()
            .filter(|name| definitions.contains_key(*name))
            .cloned()
            .collect::<Vec<_>>();
        let mut detail_paths = BTreeMap::new();
        for name in &names {
            detail_paths.insert(name.clone(), detail_path(&index_path, name));
        }
        let chapters = vec![(index_path.clone(), names)];
        let existing_paths = book
            .chapters()
            .filter_map(|chapter| chapter.path.as_ref())
            .collect::<BTreeSet<_>>();
        // {impl}`requirements.index.path_conflicts`
        if generated_index && existing_paths.contains(&index_path) {
            return Err(invalid_index(&format!(
                "generated requirements index {} conflicts with a book chapter",
                index_path.display()
            )));
        }
        let mut generated = BTreeSet::new();
        for path in detail_paths.values() {
            if existing_paths.contains(path) || !generated.insert(path) {
                return Err(invalid_index(&format!(
                    "generated evidence page {} conflicts with a book chapter",
                    path.display()
                )));
            }
        }
        Ok(Self {
            index_path,
            generated_index,
            detail_paths,
            chapters,
        })
    }

Tests

Input 3: outline src/index.rs mod[tests]/fn[generated_detail_path_cannot_replace_a_chapter]
  • File: src/index.rs
  • Selector: mod[tests]/fn[generated_detail_path_cannot_replace_a_chapter]
  • Kind: fn
  • src/index.rs:1673–1707
    // {test}`requirements.index.path_conflicts`
    #[test]
    fn generated_detail_path_cannot_replace_a_chapter() {
        for with_marker in [false, true] {
            let mut book = Book::new();
            if with_marker {
                book.push_item(Chapter::new(
                    "Index",
                    ":::{requirements-index}\n:::\n".to_owned(),
                    "requirements-index.md",
                    Vec::new(),
                ));
            }
            book.push_item(Chapter::new(
                "Existing",
                String::new(),
                "requirements-index/requirements.ready.md",
                Vec::new(),
            ));
            let definitions = BTreeMap::from([(
                "requirements.ready".to_owned(),
                Definition {
                    path: Path::new("requirements.md").to_path_buf(),
                    text: "Ready MUST work.".to_owned(),
                    kind: DefinitionKind::Requirement,
                },
            )]);
            let error =
                match DetailPlan::find(&book, &definitions, &["requirements.ready".to_owned()]) {
                    Err(error) => error,
                    Ok(_) => panic!("a generated detail page replaced a chapter"),
                };
            assert!(error.to_string().contains("conflicts with a book chapter"));
        }
    }
Input 4: outline src/index.rs mod[tests]/fn[default_index_path_cannot_replace_an_existing_chapter]
  • File: src/index.rs
  • Selector: mod[tests]/fn[default_index_path_cannot_replace_an_existing_chapter]
  • Kind: fn
  • src/index.rs:1709–1725
    // {test}`requirements.index.path_conflicts`
    #[test]
    fn default_index_path_cannot_replace_an_existing_chapter() {
        let mut book = Book::new();
        book.push_item(Chapter::new(
            "Existing",
            "Existing content.".into(),
            "requirements-index.md",
            vec![],
        ));
        let error = preprocess_book(&mut book, Path::new(".")).unwrap_err();
        assert!(error.to_string().contains(
            "generated requirements index requirements-index.md conflicts with a book chapter"
        ));
        assert_eq!(book.items.len(), 1);
        assert_eq!(book.chapters().next().unwrap().content, "Existing content.");
    }

Other sources

Input 1: requirements selection requirements.index.path_conflicts
:::{req} path_conflicts
The mdBook preprocessor MUST report an error when a generated requirements index or evidence detail page would use the path of an existing book chapter.
:::