use std::sync::LazyLock;
use fallow_config::{ExternalPluginDef, FallowConfig, RulePackDef, RulesConfig};
static DEFAULT_RULE_SEVERITIES: LazyLock<serde_json::Value> = LazyLock::new(|| {
serde_json::to_value(RulesConfig::default())
.unwrap_or_else(|_| serde_json::Value::Object(serde_json::Map::new()))
});
#[must_use]
pub fn config_schema() -> serde_json::Value {
FallowConfig::json_schema()
}
#[must_use]
pub fn plugin_schema() -> serde_json::Value {
ExternalPluginDef::json_schema()
}
#[must_use]
pub fn rule_pack_schema() -> serde_json::Value {
RulePackDef::json_schema()
}
#[cfg(feature = "schema")]
#[must_use]
pub fn similar_code_snapshot_schema() -> serde_json::Value {
fallow_output::SimilarCodeCandidateSnapshot::json_schema()
}
#[must_use]
pub fn default_rule_severities() -> serde_json::Value {
DEFAULT_RULE_SEVERITIES.clone()
}
#[must_use]
pub fn is_rule_severity_key(key: &str) -> bool {
DEFAULT_RULE_SEVERITIES
.get(key)
.is_some_and(serde_json::Value::is_string)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn schemas_are_json_objects_with_properties() {
for (label, schema) in [
("config", config_schema()),
("plugin", plugin_schema()),
("rule-pack", rule_pack_schema()),
] {
assert!(
schema
.get("properties")
.is_some_and(serde_json::Value::is_object),
"{label} schema must be an object schema with properties"
);
}
}
#[test]
fn default_severities_are_keyed_by_config_key() {
let defaults = default_rule_severities();
assert_eq!(defaults["unused-exports"], "error");
assert_eq!(defaults["security-sink"], "off");
assert!(
defaults.get("unused_exports").is_none(),
"keys are kebab-case"
);
assert!(is_rule_severity_key("coverage-gaps"));
assert!(!is_rule_severity_key("code-duplication"));
}
}