qua 0.73.0

Structure-aware query CLI for the Quarb engine
//! How the command was started: as its own program, or hosted.
//!
//! As a program of its own (`cargo install qua`, a build of this
//! tree) the command is the process: its arguments are the
//! process's, and its own file starts it again. In the `quarb-full`
//! wheel it is hosted (ruling 2026-11-21): `qua` is a console script,
//! the engine an extension module the script's Python loads, and the
//! process is Python's. There the process's arguments are
//! `python …/bin/qua ARGS`, and its own file is the interpreter. So
//! the three things the command reads of its process come from here,
//! and the host names them once, before the command runs.
//!
//! The native binary of the wheel needed the interpreter's shared
//! library on the loader's path, which a fresh Debian or Ubuntu, a
//! uv-managed Python and a conda environment do not give
//! (measured 2026-11-21). A module the interpreter loads needs
//! nothing of the kind.

use std::ffi::OsString;
use std::path::PathBuf;
use std::sync::OnceLock;

/// What a host says of the command it runs.
pub struct Invocation {
    /// The arguments as a program sees them: the command's name
    /// first (`qua`), then what the user wrote.
    pub args: Vec<OsString>,
    /// What starts this command again: a program and the arguments
    /// that stand before the command's own (`python -m quarb_full`).
    /// The resident session starts itself so.
    pub relaunch: Vec<OsString>,
    /// The file that is this build of the engine (the extension
    /// module's): a session tells two builds apart by it.
    pub identity: Option<PathBuf>,
}

static HOSTED: OnceLock<Invocation> = OnceLock::new();

/// Name the invocation; the first naming stands.
pub(crate) fn host(invocation: Invocation) {
    HOSTED.set(invocation).ok();
}

/// The arguments, the command's name first.
pub(crate) fn args_os() -> Vec<OsString> {
    match HOSTED.get() {
        Some(h) => h.args.clone(),
        None => std::env::args_os().collect(),
    }
}

/// The arguments as text, the command's name first.
pub(crate) fn args() -> Vec<String> {
    args_os()
        .iter()
        .map(|a| a.to_string_lossy().into_owned())
        .collect()
}

/// The program that starts this command again and the arguments
/// that stand before the command's own.
#[cfg(unix)]
#[cfg(feature = "adapters")]
pub(crate) fn relaunch() -> std::io::Result<(OsString, Vec<OsString>)> {
    match HOSTED.get().map(|h| h.relaunch.as_slice()) {
        Some([program, before @ ..]) => Ok((program.clone(), before.to_vec())),
        _ => Ok((std::env::current_exe()?.into_os_string(), Vec::new())),
    }
}

/// The file that is this build of the engine.
#[cfg(unix)]
#[cfg(feature = "adapters")]
pub(crate) fn identity() -> Option<PathBuf> {
    match HOSTED.get() {
        Some(h) => h.identity.clone(),
        None => std::env::current_exe().ok(),
    }
}