Skip to main content

omnigraph_compiler/lint/
codes.rs

1//! Schema-lint code catalog (MR-694 v0).
2//!
3//! Codes have the form `OG-XXX-NNN` where `XXX` is the family prefix from
4//! [`super::diagnostic::Family`]. Each code carries a default safety
5//! tier, severity, and short description. Codes are stable: once
6//! published, the meaning is frozen and `omnigraph.yaml` may override
7//! severity but never the tier or family.
8//!
9//! ## v0 catalog
10//!
11//! This PR (chassis v0) seeds the catalog with the codes attached to
12//! existing `UnsupportedChange` emissions in
13//! `catalog::schema_plan`. Subsequent PRs add codes for new migration
14//! variants (MR-695..702), the CD/VE/LK/NM families, and so on.
15
16use super::diagnostic::{Family, SafetyTier, Severity};
17
18/// Static catalog entry for a single diagnostic code.
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub struct DiagnosticCode {
21    /// The stable code string, e.g. `"OG-DS-104"`.
22    pub code: &'static str,
23    pub family: Family,
24    pub tier: SafetyTier,
25    pub default_severity: Severity,
26    pub short: &'static str,
27}
28
29// ─── Destructive (DS) — data-loss; always requires explicit opt-in ──────────
30
31/// Reserved: dropping an entire graph (schema-level). Not yet emitted.
32pub const OG_DS_101: DiagnosticCode = DiagnosticCode {
33    code: "OG-DS-101",
34    family: Family::DS,
35    tier: SafetyTier::Destructive,
36    default_severity: Severity::Error,
37    short: "drop graph type with rows",
38};
39
40/// Drop a node type that has rows.
41pub const OG_DS_102: DiagnosticCode = DiagnosticCode {
42    code: "OG-DS-102",
43    family: Family::DS,
44    tier: SafetyTier::Destructive,
45    default_severity: Severity::Error,
46    short: "drop node type with rows",
47};
48
49/// Drop an edge type that has rows.
50pub const OG_DS_103: DiagnosticCode = DiagnosticCode {
51    code: "OG-DS-103",
52    family: Family::DS,
53    tier: SafetyTier::Destructive,
54    default_severity: Severity::Error,
55    short: "drop edge type with rows",
56};
57
58/// Drop a property (column) that has data.
59pub const OG_DS_104: DiagnosticCode = DiagnosticCode {
60    code: "OG-DS-104",
61    family: Family::DS,
62    tier: SafetyTier::Destructive,
63    default_severity: Severity::Error,
64    short: "drop property with rows",
65};
66
67/// Reserved: dropping a populated vector / embedding column. Distinct
68/// from a normal property drop because it invalidates downstream
69/// `nearest()` / `@embed` references.
70pub const OG_DS_105: DiagnosticCode = DiagnosticCode {
71    code: "OG-DS-105",
72    family: Family::DS,
73    tier: SafetyTier::Destructive,
74    default_severity: Severity::Error,
75    short: "drop populated vector column",
76};
77
78// ─── Maybe-fail (MF) — data-dependent; may fail on existing rows ────────────
79
80/// Add a required (non-nullable) property to a populated type without
81/// `@default`. Existing rows have no value to fill in.
82pub const OG_MF_103: DiagnosticCode = DiagnosticCode {
83    code: "OG-MF-103",
84    family: Family::MF,
85    tier: SafetyTier::Validated,
86    default_severity: Severity::Error,
87    short: "add required property without @default to populated type",
88};
89
90/// Tighten nullable to non-nullable. May fail on existing null rows.
91/// Reserved for a follow-up that wires the validated-tier scan; not
92/// emitted in v0.
93pub const OG_MF_104: DiagnosticCode = DiagnosticCode {
94    code: "OG-MF-104",
95    family: Family::MF,
96    tier: SafetyTier::Validated,
97    default_severity: Severity::Error,
98    short: "tighten nullable to non-nullable",
99};
100
101/// Narrow a scalar (e.g. I64 → I32, F64 → F32). Lossy cast that may
102/// truncate or overflow. Today emitted on any `prop_type` change since
103/// the v1 planner doesn't yet distinguish widening from narrowing.
104pub const OG_MF_106: DiagnosticCode = DiagnosticCode {
105    code: "OG-MF-106",
106    family: Family::MF,
107    tier: SafetyTier::Destructive,
108    default_severity: Severity::Error,
109    short: "narrowing scalar type",
110};
111
112/// All v0 catalog entries. Used for chassis-level invariants
113/// (uniqueness, family coverage).
114pub const ALL_CODES: &[DiagnosticCode] = &[
115    OG_DS_101, OG_DS_102, OG_DS_103, OG_DS_104, OG_DS_105, OG_MF_103, OG_MF_104, OG_MF_106,
116];
117
118/// Codes actually emitted by the planner in v0 (i.e. not reserved).
119pub const EMITTED_IN_V0: &[&str] = &[
120    "OG-DS-102",
121    "OG-DS-103",
122    "OG-DS-104",
123    "OG-MF-103",
124    "OG-MF-106",
125];
126
127/// Look up a code by its string identifier.
128pub fn lookup(code: &str) -> Option<&'static DiagnosticCode> {
129    ALL_CODES.iter().find(|c| c.code == code)
130}
131
132#[cfg(test)]
133mod tests {
134    use super::*;
135
136    #[test]
137    fn codes_are_unique() {
138        let mut seen = std::collections::HashSet::new();
139        for c in ALL_CODES {
140            assert!(seen.insert(c.code), "duplicate code: {}", c.code);
141        }
142    }
143
144    #[test]
145    fn code_strings_match_family_prefix() {
146        for c in ALL_CODES {
147            let expected_prefix = format!("OG-{}-", c.family.prefix());
148            assert!(
149                c.code.starts_with(&expected_prefix),
150                "{} doesn't start with {}",
151                c.code,
152                expected_prefix
153            );
154        }
155    }
156
157    #[test]
158    fn code_strings_have_three_digit_suffix() {
159        for c in ALL_CODES {
160            let suffix = c.code.rsplit('-').next().unwrap();
161            assert_eq!(suffix.len(), 3, "{}: suffix not 3 chars", c.code);
162            assert!(
163                suffix.chars().all(|ch| ch.is_ascii_digit()),
164                "{}: suffix not all digits",
165                c.code
166            );
167        }
168    }
169
170    #[test]
171    fn destructive_tier_defaults_to_error_severity() {
172        for c in ALL_CODES {
173            if c.tier == SafetyTier::Destructive {
174                assert_eq!(
175                    c.default_severity,
176                    Severity::Error,
177                    "{}: destructive must default to Error",
178                    c.code
179                );
180            }
181        }
182    }
183
184    #[test]
185    fn lookup_finds_known_codes() {
186        assert_eq!(lookup("OG-DS-104"), Some(&OG_DS_104));
187        assert_eq!(lookup("OG-MF-103"), Some(&OG_MF_103));
188        assert!(lookup("OG-XX-999").is_none());
189    }
190
191    #[test]
192    fn emitted_codes_exist_in_catalog() {
193        for code in EMITTED_IN_V0 {
194            assert!(
195                lookup(code).is_some(),
196                "EMITTED_IN_V0 contains {} but it isn't in ALL_CODES",
197                code
198            );
199        }
200    }
201}