use std::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum SecretScanErrorCode {
InvalidInput,
InvalidOptions,
InvalidDetector,
DetectorFailure,
InvalidCandidate,
PolicyFailure,
InvalidPolicyAction,
InvalidFindings,
PlaceholderFailure,
InvalidPlaceholder,
InvalidLimits,
InputLimitExceeded,
BufferLimitExceeded,
TokenLimitExceeded,
MultilineLimitExceeded,
FindingLimitExceeded,
InvalidState,
InvalidRuleset,
PiiSelectorInvalid,
PiiSelectorUnsupported,
PiiSelectorUnavailable,
PiiActivationConflict,
InvalidActionPolicy,
InvalidDetectionConfig,
DetectionConfigConflict,
EmptyDetectionSet,
}
impl SecretScanErrorCode {
pub const ALL: [Self; 26] = [
Self::InvalidInput,
Self::InvalidOptions,
Self::InvalidDetector,
Self::DetectorFailure,
Self::InvalidCandidate,
Self::PolicyFailure,
Self::InvalidPolicyAction,
Self::InvalidFindings,
Self::PlaceholderFailure,
Self::InvalidPlaceholder,
Self::InvalidLimits,
Self::InputLimitExceeded,
Self::BufferLimitExceeded,
Self::TokenLimitExceeded,
Self::MultilineLimitExceeded,
Self::FindingLimitExceeded,
Self::InvalidState,
Self::InvalidRuleset,
Self::PiiSelectorInvalid,
Self::PiiSelectorUnsupported,
Self::PiiSelectorUnavailable,
Self::PiiActivationConflict,
Self::InvalidActionPolicy,
Self::InvalidDetectionConfig,
Self::DetectionConfigConflict,
Self::EmptyDetectionSet,
];
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::InvalidInput => "INVALID_INPUT",
Self::InvalidOptions => "INVALID_OPTIONS",
Self::InvalidDetector => "INVALID_DETECTOR",
Self::DetectorFailure => "DETECTOR_FAILURE",
Self::InvalidCandidate => "INVALID_CANDIDATE",
Self::PolicyFailure => "POLICY_FAILURE",
Self::InvalidPolicyAction => "INVALID_POLICY_ACTION",
Self::InvalidFindings => "INVALID_FINDINGS",
Self::PlaceholderFailure => "PLACEHOLDER_FAILURE",
Self::InvalidPlaceholder => "INVALID_PLACEHOLDER",
Self::InvalidLimits => "INVALID_LIMITS",
Self::InputLimitExceeded => "INPUT_LIMIT_EXCEEDED",
Self::BufferLimitExceeded => "BUFFER_LIMIT_EXCEEDED",
Self::TokenLimitExceeded => "TOKEN_LIMIT_EXCEEDED",
Self::MultilineLimitExceeded => "MULTILINE_LIMIT_EXCEEDED",
Self::FindingLimitExceeded => "FINDING_LIMIT_EXCEEDED",
Self::InvalidState => "INVALID_STATE",
Self::InvalidRuleset => "INVALID_RULESET",
Self::PiiSelectorInvalid => "PII_SELECTOR_INVALID",
Self::PiiSelectorUnsupported => "PII_SELECTOR_UNSUPPORTED",
Self::PiiSelectorUnavailable => "PII_SELECTOR_UNAVAILABLE",
Self::PiiActivationConflict => "PII_ACTIVATION_CONFLICT",
Self::InvalidActionPolicy => "INVALID_ACTION_POLICY",
Self::InvalidDetectionConfig => "INVALID_DETECTION_CONFIG",
Self::DetectionConfigConflict => "DETECTION_CONFIG_CONFLICT",
Self::EmptyDetectionSet => "EMPTY_DETECTION_SET",
}
}
#[must_use]
pub const fn message(self) -> &'static str {
match self {
Self::InvalidInput => "Secret scan input must be a string.",
Self::InvalidOptions => "Secret scan options are invalid.",
Self::InvalidDetector => "Invalid detector registration.",
Self::DetectorFailure => "A secret detector failed.",
Self::InvalidCandidate => "A secret detector returned an invalid candidate.",
Self::PolicyFailure => "The secret policy failed.",
Self::InvalidPolicyAction => "The secret policy returned an invalid action.",
Self::InvalidFindings => "Redaction findings are invalid.",
Self::PlaceholderFailure => "The placeholder formatter failed.",
Self::InvalidPlaceholder => "The placeholder formatter returned an invalid value.",
Self::InvalidLimits => "Secret scan limits are invalid.",
Self::InputLimitExceeded => "Secret scan input limit exceeded.",
Self::BufferLimitExceeded => "Incremental sanitizer buffer limit exceeded.",
Self::TokenLimitExceeded => "Incremental sanitizer token limit exceeded.",
Self::MultilineLimitExceeded => "Incremental sanitizer multiline limit exceeded.",
Self::FindingLimitExceeded => "Secret scan finding limit exceeded.",
Self::InvalidState => "The incremental sanitizer is no longer accepting input.",
Self::InvalidRuleset => "The supplied ruleset is invalid.",
Self::PiiSelectorInvalid => "PII selector is invalid.",
Self::PiiSelectorUnsupported => "PII jurisdiction or family is unsupported.",
Self::PiiSelectorUnavailable => "PII selection is unavailable in this artifact.",
Self::PiiActivationConflict => "PII activation is already initialized differently.",
Self::InvalidActionPolicy => "The supplied action policy is invalid.",
Self::InvalidDetectionConfig => "The detector selection is invalid.",
Self::DetectionConfigConflict => {
"The detector selection is already initialized differently."
}
Self::EmptyDetectionSet => "The configuration enables no detector.",
}
}
}
impl fmt::Display for SecretScanErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct SecretScanError {
code: SecretScanErrorCode,
}
impl SecretScanError {
#[must_use]
pub const fn new(code: SecretScanErrorCode) -> Self {
Self { code }
}
#[must_use]
pub const fn code(self) -> SecretScanErrorCode {
self.code
}
#[must_use]
pub const fn message(self) -> &'static str {
self.code.message()
}
}
impl From<SecretScanErrorCode> for SecretScanError {
fn from(code: SecretScanErrorCode) -> Self {
Self::new(code)
}
}
impl fmt::Display for SecretScanError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.message())
}
}
impl std::error::Error for SecretScanError {}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct DetectorFailure;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct PolicyFailure;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct FormatterFailure;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn codes_and_messages_are_fixed() {
let expected = [
("INVALID_INPUT", "Secret scan input must be a string."),
("INVALID_OPTIONS", "Secret scan options are invalid."),
("INVALID_DETECTOR", "Invalid detector registration."),
("DETECTOR_FAILURE", "A secret detector failed."),
(
"INVALID_CANDIDATE",
"A secret detector returned an invalid candidate.",
),
("POLICY_FAILURE", "The secret policy failed."),
(
"INVALID_POLICY_ACTION",
"The secret policy returned an invalid action.",
),
("INVALID_FINDINGS", "Redaction findings are invalid."),
("PLACEHOLDER_FAILURE", "The placeholder formatter failed."),
(
"INVALID_PLACEHOLDER",
"The placeholder formatter returned an invalid value.",
),
("INVALID_LIMITS", "Secret scan limits are invalid."),
("INPUT_LIMIT_EXCEEDED", "Secret scan input limit exceeded."),
(
"BUFFER_LIMIT_EXCEEDED",
"Incremental sanitizer buffer limit exceeded.",
),
(
"TOKEN_LIMIT_EXCEEDED",
"Incremental sanitizer token limit exceeded.",
),
(
"MULTILINE_LIMIT_EXCEEDED",
"Incremental sanitizer multiline limit exceeded.",
),
(
"FINDING_LIMIT_EXCEEDED",
"Secret scan finding limit exceeded.",
),
(
"INVALID_STATE",
"The incremental sanitizer is no longer accepting input.",
),
("INVALID_RULESET", "The supplied ruleset is invalid."),
("PII_SELECTOR_INVALID", "PII selector is invalid."),
(
"PII_SELECTOR_UNSUPPORTED",
"PII jurisdiction or family is unsupported.",
),
(
"PII_SELECTOR_UNAVAILABLE",
"PII selection is unavailable in this artifact.",
),
(
"PII_ACTIVATION_CONFLICT",
"PII activation is already initialized differently.",
),
(
"INVALID_ACTION_POLICY",
"The supplied action policy is invalid.",
),
(
"INVALID_DETECTION_CONFIG",
"The detector selection is invalid.",
),
(
"DETECTION_CONFIG_CONFLICT",
"The detector selection is already initialized differently.",
),
(
"EMPTY_DETECTION_SET",
"The configuration enables no detector.",
),
];
assert_eq!(SecretScanErrorCode::ALL.len(), expected.len());
for (code, (name, message)) in SecretScanErrorCode::ALL.into_iter().zip(expected) {
assert_eq!(code.as_str(), name);
assert_eq!(code.message(), message);
assert_eq!(code.to_string(), name);
let error = SecretScanError::new(code);
assert_eq!(error.code(), code);
assert_eq!(error.to_string(), message);
assert_eq!(
format!("{error:?}"),
format!("SecretScanError {{ code: {code:?} }}")
);
}
}
#[test]
fn opaque_failures_carry_nothing() {
assert_eq!(std::mem::size_of::<DetectorFailure>(), 0);
assert_eq!(std::mem::size_of::<PolicyFailure>(), 0);
assert_eq!(std::mem::size_of::<FormatterFailure>(), 0);
assert_eq!(std::mem::size_of::<SecretScanError>(), 1);
}
}