pub trait CollectionRule: Send + Sync {
// Required methods
fn id(&self) -> &'static str;
fn name(&self) -> &'static str;
fn description(&self) -> &'static str;
fn metadata(&self) -> RuleMetadata;
fn check_collection(
&self,
documents: &[Document],
) -> Result<Vec<Violation>, MdBookLintError>;
// Provided methods
fn create_violation(
&self,
message: String,
line: usize,
column: usize,
severity: Severity,
) -> Violation { ... }
fn create_violation_for_file(
&self,
path: &Path,
message: String,
line: usize,
column: usize,
severity: Severity,
) -> Violation { ... }
}Expand description
Trait for rules that analyze multiple documents together
Collection rules are useful for cross-document validation such as:
- Checking for duplicate identifiers across files
- Validating inter-document links
- Ensuring sequential numbering across a set of documents
- Detecting inconsistencies between related documents
Unlike regular Rule implementations which process documents one at a time,
CollectionRule implementations receive all documents at once, allowing them
to perform comparisons and validations across the entire collection.
§Implementation Example
use mdbook_lint_core::rule::{CollectionRule, RuleMetadata, RuleCategory};
use mdbook_lint_core::{Document, Violation, Result};
pub struct NoDuplicateTitles;
impl CollectionRule for NoDuplicateTitles {
fn id(&self) -> &'static str { "COLL001" }
fn name(&self) -> &'static str { "no-duplicate-titles" }
fn description(&self) -> &'static str { "No two documents should have the same title" }
fn metadata(&self) -> RuleMetadata {
RuleMetadata::stable(RuleCategory::Structure)
}
fn check_collection(&self, documents: &[Document]) -> Result<Vec<Violation>> {
// Implementation would collect titles and check for duplicates
Ok(Vec::new())
}
}Required Methods§
Sourcefn name(&self) -> &'static str
fn name(&self) -> &'static str
Human-readable name for the rule (e.g., “adr-sequential-numbering”)
Sourcefn description(&self) -> &'static str
fn description(&self) -> &'static str
Description of what the rule checks
Sourcefn metadata(&self) -> RuleMetadata
fn metadata(&self) -> RuleMetadata
Metadata about this rule’s status and properties
Sourcefn check_collection(
&self,
documents: &[Document],
) -> Result<Vec<Violation>, MdBookLintError>
fn check_collection( &self, documents: &[Document], ) -> Result<Vec<Violation>, MdBookLintError>
Check a collection of documents for violations
This method receives all documents that should be analyzed together. Implementations should filter the documents as needed (e.g., only ADR files) and return violations that reference specific documents by path.