1
//! The trigger host's invariant: it links nothing of the RPC registry, so no
2
//! script it runs can resolve a name.
3

            
4
use std::collections::HashSet;
5

            
6
use scripting::ScriptExecutor;
7

            
8
use super::{Finding, Subject};
9
use crate::natives::all_compiler_specs;
10

            
11
1
fn finding(reason: String) -> Finding {
12
1
    Finding {
13
1
        user: None,
14
1
        subject: Subject::TriggerHost,
15
1
        reason,
16
1
    }
17
1
}
18

            
19
/// The number of imports the trigger linker binds, and a finding for each that
20
/// is also a native of the RPC registry.
21
3
pub(super) fn check() -> (usize, Vec<Finding>) {
22
3
    match ScriptExecutor::try_new().and_then(|executor| executor.linked_imports()) {
23
3
        Ok(imports) => (imports.len(), offenders(&imports, &registry())),
24
        Err(err) => (0, vec![finding(format!("cannot list the imports: {err}"))]),
25
    }
26
3
}
27

            
28
4
fn registry() -> HashSet<(String, String)> {
29
4
    all_compiler_specs()
30
4
        .into_iter()
31
280
        .map(|spec| (spec.import_module, spec.import_name))
32
        .collect()
33
4
}
34

            
35
4
fn offenders(imports: &[(String, String)], registry: &HashSet<(String, String)>) -> Vec<Finding> {
36
4
    imports
37
4
        .iter()
38
38
        .filter(|import| registry.contains(*import))
39
1
        .map(|(module, name)| finding(format!("links the registry native {module}.{name}")))
40
        .collect()
41
4
}
42

            
43
#[cfg(test)]
44
mod tests;