use std::fmt;
use serde::{Deserialize, Serialize};
use crate::DiagnosticSeverity;
pub const RULE_ID_PATTERN: &str = r"^[a-z0-9][a-z0-9-]*$";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "codegen", derive(schemars::JsonSchema, ts_rs::TS))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[cfg_attr(feature = "codegen", schemars(title = "Modelith rule set"))]
pub struct RuleSet {
pub rules: Vec<Rule>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "codegen", derive(schemars::JsonSchema, ts_rs::TS))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Rule {
pub id: RuleId,
pub severity: DiagnosticSeverity,
pub message: String,
#[serde(rename = "match")]
pub target: ElementMatch,
pub require: Requirement,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "codegen", derive(schemars::JsonSchema, ts_rs::TS))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ElementMatch {
pub kind: OneOrMany,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "codegen", ts(optional))]
pub phase: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "codegen", ts(optional))]
pub name: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "codegen", derive(schemars::JsonSchema, ts_rs::TS))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Requirement {
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "codegen", ts(optional))]
pub incoming: Option<OneOrMany>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "codegen", ts(optional))]
pub outgoing: Option<OneOrMany>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "codegen", ts(optional))]
pub name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "codegen", ts(optional))]
pub phase: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "codegen", ts(optional))]
pub ports: Option<CountRange>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "codegen", ts(optional))]
pub not: Option<Box<Requirement>>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "codegen", derive(schemars::JsonSchema, ts_rs::TS))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct CountRange {
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "codegen", ts(optional))]
pub min: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "codegen", ts(optional))]
pub max: Option<u32>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "codegen", derive(schemars::JsonSchema, ts_rs::TS))]
#[serde(untagged)]
pub enum OneOrMany {
One(String),
Many(Vec<String>),
}
impl OneOrMany {
pub fn as_slice(&self) -> &[String] {
match self {
Self::One(s) => std::slice::from_ref(s),
Self::Many(v) => v,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)]
#[cfg_attr(feature = "codegen", derive(ts_rs::TS))]
#[cfg_attr(feature = "codegen", ts(type = "string"))]
pub struct RuleId(String);
impl<'de> Deserialize<'de> for RuleId {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
String::deserialize(d)?
.try_into()
.map_err(serde::de::Error::custom)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RuleIdError(pub String);
impl fmt::Display for RuleIdError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"invalid rule id {:?}: expected lowercase letters, digits and '-' like \"req-has-satisfier\"",
self.0
)
}
}
impl std::error::Error for RuleIdError {}
impl RuleId {
pub fn as_str(&self) -> &str {
&self.0
}
fn is_valid(s: &str) -> bool {
let is_word = |c: char| c.is_ascii_lowercase() || c.is_ascii_digit();
let mut chars = s.chars();
chars.next().is_some_and(is_word) && chars.all(|c| is_word(c) || c == '-')
}
}
impl TryFrom<String> for RuleId {
type Error = RuleIdError;
fn try_from(s: String) -> Result<Self, Self::Error> {
if Self::is_valid(&s) {
Ok(Self(s))
} else {
Err(RuleIdError(s))
}
}
}
impl std::str::FromStr for RuleId {
type Err = RuleIdError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
s.to_owned().try_into()
}
}
impl From<RuleId> for String {
fn from(id: RuleId) -> Self {
id.0
}
}
impl fmt::Display for RuleId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[cfg(feature = "codegen")]
impl schemars::JsonSchema for RuleId {
fn schema_name() -> std::borrow::Cow<'static, str> {
"RuleId".into()
}
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
schemars::json_schema!({
"type": "string",
"pattern": RULE_ID_PATTERN,
"description": "Rule id unique within the plugin, e.g. \"req-has-satisfier\"."
})
}
}
#[cfg(test)]
mod tests {
use super::*;
fn rule_with(require: &str) -> String {
format!(
r#"{{"rules":[{{"id":"r","severity":"warning","message":"m","match":{{"kind":"part def"}},"require":{require}}}]}}"#
)
}
fn parse(json: &str) -> Result<RuleSet, serde_json::Error> {
serde_json::from_str(json)
}
#[test]
fn parses_full_rule() {
let json = r#"{"rules":[{
"id": "req-has-satisfier",
"severity": "warning",
"message": "要求「{name}」を満たす部品がありません",
"match": { "kind": "requirement def", "phase": "SystemRequirements", "name": "^[A-Z]" },
"require": { "incoming": "satisfy" }
}]}"#;
let set = parse(json).unwrap();
assert_eq!(
set.rules,
vec![Rule {
id: "req-has-satisfier".parse().unwrap(),
severity: DiagnosticSeverity::Warning,
message: "要求「{name}」を満たす部品がありません".to_owned(),
target: ElementMatch {
kind: OneOrMany::One("requirement def".to_owned()),
phase: Some("SystemRequirements".to_owned()),
name: Some("^[A-Z]".to_owned()),
},
require: Requirement {
incoming: Some(OneOrMany::One("satisfy".to_owned())),
..Requirement::default()
},
}]
);
}
#[test]
fn one_or_many_accepts_string_and_list() {
let inputs = [r#""a""#, r#"["a","b"]"#];
let slices: Vec<Vec<String>> = inputs
.iter()
.map(|s| {
serde_json::from_str::<OneOrMany>(s)
.unwrap()
.as_slice()
.to_vec()
})
.collect();
assert_eq!(slices, [vec!["a"], vec!["a", "b"]]);
}
#[test]
fn parses_every_require_field_and_nested_not() {
let json = rule_with(
r#"{"outgoing":["derivation","flow"],"name":"^[A-Z]\\w*$","phase":true,
"ports":{"min":1,"max":8},"not":{"incoming":"satisfy"}}"#,
);
let require = parse(&json).unwrap().rules.remove(0).require;
assert_eq!(
require,
Requirement {
incoming: None,
outgoing: Some(OneOrMany::Many(vec![
"derivation".to_owned(),
"flow".to_owned()
])),
name: Some(r"^[A-Z]\w*$".to_owned()),
phase: Some(true),
ports: Some(CountRange {
min: Some(1),
max: Some(8)
}),
not: Some(Box::new(Requirement {
incoming: Some(OneOrMany::One("satisfy".to_owned())),
..Requirement::default()
})),
}
);
}
#[test]
fn accepts_empty_rule_list() {
let json = r#"{"rules":[]}"#;
let set = parse(json).unwrap();
assert!(set.rules.is_empty());
}
#[test]
fn accepts_rule_ids_at_the_edges() {
let ids = ["a", "0", "a-", "req-2"];
let results: Vec<bool> = ids.iter().map(|id| id.parse::<RuleId>().is_ok()).collect();
assert_eq!(results, [true, true, true, true]);
}
#[test]
fn rejects_malformed_rule_ids() {
let ids = ["", "-a", "Req", "req_x", "req.x", "要求"];
let accepted: Vec<&str> = ids
.iter()
.copied()
.filter(|id| id.parse::<RuleId>().is_ok())
.collect();
assert!(accepted.is_empty(), "accepted: {accepted:?}");
}
#[test]
fn rejects_unknown_keys_at_every_level() {
let cases = [
r#"{"rules":[],"extra":1}"#.to_owned(),
r#"{"rules":[{"id":"r","severity":"hint","message":"m","match":{"kind":"x"},"require":{},"extra":1}]}"#.to_owned(),
r#"{"rules":[{"id":"r","severity":"hint","message":"m","match":{"kind":"x","extra":1},"require":{}}]}"#.to_owned(),
rule_with(r#"{"extra":1}"#),
rule_with(r#"{"ports":{"min":1,"extra":1}}"#),
rule_with(r#"{"not":{"extra":1}}"#),
];
let accepted: Vec<&String> = cases.iter().filter(|c| parse(c).is_ok()).collect();
assert!(accepted.is_empty(), "accepted: {accepted:?}");
}
#[test]
fn rejects_missing_match_kind() {
let json = r#"{"rules":[{"id":"r","severity":"hint","message":"m","match":{"phase":"P"},"require":{}}]}"#;
let result = parse(json);
assert!(result.is_err());
}
#[test]
fn rejects_unknown_severity() {
let json = r#"{"rules":[{"id":"r","severity":"fatal","message":"m","match":{"kind":"x"},"require":{}}]}"#;
let result = parse(json);
assert!(result.is_err());
}
#[test]
fn rejects_negative_port_count() {
let json = rule_with(r#"{"ports":{"min":-1}}"#);
let result = parse(&json);
assert!(result.is_err());
}
#[test]
fn rejects_kind_that_is_neither_string_nor_list() {
let inputs = ["1", r#"["a",1]"#];
let accepted: Vec<&str> = inputs
.iter()
.copied()
.filter(|s| serde_json::from_str::<OneOrMany>(s).is_ok())
.collect();
assert!(accepted.is_empty(), "accepted: {accepted:?}");
}
}