Skip to main content

impactsense_parser/
python_stdlib.rs

1//! Top-level names from CPython `sys.stdlib_module_names` (embedded at build time).
2use std::collections::HashSet;
3use std::sync::OnceLock;
4
5fn stdlib_names() -> &'static HashSet<String> {
6    static SET: OnceLock<HashSet<String>> = OnceLock::new();
7    SET.get_or_init(|| {
8        include_str!("python_stdlib_modules.txt")
9            .lines()
10            .map(|l| l.trim().to_string())
11            .filter(|l| !l.is_empty())
12            .collect()
13    })
14}
15
16/// True if the first component of a dotted import is a stdlib module name.
17pub fn is_python_stdlib_top_level(import_dotted: &str) -> bool {
18    let top = import_dotted
19        .trim()
20        .split('.')
21        .next()
22        .unwrap_or("")
23        .trim();
24    !top.is_empty() && stdlib_names().contains(top)
25}