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 #[error(transparent)]
30 InvalidRegex(#[from] RegexValidationError),
31 #[error(transparent)]
33 InvalidKeywords(#[from] ProximityKeywordsValidationError),
34 #[error(transparent)]
36 InvalidMatchAction(#[from] MatchActionValidationError),
37 #[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#[derive(Debug, PartialEq, Eq, Error)]
49pub enum ScannerError {
50 #[error("Transient error while scanning")]
51 Transient,
52}
53
54#[cfg(test)]
55mod test {
56 use crate::match_action::MatchActionValidationError;
57 use crate::proximity_keywords::ProximityKeywordsValidationError;
58 use crate::{CreateScannerError, RegexValidationError};
59
60 fn test_error(error: CreateScannerError, expected_display: &str) {
61 assert_eq!(error.to_string(), expected_display)
62 }
63
64 #[test]
65 fn test_invalid_keywords() {
66 test_error(
67 CreateScannerError::InvalidKeywords(ProximityKeywordsValidationError::EmptyKeyword),
68 "Empty keywords are not allowed",
69 );
70
71 test_error(
72 CreateScannerError::InvalidKeywords(ProximityKeywordsValidationError::TooManyKeywords),
73 "No more than 50 keywords are allowed",
74 );
75
76 test_error(
77 CreateScannerError::InvalidKeywords(ProximityKeywordsValidationError::KeywordTooLong(
78 10,
79 )),
80 "Keywords cannot be longer than the look ahead character count (10)",
81 );
82
83 test_error(
84 CreateScannerError::InvalidKeywords(
85 ProximityKeywordsValidationError::InvalidLookAheadCharacterCount,
86 ),
87 "Look ahead character count should be bigger than 0 and cannot be longer than 50",
88 )
89 }
90
91 #[test]
92 fn test_invalid_regex() {
93 test_error(
94 CreateScannerError::InvalidRegex(RegexValidationError::InvalidSyntax),
95 "Invalid regex syntax",
96 );
97 test_error(
98 CreateScannerError::InvalidRegex(RegexValidationError::ExceededDepthLimit),
99 "The regex pattern is nested too deeply",
100 );
101 test_error(
102 CreateScannerError::InvalidRegex(RegexValidationError::TooComplex),
103 "The regex has exceeded the complexity limit (try simplifying the regex)",
104 );
105 test_error(
106 CreateScannerError::InvalidRegex(RegexValidationError::MatchesEmptyString),
107 "Regex patterns are not allowed to match an empty string",
108 );
109 }
110
111 #[test]
112 fn test_match_action() {
113 test_error(
114 CreateScannerError::InvalidMatchAction(
115 MatchActionValidationError::PartialRedactionNumCharsZero,
116 ),
117 "Partial redaction chars must be non-zero",
118 );
119 }
120}