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