tabnas_support/lib.rs
1// Copyright (c) 2026 tabnas, MIT License
2
3//! Shared test-support utilities for the tabnas parser system.
4//!
5//! Every tabnas package ships a canonical TypeScript implementation and
6//! ports of it, and proves they agree by running **one** set of TSV
7//! fixtures from `test/spec/` in every runtime. This crate is the
8//! machinery that reads those fixtures in Rust, so there is one loader
9//! with one set of rules instead of a copy per repository quietly
10//! drifting from its TypeScript and Go twins.
11//!
12//! The TypeScript half is `@tabnas/support` and the Go half is
13//! `github.com/tabnas/support/go`. The three are written to behave
14//! identically: same escape codec, same comment and blank-line handling,
15//! same `ERROR:<code>` contract, same value comparison. Where a
16//! difference is unavoidable it is documented at the point it appears,
17//! and collected in `doc/reference.md`.
18//!
19//! A typical suite is a few lines:
20//!
21//! ```no_run
22//! use tabnas_support::{find_spec_dir, Failure, Runner, Value};
23//!
24//! let dir = find_spec_dir(None).expect("a test/spec directory above the crate");
25//! Runner::new(|input| {
26//! // Parse with your grammar, then hand back a `Value` or a `Failure`
27//! // carrying the error code the fixture pins.
28//! let _ = input;
29//! Err(Failure::new("unexpected"))
30//! })
31//! .dir(dir.join("happy"));
32//! ```
33//!
34//! This crate has **no required dependencies**, and never will: every
35//! tabnas Rust crate would take it as a dev-dependency, so anything it
36//! required would land in all of them. The adder grammar that exercises
37//! it end to end therefore lives in the separate `adder/` crate, which
38//! may depend on the engine.
39
40use std::fmt;
41
42mod census;
43mod escape;
44mod expect;
45mod register;
46mod runner;
47mod spec;
48mod value;
49
50/// The release version, kept in step with `ts/package.json`. See
51/// `tests/version_test.rs`, which fails the build when they drift.
52pub const VERSION: &str = "0.3.9";
53
54pub use census::{
55 codes_in_spec_dir, compare_catalogues, coverage, CatalogueDiff, CensusOptions, CoverageReport,
56};
57pub use escape::{escape, unescape};
58pub use expect::{
59 equal_value, equal_value_with, error_code, error_expect, format_value, is_error_expect,
60 lone_surrogate_at, lone_surrogate_message, parse_expect, ErrorExpect, ERROR_PREFIX,
61};
62pub use register::{no_divergences, Register};
63pub use runner::{report, Failure, Runner};
64pub use spec::{
65 find_spec_dir, load_spec, load_spec_dir, parse_spec, Column, Row, SpecFile, SpecOptions,
66};
67pub use value::Value;
68
69/// A defect in a fixture, a suite or a runner: an unreadable file, a
70/// malformed expectation, a runner built without a parser. Carries the
71/// same sentence the TypeScript and Go halves raise for the same
72/// condition, so a failure reads the same whichever runtime reported it.
73#[derive(Debug, Clone, PartialEq, Eq)]
74pub struct Error(pub String);
75
76impl fmt::Display for Error {
77 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
78 formatter.write_str(&self.0)
79 }
80}
81
82impl std::error::Error for Error {}
83
84impl From<String> for Error {
85 fn from(message: String) -> Self {
86 Error(message)
87 }
88}
89
90impl From<&str> for Error {
91 fn from(message: &str) -> Self {
92 Error(message.to_string())
93 }
94}
95
96/// The result type every fallible function here returns.
97pub type Result<T> = std::result::Result<T, Error>;