no_mistakes_postgres_engine/catalog/findings/
allow.rs1use super::{slash_normalize, table_key, AllowList, CatalogObjectRef, RuleFinding};
2
3impl AllowList {
4 pub fn valid_column_selector(selector: &str) -> bool {
5 table_key::column_splits(selector).next().is_some()
6 }
7
8 pub fn has_column_selectors(&self) -> bool {
9 self.entries
10 .iter()
11 .any(|entry| entry.object.starts_with("column:"))
12 }
13
14 pub fn column_selector_is_ambiguous(&self, index: usize) -> bool {
15 self.ambiguous_columns[index]
16 }
17
18 pub fn bind_catalog_columns(self, catalog: &crate::catalog::SchemaCatalog) -> Self {
19 self.bind_column_candidates(catalog.tables().flat_map(|table| {
20 table
21 .columns
22 .iter()
23 .map(|column| (table.name.as_str(), column.name.as_str()))
24 }))
25 }
26
27 pub fn bind_column_candidates<'a>(
28 mut self,
29 candidates: impl IntoIterator<Item = (&'a str, &'a str)>,
30 ) -> Self {
31 if self.has_column_selectors() {
32 self.ambiguous_columns = table_key::ambiguous_columns(
33 self.entries.iter().map(|entry| entry.object.as_str()),
34 candidates,
35 );
36 }
37 self
38 }
39
40 pub fn apply(self, catalog_path: &str, findings: Vec<RuleFinding>) -> Vec<RuleFinding> {
41 self.apply_with(
42 catalog_path,
43 findings,
44 |finding| finding,
45 |finding| finding,
46 &[],
47 )
48 }
49
50 pub fn apply_with<T>(
51 self,
52 catalog_path: &str,
53 mut findings: Vec<T>,
54 finding: impl Fn(&T) -> &RuleFinding,
55 wrap: impl Fn(RuleFinding) -> T,
56 consumed: &[usize],
57 ) -> Vec<T> {
58 let mut used = vec![false; self.entries.len()];
59 for &index in consumed {
60 used[index] = true;
61 }
62 findings.retain(|finding_value| {
63 let Some(target) = finding(finding_value).target.as_deref() else {
64 return true;
65 };
66 !mark_used(&mut used, self.matching_entries([target]))
67 });
68 findings.extend(self.unused_findings(catalog_path, &used).map(wrap));
69 findings
70 }
71
72 pub fn matching_objects<'a, const N: usize>(
73 &'a self,
74 objects: [&'a CatalogObjectRef; N],
75 ) -> impl Iterator<Item = bool> + 'a {
76 let targets = objects.map(|object| {
77 (!matches!(object, CatalogObjectRef::Column { .. }))
78 .then(|| canonical_key(&object.to_string()))
79 });
80 self.entries
81 .iter()
82 .zip(self.keys.iter().zip(&self.ambiguous_columns))
83 .map(move |(entry, (key, ambiguous))| {
84 !ambiguous
85 && objects
86 .iter()
87 .zip(&targets)
88 .any(|(object, target)| match object {
89 CatalogObjectRef::Column { table, column } => {
90 table_key::matches_column(&entry.object, table, column)
91 }
92 _ => target.as_ref() == Some(key),
93 })
94 })
95 }
96
97 pub fn apply_objects(
98 self,
99 catalog_path: &str,
100 findings: Vec<(CatalogObjectRef, RuleFinding)>,
101 ) -> Vec<RuleFinding> {
102 let mut used = vec![false; self.entries.len()];
103 let mut findings: Vec<_> = findings
104 .into_iter()
105 .filter_map(|(object, finding)| {
106 (!mark_used(&mut used, self.matching_objects([&object]))).then_some(finding)
107 })
108 .collect();
109 findings.extend(self.unused_findings(catalog_path, &used));
110 findings
111 }
112
113 fn unused_findings<'a>(
114 &'a self,
115 catalog_path: &str,
116 used: &'a [bool],
117 ) -> impl Iterator<Item = RuleFinding> + 'a {
118 let path = slash_normalize(catalog_path);
119 self.entries
120 .iter()
121 .zip(used)
122 .filter(|(_, used)| !**used)
123 .map(move |(entry, _)| RuleFinding {
124 causes: Vec::new(),
125 uncovered_lines: Vec::new(),
126 rule: self.rule_id.clone(),
127 file: path.clone(),
128 line: 1,
129 message: format!(
130 "{path}: stale {} allow entry: {}",
131 self.rule_id, entry.object
132 ),
133 import: None,
134 target: Some(entry.object.clone()),
135 })
136 }
137}
138
139fn mark_used(used: &mut [bool], matches: impl Iterator<Item = bool>) -> bool {
140 let mut matched = false;
141 for (used, entry_matches) in used.iter_mut().zip(matches) {
142 if entry_matches {
143 *used = true;
144 matched = true;
145 }
146 }
147 matched
148}
149
150pub fn canonical_key(object: &str) -> String {
151 use super::super::names::normalize_table_name as table_key;
152 let Ok(parsed) = object.parse::<CatalogObjectRef>() else {
153 return object.to_string();
154 };
155 match parsed {
156 CatalogObjectRef::Table(table) => CatalogObjectRef::Table(table_key(&table)),
157 CatalogObjectRef::Column { table, column } => CatalogObjectRef::Column {
158 table: table_key(&table),
159 column,
160 },
161 CatalogObjectRef::Index { table, index } => CatalogObjectRef::Index {
162 table: table_key(&table),
163 index,
164 },
165 CatalogObjectRef::Trigger { table, trigger } => CatalogObjectRef::Trigger {
166 table: table_key(&table),
167 trigger,
168 },
169 CatalogObjectRef::Constraint { table, name } => CatalogObjectRef::Constraint {
170 table: table_key(&table),
171 name,
172 },
173 other => other,
174 }
175 .to_string()
176}