rils_frontend 0.4.0

Static analysis and semantic frontend for the Rils scripting language
Documentation
//! Only namespaces reached through public bindings belong to a dependency's API.
use super::{ExportTable, ExportVisibility};

/// Host calls use canonical names, so each component of that spelling must be public.
pub fn is_public_module_path(exports: &ExportTable, mut path: &str) -> bool {
    while !path.is_empty() {
        let (parent, name) = path.rsplit_once("::").unwrap_or(("", path));
        if !exports.get(parent).is_some_and(|items| {
            items.iter().any(|item| {
                item.name == name
                    && item.target_module.as_deref() == Some(path)
                    && item.visibility == ExportVisibility::Public
            })
        }) {
            return false;
        }
        path = parent;
    }
    true
}

pub fn public_exports(exports: &ExportTable) -> ExportTable {
    let mut result = ExportTable::new();
    let mut pending = vec![String::new()];
    while let Some(path) = pending.pop() {
        if result.contains_key(&path) {
            continue;
        }
        let Some(members) = exports.get(&path) else {
            continue;
        };
        let members = members
            .iter()
            .filter(|export| export.visibility == ExportVisibility::Public)
            .cloned()
            .collect::<Vec<_>>();
        pending.extend(
            members
                .iter()
                .filter_map(|export| export.target_module.clone()),
        );
        result.insert(path, members);
    }
    result
}