use std::sync::Arc;
use crate::{EvaluationResult, SecurityCategory};
#[derive(Debug, Clone)]
pub struct ExternalL1Input {
pub category: SecurityCategory,
pub text: Arc<str>,
}
impl ExternalL1Input {
pub fn new(category: SecurityCategory, text: impl Into<String>) -> Self {
Self::from_shared_text(category, Arc::<str>::from(text.into()))
}
pub fn from_shared_text(category: SecurityCategory, text: Arc<str>) -> Self {
Self { category, text }
}
}
pub trait ExternalL1Detector: Send + Sync {
fn id(&self) -> &'static str;
fn category(&self) -> SecurityCategory;
fn evaluate(&self, input: &ExternalL1Input) -> EvaluationResult;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn inputs_can_share_request_text_across_categories() {
let text = Arc::<str>::from("shared large request text");
let first =
ExternalL1Input::from_shared_text(SecurityCategory::Injection, Arc::clone(&text));
let second = ExternalL1Input::from_shared_text(SecurityCategory::Dlp, Arc::clone(&text));
assert!(Arc::ptr_eq(&first.text, &second.text));
assert!(Arc::ptr_eq(&first.text, &text));
}
}