use crate::application::contract::FailureKind;
use crate::application::diagnostic::{ApplicationDiagnostic, ApplicationDiagnosticCode};
use crate::parser::ast::{EnumDecl, FieldDecl, FieldType, FieldTypeRef, OperationClause};
use std::collections::HashSet;
#[derive(Debug, Clone, Copy)]
pub(crate) struct DeclSite<'a> {
pub logical_module_id: &'a str,
pub line: usize,
pub column: usize,
}
impl DeclSite<'_> {
pub(crate) fn diag(
&self,
code: ApplicationDiagnosticCode,
message: String,
) -> ApplicationDiagnostic {
ApplicationDiagnostic::new(code, message).at(self.logical_module_id, self.line, self.column)
}
}
pub(crate) fn check_duplicate_fields(
owner: &str,
fields: &[FieldDecl],
site: DeclSite,
diags: &mut Vec<ApplicationDiagnostic>,
) {
let mut seen = HashSet::new();
for field in fields {
if !seen.insert(field.name.as_str()) {
diags.push(site.diag(
ApplicationDiagnosticCode::App004,
format!("duplicate field '{}' in '{owner}'", field.name),
));
}
}
}
pub(crate) fn check_record_defaults(
owner: &str,
fields: &[FieldDecl],
site: DeclSite,
diags: &mut Vec<ApplicationDiagnostic>,
) {
for field in fields {
if field.default.is_some() {
diags.push(site.diag(
ApplicationDiagnosticCode::App003,
format!(
"field '{}' in record '{owner}' declares a default; defaults are entity-only",
field.name
),
));
}
}
}
fn is_key_capable(field_type: &FieldType) -> bool {
matches!(
field_type,
FieldType::Scalar(FieldTypeRef { symbol, .. }) if symbol == "uuid" || symbol == "string"
)
}
pub(crate) fn check_entity_key(
owner: &str,
fields: &[FieldDecl],
site: DeclSite,
diags: &mut Vec<ApplicationDiagnostic>,
) -> Option<String> {
let keys: Vec<&FieldDecl> = fields.iter().filter(|f| f.is_key).collect();
match keys.as_slice() {
[] => {
diags.push(site.diag(
ApplicationDiagnosticCode::App005,
format!("entity '{owner}' has no key field"),
));
None
}
[key] => {
let mut ok = true;
if !is_key_capable(&key.field_type) {
diags.push(site.diag(
ApplicationDiagnosticCode::App005,
format!(
"key field '{}' of entity '{owner}' must be uuid or string",
key.name
),
));
ok = false;
}
if key.is_optional {
diags.push(site.diag(
ApplicationDiagnosticCode::App005,
format!(
"key field '{}' of entity '{owner}' must not be optional",
key.name
),
));
ok = false;
}
if key.default.is_some() {
diags.push(site.diag(
ApplicationDiagnosticCode::App005,
format!(
"key field '{}' of entity '{owner}' must not have a default",
key.name
),
));
ok = false;
}
ok.then(|| key.name.clone())
}
many => {
diags.push(site.diag(
ApplicationDiagnosticCode::App005,
format!(
"entity '{owner}' has {} key fields; exactly one is required",
many.len()
),
));
None
}
}
}
fn clause_rank(clause: &OperationClause) -> (usize, &'static str) {
use OperationClause as C;
match clause {
C::Intent(_) => (0, "intent"),
C::Direction { .. } => (1, "direction"),
C::Actor { .. } => (2, "actor"),
C::AccessPublic | C::AccessPolicyGoverned { .. } => (3, "access"),
C::Input { .. } => (4, "input"),
C::Output { .. } => (5, "output"),
C::State { .. } => (6, "state"),
C::Effect { .. } => (7, "effect"),
C::Transaction { .. } => (8, "transaction"),
C::Failure { .. } => (9, "failure"),
C::IdempotencyKeyed { .. }
| C::IdempotencyInherent
| C::IdempotencyNotApplicable { .. } => (10, "idempotency"),
C::ConcurrencyUnique { .. }
| C::ConcurrencyOptimistic { .. }
| C::ConcurrencyReadSnapshot => (11, "concurrency"),
C::EvidenceOperationTrace => (12, "evidence"),
C::LifecycleSynchronousRequestResponse => (13, "lifecycle"),
}
}
const CLAUSE_NAMES: [&str; 14] = [
"intent",
"direction",
"actor",
"access",
"input",
"output",
"state",
"effect",
"transaction",
"failure",
"idempotency",
"concurrency",
"evidence",
"lifecycle",
];
pub(crate) fn validate_clause_shape(clauses: &[OperationClause]) -> Vec<String> {
use OperationClause as C;
let mut defects = Vec::new();
let mut counts = [0usize; 14];
let mut last_rank = 0usize;
let mut ordered = true;
for clause in clauses {
let (rank, _) = clause_rank(clause);
counts[rank] += 1;
if rank < last_rank {
ordered = false;
}
last_rank = rank;
}
for (rank, name) in CLAUSE_NAMES.iter().enumerate() {
if counts[rank] == 0 {
defects.push(format!("missing {name} clause"));
} else if counts[rank] > 1 && rank != 9 {
defects.push(format!("duplicate {name} clause"));
}
}
if !ordered {
defects.push("clauses out of canonical order".to_string());
}
let mut anonymous = false;
let mut public = false;
let mut effect_kind = None;
let mut transaction_kind = None;
for clause in clauses {
match clause {
C::Intent(text) if text.trim().is_empty() => {
defects.push("intent text is empty".to_string());
}
C::Direction { kind } if kind != "inbound" => {
defects.push(format!("direction '{kind}' is unsupported in v0.1"));
}
C::Actor { actor } if actor == "anonymous" => anonymous = true,
C::AccessPublic => public = true,
C::Effect { kind, .. } => effect_kind = Some(kind.as_str()),
C::Transaction { kind } => transaction_kind = Some(kind.as_str()),
_ => {}
}
}
if anonymous && !public && counts[3] > 0 {
defects.push("actor anonymous is legal only with access public".to_string());
}
match (effect_kind, transaction_kind) {
(Some("creates") | Some("mutates"), Some(t)) if t != "single_aggregate" => {
defects.push(format!(
"transaction '{t}' does not pair with a write effect; single_aggregate is required"
));
}
(Some("reads"), Some(t)) if t != "read_only" => {
defects.push(format!(
"transaction '{t}' does not pair with effect reads; read_only is required"
));
}
_ => {}
}
defects
}
fn is_lower_snake_case(code: &str) -> bool {
!code.is_empty()
&& code.split('_').all(|seg| {
!seg.is_empty()
&& seg
.chars()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit())
})
}
pub(crate) fn failure_kind(raw: &str) -> Option<FailureKind> {
Some(match raw {
"input_validation" => FailureKind::InputValidation,
"policy" => FailureKind::Policy,
"missing_state" => FailureKind::MissingState,
"idempotency_conflict" => FailureKind::IdempotencyConflict,
"concurrency_conflict" => FailureKind::ConcurrencyConflict,
_ => return None,
})
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn validate_failures(
operation: &str,
failures: &[(String, Vec<FailureKind>, String)],
required_kinds: &[FailureKind],
closure_codes: &mut HashSet<String>,
site: DeclSite,
diags: &mut Vec<ApplicationDiagnostic>,
) {
let mut kinds_seen: Vec<FailureKind> = Vec::new();
for (code, kinds, meaning) in failures {
if !is_lower_snake_case(code) {
diags.push(site.diag(
ApplicationDiagnosticCode::App008,
format!("failure code '{code}' in operation '{operation}' is not lower_snake_case"),
));
}
if !closure_codes.insert(code.clone()) {
diags.push(site.diag(
ApplicationDiagnosticCode::App008,
format!("failure code '{code}' is not unique in the resolved closure"),
));
}
if meaning.trim().is_empty() {
diags.push(site.diag(
ApplicationDiagnosticCode::App008,
format!("failure '{code}' in operation '{operation}' has an empty meaning"),
));
}
for kind in kinds {
if kinds_seen.contains(kind) {
diags.push(site.diag(
ApplicationDiagnosticCode::App008,
format!(
"failure kind {kind:?} appears on more than one failure of operation '{operation}'"
),
));
} else {
kinds_seen.push(*kind);
}
}
}
for required in required_kinds {
if !kinds_seen.contains(required) {
diags.push(site.diag(
ApplicationDiagnosticCode::App008,
format!(
"operation '{operation}' has a reachable {required:?} path with no declared failure code"
),
));
}
}
}
pub(crate) fn check_enum(decl: &EnumDecl, site: DeclSite, diags: &mut Vec<ApplicationDiagnostic>) {
let mut names = HashSet::new();
let mut wires = HashSet::new();
for member in &decl.members {
if !names.insert(member.name.as_str()) {
diags.push(site.diag(
ApplicationDiagnosticCode::App013,
format!("duplicate enum member '{}' in '{}'", member.name, decl.name),
));
}
if !wires.insert(member.wire.as_str()) {
diags.push(site.diag(
ApplicationDiagnosticCode::App013,
format!(
"duplicate enum wire value \"{}\" in '{}'",
member.wire, decl.name
),
));
}
}
}