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