amql-cli 0.0.0-alpha.0

AQL command-line interface and REPL
//! Build script: bundles the locale message catalog into the binary.
//!
//! Reads `AQL_LOCALE` (default: `en`), locates `locales/{locale}.toml` in the
//! workspace root, validates it parses as TOML, then copies it to `OUT_DIR/messages.toml`
//! where the binary picks it up via `include_str!`.
//!
//! Future locale builds (e.g. `aql-es`) set `AQL_LOCALE=es` and provide the
//! translation file at `locales/es.toml` — fetched by CD, never committed.

use std::path::PathBuf;

fn main() {
    let locale = std::env::var("AQL_LOCALE").unwrap_or_else(|_| "en".to_string());

    // Workspace root is two levels up from crates/amql-cli/
    let workspace_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("../..")
        .canonicalize()
        .expect("workspace root must exist");

    let locale_file = workspace_root.join(format!("locales/{locale}.toml"));

    if !locale_file.exists() {
        panic!(
            "Locale file not found: {}\n  Set AQL_LOCALE to an available locale or add the file.",
            locale_file.display()
        );
    }

    let content = std::fs::read_to_string(&locale_file)
        .unwrap_or_else(|e| panic!("Failed to read {}: {e}", locale_file.display()));

    // Validate TOML at build time — catch malformed translations before shipping
    content
        .parse::<toml::Table>()
        .unwrap_or_else(|e| panic!("Invalid TOML in {}: {e}", locale_file.display()));

    let out_dir = PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR must be set"));
    std::fs::write(out_dir.join("messages.toml"), &content)
        .unwrap_or_else(|e| panic!("Failed to write messages.toml to OUT_DIR: {e}"));

    // Re-run only if locale file or AQL_LOCALE changes
    println!("cargo:rerun-if-changed={}", locale_file.display());
    println!("cargo:rerun-if-env-changed=AQL_LOCALE");
}