1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use smol_str::SmolStr;

pub fn object_ref_matches_table(
    possible_references: &[Vec<SmolStr>],
    targets: &[Vec<SmolStr>],
) -> bool {
    // Simple case: If there are no references, assume okay.
    if possible_references.is_empty() {
        return true;
    }

    // Simple case: Reference exactly matches a target.
    for pr in possible_references {
        if targets.contains(pr) {
            return true;
        }
    }

    // Tricky case: If one is shorter than the other, check for a suffix match.
    for pr in possible_references {
        for t in targets {
            if (pr.len() < t.len() && pr == &t[t.len() - pr.len()..])
                || (t.len() < pr.len() && t == &pr[pr.len() - t.len()..])
            {
                return true;
            }
        }
    }

    false
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_object_ref_matches_table() {
        let test_cases = vec![
            // Empty list of references is always true
            (vec![], vec![vec!["abc".into()]], true),
            // Simple cases: one reference, one target
            (vec![vec!["agent1".into()]], vec![vec!["agent1".into()]], true),
            (vec![vec!["agent1".into()]], vec![vec!["customer".into()]], false),
            // Multiple references. If any match, good.
            (vec![vec!["bar".into()], vec!["user_id".into()]], vec![vec!["bar".into()]], true),
            (vec![vec!["foo".into()], vec!["user_id".into()]], vec![vec!["bar".into()]], false),
            // Multiple targets. If any reference matches, good.
            (
                vec![vec!["table1".into()]],
                vec![vec!["table1".into()], vec!["table2".into()], vec!["table3".into()]],
                true,
            ),
            (vec![vec!["tbl2".into()]], vec![vec!["db".into(), "sc".into(), "tbl1".into()]], false),
            (vec![vec!["tbl2".into()]], vec![vec!["db".into(), "sc".into(), "tbl2".into()]], true),
            // Multi-part references and targets. Checks for a suffix match.
            (
                vec![vec!["Arc".into(), "tbl1".into()]],
                vec![vec!["db".into(), "sc".into(), "tbl1".into()]],
                false,
            ),
            (
                vec![vec!["sc".into(), "tbl1".into()]],
                vec![vec!["db".into(), "sc".into(), "tbl1".into()]],
                true,
            ),
            (
                vec![vec!["cb".into(), "sc".into(), "tbl1".into()]],
                vec![vec!["db".into(), "sc".into(), "tbl1".into()]],
                false,
            ),
            (
                vec![vec!["db".into(), "sc".into(), "tbl1".into()]],
                vec![vec!["db".into(), "sc".into(), "tbl1".into()]],
                true,
            ),
            (vec![vec!["public".into(), "agent1".into()]], vec![vec!["agent1".into()]], true),
            (vec![vec!["public".into(), "agent1".into()]], vec![vec!["public".into()]], false),
        ];

        for (possible_references, targets, expected) in test_cases {
            assert_eq!(object_ref_matches_table(&possible_references, &targets), expected);
        }
    }
}