/ scripts / check-domain-fact-contract.sh
check-domain-fact-contract.sh
 1  #!/usr/bin/env bash
 2  set -euo pipefail
 3  
 4  root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
 5  cd "$root"
 6  
 7  fail=0
 8  
 9  echo "Domain fact contract lint"
10  
11  # Ensure DomainFact derive annotations include schema_version + context/context_fn.
12  while IFS= read -r line; do
13    file="${line%%:*}"
14    attr="${line#*:}"
15    if [[ "$attr" != *"schema_version"* ]]; then
16      echo "Missing schema_version in domain_fact attribute: ${file}"
17      fail=1
18    fi
19    if [[ "$attr" != *"context"* && "$attr" != *"context_fn"* ]]; then
20      echo "Missing context/context_fn in domain_fact attribute: ${file}"
21      fail=1
22    fi
23  done < <(rg -n --no-heading "#\\[domain_fact\\(" crates)
24  
25  # Validate fact type IDs declare schema versions and reducers.
26  while IFS= read -r line; do
27    file="${line%%:*}"
28    const_name=$(echo "$line" | sed -E 's/.*pub const ([A-Z0-9_]+_FACT_TYPE_ID).*/\1/')
29    type_id=$(echo "$line" | sed -E 's/.*= "([^"]+)".*/\1/')
30  
31    if [[ -z "$const_name" || -z "$type_id" ]]; then
32      continue
33    fi
34  
35    if rg -n --no-heading "#\\[domain_fact\\(.*type_id = \"${type_id}\"" "$file" >/dev/null; then
36      :
37    else
38      schema_const="${const_name/_TYPE_ID/_SCHEMA_VERSION}"
39      if ! rg -n --no-heading "pub const ${schema_const}: u16" "$file" >/dev/null; then
40        echo "Missing schema version constant ${schema_const} for ${type_id} in ${file}"
41        fail=1
42      fi
43      if ! rg -n --no-heading "encode_domain_fact|VersionedMessage" "$file" >/dev/null; then
44        echo "Missing canonical encoding helper for ${type_id} in ${file}"
45        fail=1
46      fi
47    fi
48  
49    if ! rg -n --no-heading "FactReducer" "$file" >/dev/null; then
50      echo "Missing FactReducer implementation near ${const_name} in ${file}"
51      fail=1
52    fi
53  done < <(rg -n --no-heading "pub const [A-Z0-9_]+_FACT_TYPE_ID: &str = \"[^\"]+\";" crates)
54  
55  if [[ "$fail" -ne 0 ]]; then
56    echo "Domain fact contract lint failed"
57    exit 1
58  fi
59  
60  echo "Domain fact contract lint passed"