omnigraph_compiler/lint/
codes.rs1use super::diagnostic::{Family, SafetyTier, Severity};
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub struct DiagnosticCode {
21 pub code: &'static str,
23 pub family: Family,
24 pub tier: SafetyTier,
25 pub default_severity: Severity,
26 pub short: &'static str,
27}
28
29pub 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
40pub 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
49pub 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
58pub 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
67pub 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
78pub 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
90pub 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
101pub 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
112pub 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
118pub 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
127pub 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}