use std::sync::OnceLock;
use crate::diagnostics::BehaviorVersion;
const FIELD_SEPARATOR: char = '\u{1f}';
const RECORD_SEPARATOR: char = '\u{1e}';
const AGE_CANDIDATES: &[&str] = &[
"23.0", "22.0", "21.0", "20.0", "19.0", "18.0", "17.0", "16.0", "15.1", "15.0", "14.0", "13.0",
"12.1", "12.0", "11.0", "10.0",
];
struct Reading {
unit: String,
judged: String,
source: String,
observed: Option<String>,
}
fn judged_readings() -> Vec<Reading> {
let recorded = env!("TOKTIER_RUST_API_JUDGED_BEHAVIOR");
if recorded.is_empty() {
return Vec::new();
}
recorded
.split(FIELD_SEPARATOR)
.filter_map(|record| {
let mut fields = record.split(RECORD_SEPARATOR);
let unit = fields.next()?;
let judged = fields.next()?;
let source = fields.next()?;
if unit.is_empty() || judged.is_empty() {
return None;
}
Some(Reading {
observed: observe(unit),
unit: unit.to_owned(),
judged: judged.to_owned(),
source: source.to_owned(),
})
})
.collect()
}
fn drift_for(unit: &str) -> Option<&'static str> {
let recorded = env!("TOKTIER_RUST_API_R2_DRIFT");
recorded.split(FIELD_SEPARATOR).find_map(|record| {
let (name, report) = record.split_once(RECORD_SEPARATOR)?;
(name == unit).then_some(report)
})
}
fn observe(unit: &str) -> Option<String> {
match unit {
"regex" => AGE_CANDIDATES
.iter()
.find(|age| regex::Regex::new(&format!(r"\p{{Age={age}}}")).is_ok())
.map(|age| (*age).to_owned()),
"onig" => Some(onig::version()).filter(|version| !version.is_empty()),
"unicode-segmentation" => {
let (major, minor, patch) = unicode_segmentation::UNICODE_VERSION;
Some(format!("{major}.{minor}.{patch}"))
}
_ => None,
}
}
fn carries(unit: &str, observed: &str) -> String {
match unit {
"regex" => format!("carries Unicode {observed} (regex tables)"),
"onig" => format!("links Oniguruma {observed}"),
"unicode-segmentation" => format!("reads segmentation tables {observed}"),
other => format!("reads {other} {observed}"),
}
}
fn unchanged(unit: &str, version: &str) -> String {
match unit {
"regex" => format!("Unicode data version unchanged ({version})"),
"onig" => format!("Oniguruma library version unchanged ({version})"),
"unicode-segmentation" => format!("segmentation table version unchanged ({version})"),
other => format!("{other} behaviour version unchanged ({version})"),
}
}
const COMPARE_HINT: &str = "To compare the accelerated engine with this binary's reference \
engine on your own text, run `toktier-rust verify-local --engine \
cpu --family <family> --input <path>` where that CLI is \
installed (`cargo install --locked toktier` builds it; a crate \
dependency alone brings no binary), or `toktier verify-local` \
with the same options from the Python package";
const AFFECTED_HINT: &str = "This build admits no accelerated cpu route while the core is \
mismatched, so `verify-local --engine cpu` has nothing to \
compare here; put the library back on the judged behaviour \
version instead: the `dependency_closure` line of this same \
report carries the `cargo update --precise` command for each \
package it names, to run in the workspace that owns the \
governing lockfile";
fn readings() -> &'static Vec<Reading> {
static READINGS: OnceLock<Vec<Reading>> = OnceLock::new();
READINGS.get_or_init(judged_readings)
}
pub(crate) fn core_closure() -> &'static str {
static CORE: OnceLock<String> = OnceLock::new();
CORE.get_or_init(|| {
decide(
env!("TOKTIER_RUST_API_CORE_CLOSURE_STATIC"),
readings(),
&drift_for,
)
})
}
fn decide(
recorded: &str,
readings: &[Reading],
drift: &dyn Fn(&str) -> Option<&'static str>,
) -> String {
if recorded != "verified" {
return recorded.to_owned();
}
for reading in readings {
match &reading.observed {
Some(observed) if observed != &reading.judged => {
return format!(
"mismatched: the reference engine in this binary {} where the certified \
evidence was taken on {}; some code points may tokenize differently; the \
accelerated engines are held on the reference route. {AFFECTED_HINT}",
carries(&reading.unit, observed),
reading.judged
);
}
Some(_) => {}
None => {
if let Some(moved) = drift(&reading.unit) {
return format!(
"mismatched: the behaviour version of {} could not be read in this \
binary, so the package versions are compared instead: {moved}. \
{AFFECTED_HINT}",
reading.unit
);
}
}
}
}
"verified".to_owned()
}
pub(crate) fn core_closure_verified() -> bool {
core_closure() == "verified"
}
pub(crate) fn dependency_advisory() -> Option<&'static str> {
static ADVISORY: OnceLock<Option<String>> = OnceLock::new();
ADVISORY
.get_or_init(|| {
let mut parts = Vec::new();
let outside = env!("TOKTIER_RUST_API_DEPENDENCY_ADVISORY");
if !outside.is_empty() {
parts.push(outside.to_owned());
}
for reading in readings() {
let Some(observed) = &reading.observed else {
continue;
};
if observed != &reading.judged {
continue;
}
if let Some(drift) = drift_for(&reading.unit) {
parts.push(format!(
"{drift}: {}; the certificate holds. {COMPARE_HINT}",
unchanged(&reading.unit, observed)
));
}
}
(!parts.is_empty()).then(|| parts.join(". "))
})
.as_deref()
}
pub(crate) fn facts() -> Vec<BehaviorVersion> {
readings()
.iter()
.map(|reading| BehaviorVersion {
unit: reading.unit.clone(),
observed: reading.observed.clone(),
judged: reading.judged.clone(),
source: reading.source.clone(),
})
.collect()
}
#[cfg(test)]
mod tests {
use super::{
carries, decide, drift_for, observe, readings, unchanged, Reading, AGE_CANDIDATES,
FIELD_SEPARATOR, RECORD_SEPARATOR,
};
fn reading(unit: &str, judged: &str, observed: Option<&str>) -> Reading {
Reading {
unit: unit.to_owned(),
judged: judged.to_owned(),
source: "probe:regex-age".to_owned(),
observed: observed.map(str::to_owned),
}
}
fn no_drift(_: &str) -> Option<&'static str> {
None
}
#[test]
fn the_core_answer_has_four_states() {
assert_eq!(
decide(
"mismatched: the judged build compiled memchr 2.8.3",
&[reading("regex", "16.0", Some("17.0"))],
&no_drift,
),
"mismatched: the judged build compiled memchr 2.8.3"
);
let moved = decide(
"verified",
&[reading("regex", "16.0", Some("17.0"))],
&no_drift,
);
assert_eq!(
moved,
"mismatched: the reference engine in this binary carries Unicode 17.0 (regex \
tables) where the certified evidence was taken on 16.0; some code points may \
tokenize differently; the accelerated engines are held on the reference route. \
This build admits no accelerated cpu route while the core is mismatched, so \
`verify-local --engine cpu` has nothing to compare here; put the library back \
on the judged behaviour version instead: the `dependency_closure` line of this \
same report carries the `cargo update --precise` command for each package it \
names, to run in the workspace that owns the governing lockfile"
);
assert_eq!(
decide("verified", &[reading("regex", "16.0", None)], &no_drift),
"verified"
);
assert_eq!(
decide("verified", &[reading("regex", "16.0", None)], &|unit| {
(unit == "regex").then_some("regex 1.13.1 -> 1.14.0")
}),
"mismatched: the behaviour version of regex could not be read in this binary, so \
the package versions are compared instead: regex 1.13.1 -> 1.14.0. This build \
admits no accelerated cpu route while the core is mismatched, so `verify-local \
--engine cpu` has nothing to compare here; put the library back on the judged \
behaviour version instead: the `dependency_closure` line of this same report \
carries the `cargo update --precise` command for each package it names, to run \
in the workspace that owns the governing lockfile"
);
assert_eq!(
decide(
"verified",
&[
reading("regex", "16.0", Some("16.0")),
reading("onig", "6.9.10", Some("6.9.10")),
],
&no_drift,
),
"verified"
);
}
#[test]
fn a_record_with_no_units_leaves_the_exact_answer_standing() {
assert_eq!(decide("verified", &[], &no_drift), "verified");
assert_eq!(
decide("unlocated: no lockfile", &[], &no_drift),
"unlocated: no lockfile"
);
}
#[test]
fn every_judged_unit_reads_the_way_the_record_says() {
let readings = readings();
assert!(!readings.is_empty(), "no behaviour unit was recorded");
for reading in readings {
let observed = reading
.observed
.as_deref()
.unwrap_or_else(|| panic!("{} could not be read", reading.unit));
assert_eq!(
observed, reading.judged,
"{} reads {observed} and the record judges {}",
reading.unit, reading.judged
);
}
}
#[test]
fn a_unit_this_release_cannot_ask_reads_as_unreadable() {
assert!(observe("regex").is_some());
assert!(observe("onig").is_some());
assert!(observe("unicode-segmentation").is_some());
assert!(observe("unicode_categories").is_none());
assert!(observe("").is_none());
}
#[test]
fn the_age_probe_answers_from_the_tables_rather_than_the_candidates() {
let observed = observe("regex").expect("a regex age");
assert!(AGE_CANDIDATES.contains(&observed.as_str()));
assert!(
regex::Regex::new(&format!(r"\p{{Age={observed}}}")).is_ok(),
"the answer does not compile"
);
let newer = AGE_CANDIDATES
.iter()
.take_while(|age| **age != observed)
.collect::<Vec<_>>();
for age in newer {
assert!(
regex::Regex::new(&format!(r"\p{{Age={age}}}")).is_err(),
"{age} compiles and is newer than the answer"
);
}
}
#[test]
fn the_unit_sentences_say_which_tables_and_which_version() {
assert_eq!(
carries("regex", "17.0"),
"carries Unicode 17.0 (regex tables)"
);
assert_eq!(carries("onig", "6.9.11"), "links Oniguruma 6.9.11");
assert_eq!(
unchanged("regex", "16.0"),
"Unicode data version unchanged (16.0)"
);
assert_eq!(
unchanged("a-new-unit", "1.0"),
"a-new-unit behaviour version unchanged (1.0)"
);
}
#[test]
fn the_drift_line_is_read_by_unit() {
assert_eq!(FIELD_SEPARATOR, '\u{1f}');
assert_eq!(RECORD_SEPARATOR, '\u{1e}');
assert!(drift_for("regex").is_none());
assert!(drift_for("a-unit-that-is-not-recorded").is_none());
}
}