use std::borrow::Cow;
use crate::scanner::ScanError;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ScannerId(pub &'static str);
impl ScannerId {
#[must_use]
pub const fn as_str(self) -> &'static str {
self.0
}
}
impl std::fmt::Display for ScannerId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
Input,
Output,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HoldBack {
TokenBoundary,
MaxLen(usize),
WholeStream,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Disposition {
Transform,
Block,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RestorePolicy {
RoundTrip,
OneWay,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Verdict<'a> {
pub text: Cow<'a, str>,
pub valid: bool,
pub risk: f32,
}
impl<'a> Verdict<'a> {
pub const NOT_A_RISK: f32 = -1.0;
#[must_use]
pub fn transformed(text: Cow<'a, str>) -> Self {
Self {
text,
valid: true,
risk: Self::NOT_A_RISK,
}
}
#[must_use]
pub fn is_risk_judgment(&self) -> bool {
self.risk >= 0.0
}
#[must_use]
pub fn detected(text: &'a str, risk: f32, threshold: f32) -> Self {
Self {
text: Cow::Borrowed(text),
valid: risk < threshold,
risk,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Threshold(f32);
impl Threshold {
#[must_use]
pub fn new(value: f32) -> Self {
Self(value.clamp(0.0, 1.0))
}
#[must_use]
pub const fn get(self) -> f32 {
self.0
}
}
impl Default for Threshold {
fn default() -> Self {
Self(1.0)
}
}
pub type ScanResult<'a> = Result<Verdict<'a>, ScanError>;