use super::sequence::Sequences;
use crate::charset::{CharSet, SetCatalog, builtin};
use crate::lint::Lint;
use crate::scan::{Hit, scan_str};
use std::sync::LazyLock;
const CLASSES: [(&str, &str); 5] = [
("hazard-bidi", "bidi-control"),
("hazard-tag", "tag-character"),
("hazard-bom", "stray-bom"),
("hazard-control", "control-character"),
("hazard-invisible", "invisible"),
];
const STRAY_BOM: &str = "stray-bom";
const CONTROL: &str = "control-character";
pub(crate) fn carries_no_text(lint: Lint) -> bool {
lint.group == crate::lint::Group::Hazard && lint.name != CONTROL
}
const FAMILY: &str = "text";
static BUILTIN: LazyLock<Hazards> =
LazyLock::new(|| Hazards::compiled().unwrap_or_else(|_| Hazards::none()));
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Hazards {
classes: Vec<(Lint, CharSet)>,
excusing: Vec<Excuse>,
sequences: Sequences,
}
impl Hazards {
#[must_use]
pub fn builtin() -> Self {
BUILTIN.clone()
}
fn none() -> Self {
Self {
classes: Vec::new(),
excusing: Vec::new(),
sequences: Sequences::default(),
}
}
fn compiled() -> Result<Self, String> {
let classes = classes()?;
let excusing = excusing(&classes)?;
let sequences = Sequences::deferred();
Ok(Self {
classes,
excusing,
sequences,
})
}
pub(crate) fn exempt(&self, text: &str) -> Vec<usize> {
self.sequences.exempt(text)
}
pub(crate) fn selectors(
&self,
text: &str,
granted: impl Fn(char) -> bool,
) -> Vec<usize> {
super::sequence::selectors(text, granted)
}
pub(crate) fn lint_at(&self, hit: Hit, exempt: &[usize]) -> Option<Lint> {
let inside = exempt.binary_search(&hit.position.byte).is_ok();
self.lint_for(hit).filter(|_| !inside)
}
pub(crate) fn excuses(&self, granted: &str, character: char) -> bool {
granted.split('+').any(|name| {
self.excusing.iter().any(|(set, joiners, _)| {
set == name && joiners.contains(&character)
})
})
}
#[must_use]
pub(crate) fn vouched_by(mut self, catalog: &SetCatalog) -> Self {
self.excusing.retain(|(name, _, shipped)| {
catalog
.resolve(name, FAMILY)
.is_ok_and(|run| run == *shipped)
});
self
}
pub fn contains(&self, character: char) -> bool {
self.classes.iter().any(|(_, set)| set.contains(character))
}
#[must_use]
pub fn lints_in(&self, text: &str) -> Vec<(Hit, Lint)> {
let hits = scan_str(text, |c| !self.contains(c));
if hits.is_empty() {
return Vec::new();
}
let exempt = self.exempt(text);
hits.into_iter()
.filter_map(|hit| Some((hit, self.lint_at(hit, &exempt)?)))
.collect()
}
pub(crate) fn lint_for(&self, hit: Hit) -> Option<Lint> {
let lint = self.class_of(hit.character)?;
let signature = lint.name == STRAY_BOM && hit.position.byte == 0;
(!signature).then_some(lint)
}
pub(crate) fn lint_unsigned(
&self,
hit: Hit,
exempt: &[usize],
) -> Option<Lint> {
let inside = exempt.binary_search(&hit.position.byte).is_ok();
self.class_of(hit.character).filter(|_| !inside)
}
pub(crate) fn class_of(&self, character: char) -> Option<Lint> {
let (lint, _) = self
.classes
.iter()
.find(|(_, set)| set.contains(character))?;
Some(*lint)
}
}
type Class = (Lint, CharSet);
fn classes() -> Result<Vec<Class>, String> {
let catalog = builtin::hazard_catalog().map_err(|e| e.to_string())?;
CLASSES
.iter()
.map(|(set, lint)| {
let lint = Lint::named(lint)
.ok_or_else(|| format!("no `{lint}` lint registered"))?;
let set = catalog.resolve(set, FAMILY);
Ok((lint, set.map_err(|e| e.to_string())?))
})
.collect()
}
const JOINERS: [char; 2] = ['\u{200C}', '\u{200D}'];
type Excuse = (String, Vec<char>, CharSet);
fn excusing(classes: &[(Lint, CharSet)]) -> Result<Vec<Excuse>, String> {
let catalog = builtin::catalog().map_err(|e| e.to_string())?;
let mut found = Vec::new();
for name in catalog.names() {
let set = catalog.resolve(name, FAMILY).map_err(|e| e.to_string())?;
let joiners: Vec<char> =
JOINERS.into_iter().filter(|j| set.contains(*j)).collect();
if !joiners.is_empty() && !grants_other_hazard(&set, classes) {
found.push((name.to_owned(), joiners, set));
}
}
Ok(found)
}
fn grants_other_hazard(set: &CharSet, classes: &[(Lint, CharSet)]) -> bool {
classes
.iter()
.flat_map(|(_, class)| &class.ranges)
.any(|range| {
(range.start..=range.end)
.any(|c| !JOINERS.contains(&c) && set.contains(c))
})
}
#[cfg(test)]
mod tests {
use super::Hazards;
use crate::lint::{Group, Level, Levels, Lint, Target};
use crate::scan::{Hit, Position};
fn hazards() -> Hazards {
Hazards::builtin()
}
#[test]
fn the_compiled_data_parses() {
let compiled = Hazards::compiled();
assert!(compiled.is_ok(), "{compiled:?}");
assert_eq!(compiled.ok(), Some(Hazards::builtin()));
assert_ne!(Hazards::builtin(), Hazards::none());
}
fn at(byte: usize, character: char) -> Hit {
let position = Position {
line: 1,
column: 1,
byte,
};
Hit {
position,
character,
}
}
fn fired(character: char) -> &'static str {
hazards().lint_for(at(5, character)).map_or("", |l| l.name)
}
#[test]
fn a_bidi_override_or_isolate_is_a_bidi_control() {
for point in ['\u{202A}', '\u{202E}', '\u{2066}', '\u{2069}'] {
assert_eq!(fired(point), "bidi-control", "{point:?}");
}
assert_eq!(fired('\u{200F}'), "bidi-control");
}
#[test]
fn a_tag_letter_is_a_tag_character() {
assert_eq!(fired('\u{E0041}'), "tag-character");
assert_eq!(fired('\u{E0001}'), "tag-character");
assert_eq!(fired('\u{E007F}'), "tag-character");
}
#[test]
fn a_control_other_than_the_layout_three_is_a_control_character() {
for point in ['\u{0007}', '\u{000B}', '\u{001B}', '\u{007F}'] {
assert_eq!(fired(point), "control-character", "{point:?}");
}
assert_eq!(fired('\u{0085}'), "control-character");
}
#[test]
fn tab_newline_and_carriage_return_are_no_hazard() {
assert_eq!(fired('\t'), "");
assert_eq!(fired('\n'), "");
assert_eq!(fired('\r'), "");
}
#[test]
fn the_rest_of_the_ignorables_are_invisible() {
for point in ['\u{200B}', '\u{200D}', '\u{00AD}', '\u{FE0F}'] {
assert_eq!(fired(point), "invisible", "{point:?}");
}
assert_eq!(fired('\u{3164}'), "invisible");
assert_eq!(fired('\u{E0100}'), "invisible");
}
#[test]
fn ordinary_text_is_no_hazard() {
for point in ['A', ' ', '\u{00E9}', '\u{2014}', '\u{1F600}'] {
assert_eq!(fired(point), "", "{point:?}");
}
assert!(!hazards().contains('A'));
}
#[test]
fn a_bom_is_a_hazard_everywhere_but_byte_zero() {
let found = hazards();
assert_eq!(found.lint_for(at(0, '\u{FEFF}')), None);
let stray = found.lint_for(at(3, '\u{FEFF}')).map(|l| l.name);
assert_eq!(stray, Some("stray-bom"));
assert!(found.contains('\u{FEFF}'));
}
#[test]
fn only_the_shipped_preset_vouches_for_a_joiner() {
use crate::charset::{SetCatalog, builtin, parse_line};
let zwnj = '\u{200C}';
let shipped = builtin::catalog().unwrap_or_else(|_| SetCatalog::new());
assert!(hazards().vouched_by(&shipped).excuses("hindi", zwnj));
let mut redeclared = shipped;
let line = parse_line("hindi ascii U+200C U+200D");
if let Ok(Some(definition)) = line {
redeclared.insert(definition);
}
assert!(!hazards().vouched_by(&redeclared).excuses("hindi", zwnj));
let none = SetCatalog::new();
assert!(!hazards().vouched_by(&none).excuses("hindi", zwnj));
}
#[test]
fn unsigned_text_lets_no_bom_off() {
let lint = hazards().lint_unsigned(at(0, '\u{FEFF}'), &[]);
assert_eq!(lint.map(|l| l.name), Some("stray-bom"));
}
#[test]
fn byte_zero_lets_off_the_bom_and_nothing_else() {
let first = hazards().lint_for(at(0, '\u{202E}')).map(|l| l.name);
assert_eq!(first, Some("bidi-control"));
}
#[test]
fn every_class_fires_a_hazard_lint_at_forbid() {
for (lint, _) in &hazards().classes {
assert_eq!(lint.group, Group::Hazard, "{}", lint.name);
assert_eq!(lint.default_level(), Level::Forbid, "{}", lint.name);
}
}
#[test]
fn no_rule_lowers_a_real_hazard_lint() {
let lint = hazards().lint_for(at(5, '\u{202E}'));
let lint = lint.unwrap_or(Lint::new("bidi-control", Group::Hazard));
let mut levels = Levels::new();
levels.set(Target::Group(Group::Hazard), Level::Allow);
levels.set(Target::Lint(lint), Level::Warn);
levels.set_charset(Level::Allow);
assert_eq!(levels.level_of(lint), Level::Forbid);
}
}