use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum AcknowledgeableWarningCategory {
VirtualFieldPath,
DocSnippetReservedDomain,
}
impl AcknowledgeableWarningCategory {
pub fn config_value(self) -> &'static str {
match self {
Self::VirtualFieldPath => "virtual_field_path",
Self::DocSnippetReservedDomain => "doc_snippet_reserved_domain",
}
}
}
impl std::fmt::Display for AcknowledgeableWarningCategory {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(self.config_value())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct WarningAcknowledgement {
pub category: AcknowledgeableWarningCategory,
pub identity: String,
pub target: String,
#[serde(default)]
pub reason: Option<String>,
}
impl WarningAcknowledgement {
pub fn config_entry_for(category: AcknowledgeableWarningCategory, identity: &str, target: &str) -> String {
format!("{{ category = \"{category}\", identity = \"{identity}\", target = \"{target}\" }}")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_acknowledgeable_category_round_trips_through_toml() {
let toml_str = r#"
category = "doc_snippet_reserved_domain"
identity = "extract_uri"
target = "python"
"#;
let entry: WarningAcknowledgement = toml::from_str(toml_str).expect("a well-formed entry parses");
assert_eq!(entry.category, AcknowledgeableWarningCategory::DocSnippetReservedDomain);
assert_eq!(entry.identity, "extract_uri");
assert_eq!(entry.target, "python");
assert_eq!(entry.reason, None);
}
#[test]
fn a_non_acknowledgeable_category_is_rejected_by_deserialization_not_merely_discouraged() {
let toml_str = r#"
category = "scaffold_ownership_refusal"
identity = "unowned-app"
target = "go"
"#;
let error = toml::from_str::<WarningAcknowledgement>(toml_str)
.expect_err("a category with no enum variant must fail to parse");
let message = error.to_string();
assert!(
message.contains("scaffold_ownership_refusal") || message.contains("unknown variant"),
"error must name the offending, non-acknowledgeable category: {message}"
);
}
#[test]
fn an_unknown_key_is_rejected_not_silently_dropped() {
let toml_str = r#"
category = "doc_snippet_reserved_domain"
identity = "extract_uri"
target = "python"
severity = "low"
"#;
let error = toml::from_str::<WarningAcknowledgement>(toml_str).expect_err("an unknown key must be rejected");
assert!(
error.to_string().contains("severity"),
"error must name the offending key: {error}"
);
}
#[test]
fn config_entry_for_renders_the_exact_shape_a_consumer_pastes_back() {
let rendered = WarningAcknowledgement::config_entry_for(
AcknowledgeableWarningCategory::DocSnippetReservedDomain,
"extract_uri",
"python",
);
assert_eq!(
rendered,
r#"{ category = "doc_snippet_reserved_domain", identity = "extract_uri", target = "python" }"#
);
}
}