Skip to main content

rust_censure/structs/
mod.rs

1use std::collections::HashMap;
2use std::sync::{Arc};
3use parking_lot::RwLock;
4use once_cell::sync::Lazy;
5use thiserror::Error;
6use crate::lang::LangProvider;
7
8#[derive(Debug)]
9pub struct LangData {
10    pub beep: &'static str,
11    pub beep_html: &'static str,
12
13    pub foul_data: &'static HashMap<&'static str, Vec<&'static str>>,
14    pub foul_core: &'static HashMap<&'static str, &'static str>,
15    pub excludes_data: &'static HashMap<&'static str, Vec<&'static str>>,
16    pub excludes_core: &'static HashMap<&'static str, &'static str>,
17    pub bad_semi_phrases: &'static Vec<&'static str>,
18    pub bad_phrases: &'static Vec<&'static str>,
19    pub trans_tab: &'static HashMap<char, char>,
20}
21
22impl LangData {
23    pub fn get_beep(&self) -> &str {
24        self.beep
25    }
26
27    pub fn set_beep(&mut self, val: &'static str) -> &str {
28        self.beep = val;
29        self.beep
30    }
31
32    pub fn get_beep_html(&self) -> &str {
33        self.beep_html
34    }
35
36    pub fn set_beep_html(&mut self, val: &'static str) -> &str {
37        self.beep_html = val;
38        self.beep_html
39    }
40}
41
42#[derive(Clone, Copy, Debug)]
43pub enum CensorLang { Ru, En }
44
45pub struct Censor {
46    pub lang: Arc<dyn LangProvider + Send + Sync + 'static>,
47    pub data: LangData,
48    pub re_cache: Lazy<Arc<RwLock<HashMap<String, fancy_regex::Regex>>>>
49}
50
51#[derive(Debug, Error)]
52pub enum CensorError {
53    #[error("unsupported language: {0}")]
54    UnsupportedLang(String),
55
56    #[error("regex compilation failed: {0}")]
57    RegexCompilationFailed(String)
58}
59
60#[derive(Debug)]
61pub struct WordInfo {
62    pub is_good: bool,
63    pub word: Box<str>,
64    pub accuse: Vec<Box<str>>,
65    pub excuse: Vec<Box<str>>,
66}
67impl WordInfo {
68    pub fn new(word: Box<str>) -> Self {
69        Self { is_good: true, word, accuse: vec![], excuse: vec![] }
70    }
71}
72
73#[derive(Debug)]
74pub struct CleanLineResult {
75    pub line: String,
76    pub bad_words_count: usize,
77    pub bad_phrases_count: usize,
78    pub detected_bad_words: Vec<Box<str>>,
79    pub detected_patterns: Vec<Box<str>>,
80}
81
82#[derive(Debug)]
83pub struct CleanHtmlResult {
84    pub line: String,
85    pub bad_words_count: usize,
86}