planner / src/knowledge.rs

raw file 2535 bytes
use crate::model::{Language, Library};
use include_dir::{include_dir, Dir};
use std::sync::OnceLock;

static DATA_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/data");

static LANGS: OnceLock<Vec<Language>> = OnceLock::new();
static LIBS: OnceLock<Vec<Library>> = OnceLock::new();

pub fn languages() -> &'static [Language] {
    LANGS.get_or_init(|| {
        let f = DATA_DIR.get_file("languages.json").expect("languages.json");
        serde_json::from_str(f.contents_utf8().unwrap()).unwrap()
    })
}

pub fn libraries() -> &'static [Library] {
    LIBS.get_or_init(|| {
        let f = DATA_DIR.get_file("libraries.json").expect("libraries.json");
        serde_json::from_str(f.contents_utf8().unwrap()).unwrap()
    })
}

pub fn lang_by_id(id: &str) -> Option<&'static Language> {
    languages().iter().find(|l| l.id == id)
}

pub fn libs_for(lang: &str, domain: Option<&str>) -> Vec<&'static Library> {
    libraries()
        .iter()
        .filter(|l| l.lang == lang && domain.map(|d| l.domain == d).unwrap_or(true))
        .collect()
}

pub fn suggest_language(project_type: &str, pref: Option<&str>) -> String {
    if let Some(p) = pref {
        if !p.is_empty() && p != "auto" {
            return p.to_string();
        }
    }
    match project_type {
        "cli" => "rust".to_string(),
        "rest-api" | "web-backend" => "rust".to_string(),
        "web-app" | "web-frontend" | "fullstack" => "typescript".to_string(),
        "library" => "rust".to_string(),
        "desktop" => "rust".to_string(),
        "mobile" => "typescript".to_string(),
        "data-pipeline" | "data" => "python".to_string(),
        "ml" | "ml-project" => "python".to_string(),
        "game" => "rust".to_string(),
        "automation" | "scripting" => "python".to_string(),
        _ => "rust".to_string(),
    }
}

pub fn project_type_aliases(t: &str) -> &'static str {
    let t = t.to_lowercase();
    match t.as_str() {
        "cli" | "command-line" | "terminal" | "tool" => "cli",
        "api" | "rest" | "rest-api" | "backend" | "web-backend" | "service" => "rest-api",
        "web" | "web-app" | "frontend" | "spa" | "site" => "web-app",
        "lib" | "library" | "crate" | "package" => "library",
        "desktop" | "desktop-app" | "gui" => "desktop",
        "mobile" | "ios" | "android" => "mobile",
        "data" | "pipeline" | "etl" | "data-pipeline" => "data-pipeline",
        "ml" | "ai" | "machine-learning" | "model" | "ml-project" => "ml-project",
        "game" => "game",
        _ => "cli",
    }
}