1use std::fmt;
10
11#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
17#[non_exhaustive]
18pub enum SecretScanErrorCode {
19 InvalidInput,
22 InvalidOptions,
25 InvalidDetector,
27 DetectorFailure,
29 InvalidCandidate,
31 PolicyFailure,
33 InvalidPolicyAction,
37 InvalidFindings,
40 PlaceholderFailure,
42 InvalidPlaceholder,
45 InvalidLimits,
49 InputLimitExceeded,
53 BufferLimitExceeded,
56 TokenLimitExceeded,
59 MultilineLimitExceeded,
62 FindingLimitExceeded,
67 InvalidState,
70 InvalidRuleset,
76 PiiSelectorInvalid,
78 PiiSelectorUnsupported,
80 PiiSelectorUnavailable,
82 PiiActivationConflict,
84 InvalidActionPolicy,
90 InvalidDetectionConfig,
96 DetectionConfigConflict,
99 EmptyDetectionSet,
103}
104
105impl SecretScanErrorCode {
106 pub const ALL: [Self; 26] = [
108 Self::InvalidInput,
109 Self::InvalidOptions,
110 Self::InvalidDetector,
111 Self::DetectorFailure,
112 Self::InvalidCandidate,
113 Self::PolicyFailure,
114 Self::InvalidPolicyAction,
115 Self::InvalidFindings,
116 Self::PlaceholderFailure,
117 Self::InvalidPlaceholder,
118 Self::InvalidLimits,
119 Self::InputLimitExceeded,
120 Self::BufferLimitExceeded,
121 Self::TokenLimitExceeded,
122 Self::MultilineLimitExceeded,
123 Self::FindingLimitExceeded,
124 Self::InvalidState,
125 Self::InvalidRuleset,
126 Self::PiiSelectorInvalid,
127 Self::PiiSelectorUnsupported,
128 Self::PiiSelectorUnavailable,
129 Self::PiiActivationConflict,
130 Self::InvalidActionPolicy,
131 Self::InvalidDetectionConfig,
132 Self::DetectionConfigConflict,
133 Self::EmptyDetectionSet,
134 ];
135
136 #[must_use]
138 pub const fn as_str(self) -> &'static str {
139 match self {
140 Self::InvalidInput => "INVALID_INPUT",
141 Self::InvalidOptions => "INVALID_OPTIONS",
142 Self::InvalidDetector => "INVALID_DETECTOR",
143 Self::DetectorFailure => "DETECTOR_FAILURE",
144 Self::InvalidCandidate => "INVALID_CANDIDATE",
145 Self::PolicyFailure => "POLICY_FAILURE",
146 Self::InvalidPolicyAction => "INVALID_POLICY_ACTION",
147 Self::InvalidFindings => "INVALID_FINDINGS",
148 Self::PlaceholderFailure => "PLACEHOLDER_FAILURE",
149 Self::InvalidPlaceholder => "INVALID_PLACEHOLDER",
150 Self::InvalidLimits => "INVALID_LIMITS",
151 Self::InputLimitExceeded => "INPUT_LIMIT_EXCEEDED",
152 Self::BufferLimitExceeded => "BUFFER_LIMIT_EXCEEDED",
153 Self::TokenLimitExceeded => "TOKEN_LIMIT_EXCEEDED",
154 Self::MultilineLimitExceeded => "MULTILINE_LIMIT_EXCEEDED",
155 Self::FindingLimitExceeded => "FINDING_LIMIT_EXCEEDED",
156 Self::InvalidState => "INVALID_STATE",
157 Self::InvalidRuleset => "INVALID_RULESET",
158 Self::PiiSelectorInvalid => "PII_SELECTOR_INVALID",
159 Self::PiiSelectorUnsupported => "PII_SELECTOR_UNSUPPORTED",
160 Self::PiiSelectorUnavailable => "PII_SELECTOR_UNAVAILABLE",
161 Self::PiiActivationConflict => "PII_ACTIVATION_CONFLICT",
162 Self::InvalidActionPolicy => "INVALID_ACTION_POLICY",
163 Self::InvalidDetectionConfig => "INVALID_DETECTION_CONFIG",
164 Self::DetectionConfigConflict => "DETECTION_CONFIG_CONFLICT",
165 Self::EmptyDetectionSet => "EMPTY_DETECTION_SET",
166 }
167 }
168
169 #[must_use]
171 pub const fn message(self) -> &'static str {
172 match self {
173 Self::InvalidInput => "Secret scan input must be a string.",
174 Self::InvalidOptions => "Secret scan options are invalid.",
175 Self::InvalidDetector => "Invalid detector registration.",
176 Self::DetectorFailure => "A secret detector failed.",
177 Self::InvalidCandidate => "A secret detector returned an invalid candidate.",
178 Self::PolicyFailure => "The secret policy failed.",
179 Self::InvalidPolicyAction => "The secret policy returned an invalid action.",
180 Self::InvalidFindings => "Redaction findings are invalid.",
181 Self::PlaceholderFailure => "The placeholder formatter failed.",
182 Self::InvalidPlaceholder => "The placeholder formatter returned an invalid value.",
183 Self::InvalidLimits => "Secret scan limits are invalid.",
184 Self::InputLimitExceeded => "Secret scan input limit exceeded.",
185 Self::BufferLimitExceeded => "Incremental sanitizer buffer limit exceeded.",
186 Self::TokenLimitExceeded => "Incremental sanitizer token limit exceeded.",
187 Self::MultilineLimitExceeded => "Incremental sanitizer multiline limit exceeded.",
188 Self::FindingLimitExceeded => "Secret scan finding limit exceeded.",
189 Self::InvalidState => "The incremental sanitizer is no longer accepting input.",
190 Self::InvalidRuleset => "The supplied ruleset is invalid.",
191 Self::PiiSelectorInvalid => "PII selector is invalid.",
192 Self::PiiSelectorUnsupported => "PII jurisdiction or family is unsupported.",
193 Self::PiiSelectorUnavailable => "PII selection is unavailable in this artifact.",
194 Self::PiiActivationConflict => "PII activation is already initialized differently.",
195 Self::InvalidActionPolicy => "The supplied action policy is invalid.",
196 Self::InvalidDetectionConfig => "The detector selection is invalid.",
197 Self::DetectionConfigConflict => {
198 "The detector selection is already initialized differently."
199 }
200 Self::EmptyDetectionSet => "The configuration enables no detector.",
201 }
202 }
203}
204
205impl fmt::Display for SecretScanErrorCode {
206 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
207 f.write_str(self.as_str())
208 }
209}
210
211#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
236pub struct SecretScanError {
237 code: SecretScanErrorCode,
238}
239
240impl SecretScanError {
241 #[must_use]
243 pub const fn new(code: SecretScanErrorCode) -> Self {
244 Self { code }
245 }
246
247 #[must_use]
249 pub const fn code(self) -> SecretScanErrorCode {
250 self.code
251 }
252
253 #[must_use]
255 pub const fn message(self) -> &'static str {
256 self.code.message()
257 }
258}
259
260impl From<SecretScanErrorCode> for SecretScanError {
261 fn from(code: SecretScanErrorCode) -> Self {
262 Self::new(code)
263 }
264}
265
266impl fmt::Display for SecretScanError {
267 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
268 f.write_str(self.message())
269 }
270}
271
272impl std::error::Error for SecretScanError {}
273
274#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
280pub struct DetectorFailure;
281
282#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
286pub struct PolicyFailure;
287
288#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
292pub struct FormatterFailure;
293
294#[cfg(test)]
295mod tests {
296 use super::*;
297
298 #[test]
299 fn codes_and_messages_are_fixed() {
300 let expected = [
301 ("INVALID_INPUT", "Secret scan input must be a string."),
302 ("INVALID_OPTIONS", "Secret scan options are invalid."),
303 ("INVALID_DETECTOR", "Invalid detector registration."),
304 ("DETECTOR_FAILURE", "A secret detector failed."),
305 (
306 "INVALID_CANDIDATE",
307 "A secret detector returned an invalid candidate.",
308 ),
309 ("POLICY_FAILURE", "The secret policy failed."),
310 (
311 "INVALID_POLICY_ACTION",
312 "The secret policy returned an invalid action.",
313 ),
314 ("INVALID_FINDINGS", "Redaction findings are invalid."),
315 ("PLACEHOLDER_FAILURE", "The placeholder formatter failed."),
316 (
317 "INVALID_PLACEHOLDER",
318 "The placeholder formatter returned an invalid value.",
319 ),
320 ("INVALID_LIMITS", "Secret scan limits are invalid."),
321 ("INPUT_LIMIT_EXCEEDED", "Secret scan input limit exceeded."),
322 (
323 "BUFFER_LIMIT_EXCEEDED",
324 "Incremental sanitizer buffer limit exceeded.",
325 ),
326 (
327 "TOKEN_LIMIT_EXCEEDED",
328 "Incremental sanitizer token limit exceeded.",
329 ),
330 (
331 "MULTILINE_LIMIT_EXCEEDED",
332 "Incremental sanitizer multiline limit exceeded.",
333 ),
334 (
335 "FINDING_LIMIT_EXCEEDED",
336 "Secret scan finding limit exceeded.",
337 ),
338 (
339 "INVALID_STATE",
340 "The incremental sanitizer is no longer accepting input.",
341 ),
342 ("INVALID_RULESET", "The supplied ruleset is invalid."),
343 ("PII_SELECTOR_INVALID", "PII selector is invalid."),
344 (
345 "PII_SELECTOR_UNSUPPORTED",
346 "PII jurisdiction or family is unsupported.",
347 ),
348 (
349 "PII_SELECTOR_UNAVAILABLE",
350 "PII selection is unavailable in this artifact.",
351 ),
352 (
353 "PII_ACTIVATION_CONFLICT",
354 "PII activation is already initialized differently.",
355 ),
356 (
357 "INVALID_ACTION_POLICY",
358 "The supplied action policy is invalid.",
359 ),
360 (
361 "INVALID_DETECTION_CONFIG",
362 "The detector selection is invalid.",
363 ),
364 (
365 "DETECTION_CONFIG_CONFLICT",
366 "The detector selection is already initialized differently.",
367 ),
368 (
369 "EMPTY_DETECTION_SET",
370 "The configuration enables no detector.",
371 ),
372 ];
373 assert_eq!(SecretScanErrorCode::ALL.len(), expected.len());
374 for (code, (name, message)) in SecretScanErrorCode::ALL.into_iter().zip(expected) {
375 assert_eq!(code.as_str(), name);
376 assert_eq!(code.message(), message);
377 assert_eq!(code.to_string(), name);
378 let error = SecretScanError::new(code);
379 assert_eq!(error.code(), code);
380 assert_eq!(error.to_string(), message);
381 assert_eq!(
382 format!("{error:?}"),
383 format!("SecretScanError {{ code: {code:?} }}")
384 );
385 }
386 }
387
388 #[test]
389 fn opaque_failures_carry_nothing() {
390 assert_eq!(std::mem::size_of::<DetectorFailure>(), 0);
391 assert_eq!(std::mem::size_of::<PolicyFailure>(), 0);
392 assert_eq!(std::mem::size_of::<FormatterFailure>(), 0);
393 assert_eq!(std::mem::size_of::<SecretScanError>(), 1);
394 }
395}