dd_sds/scanner/
error.rs

1use std::convert::From;
2use thiserror::Error;
3
4use crate::{
5    match_action::MatchActionValidationError, proximity_keywords::ProximityKeywordsValidationError,
6    RegexValidationError,
7};
8
9impl From<CreateScannerError> for i64 {
10    fn from(value: CreateScannerError) -> i64 {
11        match value {
12            CreateScannerError::InvalidRegex(_) => -2,
13            CreateScannerError::InvalidKeywords(_) => -3,
14            CreateScannerError::InvalidMatchAction(_) => -4,
15            CreateScannerError::InvalidMatchValidator(_) => -5,
16        }
17    }
18}
19
20#[derive(Debug, PartialEq, Eq, Error)]
21pub enum MatchValidatorCreationError {
22    #[error("Internal error while creating the match validator")]
23    InternalError,
24}
25
26#[derive(Debug, PartialEq, Eq, Error)]
27pub enum CreateScannerError {
28    //The regex is invalid (too long, too complex, etc.)
29    #[error(transparent)]
30    InvalidRegex(#[from] RegexValidationError),
31    /// The included keywords config is invalid (empty keyword, too many keywords, etc.)
32    #[error(transparent)]
33    InvalidKeywords(#[from] ProximityKeywordsValidationError),
34    /// Invalid configuration of a match action
35    #[error(transparent)]
36    InvalidMatchAction(#[from] MatchActionValidationError),
37    /// The match validator cannot be created
38    #[error(transparent)]
39    InvalidMatchValidator(#[from] MatchValidatorCreationError),
40}
41
42#[derive(Debug, PartialEq, Eq, Error)]
43pub enum MatchValidationError {
44    #[error("No MatchValidationType provided")]
45    NoMatchValidationType,
46}
47
48#[cfg(test)]
49mod test {
50    use crate::match_action::MatchActionValidationError;
51    use crate::proximity_keywords::ProximityKeywordsValidationError;
52    use crate::{CreateScannerError, RegexValidationError};
53
54    fn test_error(error: CreateScannerError, expected_display: &str) {
55        assert_eq!(error.to_string(), expected_display)
56    }
57
58    #[test]
59    fn test_invalid_keywords() {
60        test_error(
61            CreateScannerError::InvalidKeywords(ProximityKeywordsValidationError::EmptyKeyword),
62            "Empty keywords are not allowed",
63        );
64
65        test_error(
66            CreateScannerError::InvalidKeywords(ProximityKeywordsValidationError::TooManyKeywords),
67            "No more than 50 keywords are allowed",
68        );
69
70        test_error(
71            CreateScannerError::InvalidKeywords(ProximityKeywordsValidationError::KeywordTooLong(
72                10,
73            )),
74            "Keywords cannot be longer than the look ahead character count (10)",
75        );
76
77        test_error(
78            CreateScannerError::InvalidKeywords(
79                ProximityKeywordsValidationError::InvalidLookAheadCharacterCount,
80            ),
81            "Look ahead character count should be bigger than 0 and cannot be longer than 50",
82        )
83    }
84
85    #[test]
86    fn test_invalid_regex() {
87        test_error(
88            CreateScannerError::InvalidRegex(RegexValidationError::InvalidSyntax),
89            "Invalid regex syntax",
90        );
91        test_error(
92            CreateScannerError::InvalidRegex(RegexValidationError::ExceededDepthLimit),
93            "The regex pattern is nested too deeply",
94        );
95        test_error(
96            CreateScannerError::InvalidRegex(RegexValidationError::TooComplex),
97            "The regex has exceeded the complexity limit (try simplifying the regex)",
98        );
99        test_error(
100            CreateScannerError::InvalidRegex(RegexValidationError::MatchesEmptyString),
101            "Regex patterns are not allowed to match an empty string",
102        );
103    }
104
105    #[test]
106    fn test_match_action() {
107        test_error(
108            CreateScannerError::InvalidMatchAction(
109                MatchActionValidationError::PartialRedactionNumCharsZero,
110            ),
111            "Partial redaction chars must be non-zero",
112        );
113    }
114}