#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum CacheWarningReason {
Missing,
CacheRootUnavailable,
Unreadable,
SourceMismatch,
Incompatible,
Invalid,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct CacheWarning {
reason: &'static str,
recovery: &'static str,
}
impl CacheWarning {
pub(crate) fn compile_from_text(reason: CacheWarningReason) -> Self {
return Self {
reason: reason_token(reason),
recovery: "compile-from-text",
}
}
pub(crate) fn write_failed() -> Self {
return Self {
reason: "write-failed",
recovery: "continue-with-compiled-rules",
}
}
}
fn reason_token(reason: CacheWarningReason) -> &'static str {
if reason == CacheWarningReason::Missing {
return "missing";
}
if reason == CacheWarningReason::CacheRootUnavailable {
return "cache-root-unavailable";
}
if reason == CacheWarningReason::Unreadable {
return "unreadable";
}
if reason == CacheWarningReason::SourceMismatch {
return "source-mismatch";
}
if reason == CacheWarningReason::Incompatible {
return "incompatible";
}
return "invalid"
}
impl std::fmt::Display for CacheWarning {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
return write!(
formatter,
"{{\"type\":\"forbidden-strings/cache-warning\",\"schemaVersion\":1,\"reason\":\"{}\",\"recovery\":\"{}\"}}",
self.reason,
self.recovery,
)
}
}
#[cfg(test)]
#[path = "warning_tests.rs"]
mod tests;