8. Module contracts
The boundaries between the pieces of glance, fixed before implementation so that separate agents can build them in parallel. Behaviour comes from docs/spec/; this chapter only says where each piece lives and how the pieces talk.
8.1 Repository layout
Cargo.toml workspace (resolver 3, edition 2024, MSRV in rust-toolchain.toml)
crates/glance-a11y/ the core: no_std + alloc, no I/O, no wasm-bindgen
src/snapshot/ arena, string table, decoding of the collector's columns
src/host.rs the Host trait (lazy page facts), the only way out of the core
src/page.rs Page = Snapshot + Host + memo caches
src/style.rs typed computed style, generated from schema/styles.json
src/css/ colour parsing, compositing, luminance, lengths
src/aria/ WAI-ARIA 1.2 and ARIA-in-HTML tables as const data
src/facts/ role, accessible name, visibility, focus, language
src/engine/ rule registry, dispatch, check outcomes, aggregation, ordering
src/select/ target, ancestry and xpath generation over the arena
src/rules/<group>/<rule>.rs one rule (with its checks) per file
src/results.rs raw and aggregated results, JSON serialisation
crates/glance-wasm/ wasm-bindgen cdylib: decodes buffers, implements Host by imports
js/src/collector/ in-page walk producing the snapshot columns
js/src/host/ the JS side of the Host imports
js/src/api/ run, configure, getRules, reset, setup/teardown, frames, reporters
js/src/utils/ axe.utils and axe.commons members
js/src/messages/ glance's own English messages (from rules.json)
rules.json SSOT: rule and check metadata and messages
schema/snapshot.json SSOT: snapshot columns and node property list
schema/styles.json SSOT: computed-style properties and their parse types
scripts/gen-*.mjs generators for the Rust and TS views of the schema files
conformance/ differential harness, ACT runner, reference-fixture runner
Every source file stays at or under 250 lines. A CI guard enforces it.
8.2 Snapshot (collector → core)
Eager, cheap facts, collected in one walk of the flattened tree (docs/spec/concepts-tree.md):
- node kind (element, text, document, document-fragment for open shadow roots), parent, first child, next sibling (flattened order), the node's root (document or shadow root) and frame document;
- element tag and namespace, attribute list (name id, value id), both interned in one string table, names lower-cased for HTML elements;
- text node data (string id);
- DOM-computed properties listed in schema/snapshot.json (for example
tabIndex,disabled,type,checked,valuelength,isContentEditable, media state); - a context flag per node (inside include, outside exclude), from the browser's
querySelectorAll.
Transport: typed arrays for the columns and one UTF-8 buffer for the string table, copied into wasm memory once per run.
8.3 Host (core → page), lazy
pub trait Host {
fn styles(&mut self, node: NodeId, pseudo: Pseudo, group: StyleGroup) -> Result<GroupStyle, HostError>;
fn border_box(&mut self, node: NodeId) -> Result<Rect, HostError>;
fn client_rects(&mut self, node: NodeId, out: &mut Vec<Rect>) -> Result<(), HostError>;
fn text_rects(&mut self, text: NodeId, out: &mut Vec<Rect>) -> Result<(), HostError>;
fn elements_from_point(&mut self, x: f64, y: f64, out: &mut Vec<NodeId>) -> Result<(), HostError>;
fn scroll(&mut self, node: NodeId) -> Result<ScrollBox, HostError>;
fn viewport(&mut self) -> Result<Viewport, HostError>;
fn pseudo_owners(&mut self) -> Result<Option<Vec<bool>>, HostError>;
fn media(&mut self, node: NodeId) -> Result<MediaState, HostError>;
fn link_url(&mut self, node: NodeId) -> Result<String, HostError>;
// Extended by rule agents with one method per new fact; each is documented in the trait.
}
styles sends one group of computed values (schema/styles.json groups) as written by the
page. Reading getPropertyValue costs about the same per property whatever the property, so
the core asks for a group only when a rule or fact first reads one of its properties on that
element; the groups are chosen from measured demand, so most elements cost a group or two.
An element Chrome computes no style for (the content of <video>/<audio>) sends every value
of the group empty, which parses as that group's explicit uncomputed() values; any other
empty value is an error
(docs/spec/concepts-visibility.md, "Elements with no computed style").
Page memoises every answer, so a fact is fetched at most once per node per run. Facts
needed by most rules on most nodes are moved into the eager snapshot when profiling shows
the call overhead matters; the trait stays the boundary either way. Tests implement Host
over a recorded snapshot (conformance/snapshots/*.json, produced by the collector in eager
mode), so the core is tested without a browser.
8.4 Facts (src/facts)
Functions of &Page and a NodeId, memoised in Page:
explicit_role, implicit_role, role, accessible_name, accessible_text (with the
control and labelled-by context flags), is_hidden_visually, is_hidden_for_at,
is_visible_on_screen, is_focusable, is_natively_focusable, is_tabbable,
lang_of, is_valid_lang. Each implements the behaviour in the matching
docs/spec/concepts-*.md file, deviations included.
8.5 Rules and checks (src/engine, src/rules)
pub struct Rule {
pub id: &'static str, // metadata comes from rules.json by id
pub applies: fn(&Page, NodeId) -> bool, // selector + matches, as one predicate
pub any: &'static [Check], pub all: &'static [Check], pub none: &'static [Check],
pub page_level: bool,
pub after: Option<fn(&Page, &mut [NodeResult])>, // whole-run reductions
}
pub struct Check {
pub id: &'static str,
pub evaluate: fn(&Page, NodeId, &CheckOptions) -> Result<CheckOutcome, CheckError>,
}
pub struct CheckOutcome { pub result: Tri, pub data: Data, pub related: Vec<NodeId> }
The engine walks the flattened tree once, in document order, offering each in-context node
to each enabled rule's applies. A CheckError becomes the error-occurred result the
spec describes (docs/spec/results-shape.md). Aggregation into outcomes and impacts, check
listing and ordering follow docs/spec/results-semantics.md and live in the engine, not in
rules.
8.6 Results (core → API layer)
JSON, already aggregated and ordered: per rule its outcome groups; per node target,
ancestry, xpath, impact, the node index, and its checks (id, impact, data, related node
targets). The API layer adds what needs the real DOM or the messages: html (the element
source), description, help, helpUrl, check message, failureSummary, element refs,
and the top-level testEngine, testRunner, testEnvironment, url, timestamp,
toolOptions.
8.7 rules.json
One entry per rule: id, tags (in reference order), impact, enabled, pageLevel,
actIds, description, help (glance's own words), and its checks with id, impact,
and messages (pass, fail, incomplete, keyed by messageKey where the data has one).
Generated from it: the Rust registry constant, the TS types, getRules(), and the rule table
in COMPAT.md. Messages are written for glance, never copied.