Skip to content

Library custom rules and collectors

Abdulkader Safi edited this page Nov 23, 2026 · 2 revisions

Library custom rules and collectors

Every page gets two kinds of output, and you can add your own of each:

  • A rule returns findings: pass, fail, warn or info, with a severity and evidence. Findings move the score.
  • A collector returns measurements: numbers and tables with their own ok, warn, bad or missing status. Measurements never touch the score.

A rule

use std::sync::Arc;
use sitevet::{Options, Rule, RuleCx, RuleMeta, RuleResult, Severity, Stage};

/// Flags pages that still carry placeholder text.
struct NoLoremIpsum;

impl Rule for NoLoremIpsum {
    fn meta(&self) -> RuleMeta {
        let mut meta = RuleMeta::new("content/no-lorem-ipsum", "content", "No placeholder text", Severity::Warning, Stage::Dom);
        meta.fix = Some("Replace the lorem ipsum text with real copy.");
        meta
    }

    fn run(&self, cx: &RuleCx<'_>) -> Vec<RuleResult> {
        if cx.page.text.to_lowercase().contains("lorem ipsum") {
            vec![RuleResult::fail("Page contains lorem ipsum placeholder text")]
        } else {
            vec![RuleResult::pass("No placeholder text")]
        }
    }
}

let mut rules = sitevet::all_rules(); // keep the built-in rules
rules.push(Arc::new(NoLoremIpsum));
let mut options = Options::default();
options.rules = Some(rules);

options.rules replaces the built-in set, so start from all_rules() to add to it, or pass only your own to run nothing else. only and skip still filter by category. The site-wide checks always run, filtered by only and skip.

The full example is examples/custom-rule.rs.

Rule IDs

Use category/name. The category decides where the finding is grouped and scored. An existing category (content, security) adds to its score; a new one gets its own row.

Results

RuleResult::pass, fail, warn and info take a message. .with_evidence("...") attaches the offending markup or value (capped at 2,000 characters in the report). A rule can return several results, for example one per offending element. Info results never count in the score, so use them for "nothing to check here".

Stages

A rule runs as soon as its inputs exist. Pick the stage by what you need from RuleCx:

Stage Runs RuleCx has
Stage::Dom During the crawl, per page page (the extracted PageData, including page.tech: one TechHit with name, category and evidence per vendor found), dom (the parsed scraper::Html), html (the raw HTML), site
Stage::Enriched After the crawl, once the page's links are checked page, links (a status for every link on the page), site
Stage::Rendered After the browser or PageSpeed Insights stage page, rendered (axe results and performance metrics), links, site

dom and html exist only in Stage::Dom. site holds robots.txt, the sitemap and llms.txt, fetched once per audit. sitevet::scraper is re-exported, so you can use its selectors without adding the crate yourself.

A collector

use sitevet::{Collector, CollectorMeta, MetricCell, MetricInput, RuleCx, Stage, ValueStatus};

struct ImageCount;

impl Collector for ImageCount {
    fn meta(&self) -> CollectorMeta {
        CollectorMeta::new("images/count", "images", Stage::Dom)
    }

    fn collect(&self, cx: &RuleCx<'_>) -> Vec<MetricInput> {
        let n = cx.page.images.len();
        vec![
            MetricInput::value("images/count", "Images", Some(MetricCell::Number(n as f64)), None)
                .with_status(if n > 50 { ValueStatus::Warn } else { ValueStatus::Ok })
                .with_note("Every <img> on the page"),
        ]
    }
}

let mut collectors = sitevet::all_collectors();
collectors.push(std::sync::Arc::new(ImageCount));
options.collectors = Some(collectors);

MetricInput::table(id, label, &["Column", ...], rows) builds a table instead. Tables keep 25 rows (sitevet::ROW_CAP) and record the full count, so the report can say "showing 25 of 300".

A panic never stops the audit. A rule that panics becomes one info finding on that page saying "rule crashed" with the panic message, and a collector that panics becomes one empty measurement with the status missing and the note "Collector errored".

Clone this wiki locally