no-mistakes-postgres-engine 0.109.129

PostgreSQL embedded SQL extraction, schema catalog, and replay-safety analysis for no-mistakes
Documentation
use super::{slash_normalize, AllowList, CatalogObjectRef, RuleFinding};

impl AllowList {
    pub fn apply(self, catalog_path: &str, findings: Vec<RuleFinding>) -> Vec<RuleFinding> {
        self.apply_with(
            catalog_path,
            findings,
            |finding| finding,
            |finding| finding,
            &[],
        )
    }

    pub fn apply_with<T>(
        self,
        catalog_path: &str,
        mut findings: Vec<T>,
        finding: impl Fn(&T) -> &RuleFinding,
        wrap: impl Fn(RuleFinding) -> T,
        consumed: &[String],
    ) -> Vec<T> {
        let path = slash_normalize(catalog_path);
        let mut used = vec![false; self.entries.len()];
        for (key, slot) in self.keys.iter().zip(&mut used) {
            if consumed.iter().any(|object| object == key) {
                *slot = true;
            }
        }
        findings.retain(|finding_value| {
            let Some(target) = finding(finding_value).target.as_deref() else {
                return true;
            };
            let target = canonical_key(target);
            let mut matched = false;
            for (key, slot) in self.keys.iter().zip(&mut used) {
                if *key == target {
                    *slot = true;
                    matched = true;
                }
            }
            !matched
        });
        for (index, was_used) in used.into_iter().enumerate() {
            if was_used {
                continue;
            }
            let entry = &self.entries[index];
            findings.push(wrap(RuleFinding {
                causes: Vec::new(),
                rule: self.rule_id.clone(),
                file: path.clone(),
                line: 1,
                message: format!(
                    "{path}: stale {} allow entry: {}",
                    self.rule_id, entry.object
                ),
                import: None,
                target: Some(entry.object.clone()),
            }));
        }
        findings
    }
}

pub fn canonical_key(object: &str) -> String {
    use super::super::names::normalize_table_name as table_key;
    let Ok(parsed) = object.parse::<CatalogObjectRef>() else {
        return object.to_string();
    };
    match parsed {
        CatalogObjectRef::Table(table) => CatalogObjectRef::Table(table_key(&table)),
        CatalogObjectRef::Column { table, column } => CatalogObjectRef::Column {
            table: table_key(&table),
            column,
        },
        CatalogObjectRef::Index { table, index } => CatalogObjectRef::Index {
            table: table_key(&table),
            index,
        },
        CatalogObjectRef::Trigger { table, trigger } => CatalogObjectRef::Trigger {
            table: table_key(&table),
            trigger,
        },
        CatalogObjectRef::Constraint { table, name } => CatalogObjectRef::Constraint {
            table: table_key(&table),
            name,
        },
        other => other,
    }
    .to_string()
}