M3: STL/OBJ import with mesh validation, mass/COM/inertia, material database, RON scene with content-addressed blobs and v0->v1 migration. M4: deterministic heightmap terrain, trimesh colliders, wgpu renderer with instancing and depth, winit app with 1 kHz physics, fly camera, on-screen metrics overlay and per-frame CSV/dev logs. Docs: UI, logging, network and headless CLI plans.
193 lines
5.8 KiB
Rust
193 lines
5.8 KiB
Rust
//! Command-line and environment configuration for diagnostics.
|
|
//!
|
|
//! The parser is pure: arguments and environment values are passed in, so the
|
|
//! precedence rules are unit-tested without touching the process.
|
|
//!
|
|
//! Precedence for each path is command line, then environment, then the
|
|
//! `--dev` default. Logs are off unless a path or dev mode asks for them.
|
|
|
|
#[cfg(test)]
|
|
mod tests;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
/// Default metrics CSV written by `--dev` when no path is given.
|
|
pub(crate) const DEFAULT_METRICS: &str = "fabrilis-metrics.csv";
|
|
|
|
/// Default dev log written by `--dev` when no path is given.
|
|
pub(crate) const DEFAULT_LOG: &str = "fabrilis-dev.log";
|
|
|
|
/// Environment variable with the metrics CSV path.
|
|
const ENV_METRICS: &str = "FABRILIS_METRICS_LOG";
|
|
|
|
/// Environment variable with the dev log path.
|
|
const ENV_LOG: &str = "FABRILIS_LOG";
|
|
|
|
/// Environment variable that enables dev mode when set to `1`.
|
|
const ENV_DEV: &str = "FABRILIS_DEV";
|
|
|
|
/// Usage text printed for `--help` and after a parse error.
|
|
pub(crate) const USAGE: &str = "\
|
|
Usage: fabrilis-app [options]
|
|
|
|
Options:
|
|
--metrics-log <path> Write one metrics CSV row per rendered frame
|
|
--log <path> Write the dev log to <path>
|
|
--dev Verbose mode: default log paths and debug level
|
|
--append Append to existing logs instead of truncating
|
|
-h, --help Print this help and exit
|
|
|
|
Environment:
|
|
FABRILIS_METRICS_LOG Metrics CSV path (command line wins)
|
|
FABRILIS_LOG Dev log path (command line wins)
|
|
FABRILIS_DEV=1 Same as --dev
|
|
";
|
|
|
|
/// Resolved diagnostics configuration.
|
|
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
|
pub(crate) struct Config {
|
|
/// Metrics CSV path; `None` keeps the CSV off.
|
|
pub(crate) metrics_log: Option<PathBuf>,
|
|
/// Dev log path; `None` keeps the text log off.
|
|
pub(crate) log: Option<PathBuf>,
|
|
/// Verbose mode: enables the dev log and the debug level.
|
|
pub(crate) dev: bool,
|
|
/// Append to existing files instead of truncating them at start.
|
|
pub(crate) append: bool,
|
|
}
|
|
|
|
/// What the command line asked for.
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub(crate) enum Outcome {
|
|
/// Run the application with this configuration.
|
|
Run(Config),
|
|
/// Print the usage text and exit successfully.
|
|
Help,
|
|
}
|
|
|
|
/// A configuration the parser rejected.
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub(crate) enum ConfigError {
|
|
/// An argument is not one of the documented flags.
|
|
Unknown(String),
|
|
/// A flag that takes a path was the last argument.
|
|
MissingValue(&'static str),
|
|
}
|
|
|
|
impl core::fmt::Display for ConfigError {
|
|
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
|
match self {
|
|
Self::Unknown(argument) => write!(formatter, "unknown argument: {argument}"),
|
|
Self::MissingValue(flag) => write!(formatter, "missing value for {flag}"),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for ConfigError {}
|
|
|
|
/// Environment values that fill in paths the command line did not set.
|
|
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
|
pub(crate) struct Environment {
|
|
pub(crate) metrics_log: Option<PathBuf>,
|
|
pub(crate) log: Option<PathBuf>,
|
|
pub(crate) dev: bool,
|
|
}
|
|
|
|
impl Environment {
|
|
/// Reads the documented `FABRILIS_*` variables, ignoring unset ones.
|
|
pub(crate) fn from_process() -> Self {
|
|
Self {
|
|
metrics_log: path_variable(ENV_METRICS),
|
|
log: path_variable(ENV_LOG),
|
|
dev: std::env::var_os(ENV_DEV).is_some_and(|value| value.to_str() == Some("1")),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Config {
|
|
/// Parses the process arguments and environment.
|
|
pub(crate) fn from_process() -> Result<Outcome, ConfigError> {
|
|
let args: Vec<String> = std::env::args_os()
|
|
.skip(1)
|
|
.map(|argument| argument.to_string_lossy().into_owned())
|
|
.collect();
|
|
parse(&args, &Environment::from_process())
|
|
}
|
|
}
|
|
|
|
/// Parses arguments and resolves them against the environment.
|
|
pub(crate) fn parse(args: &[String], env: &Environment) -> Result<Outcome, ConfigError> {
|
|
let mut cli = Cli::default();
|
|
let mut arguments = args.iter();
|
|
while let Some(argument) = arguments.next() {
|
|
match argument.as_str() {
|
|
"--help" | "-h" => return Ok(Outcome::Help),
|
|
"--dev" => cli.dev = true,
|
|
"--append" => cli.append = true,
|
|
"--metrics-log" => cli.metrics_log = Some(path_value(&mut arguments, "--metrics-log")?),
|
|
"--log" => cli.log = Some(path_value(&mut arguments, "--log")?),
|
|
argument => inline(&mut cli, argument)?,
|
|
}
|
|
}
|
|
Ok(Outcome::Run(resolve(cli, env)))
|
|
}
|
|
|
|
/// Flags exactly as the command line gave them.
|
|
#[derive(Debug, Clone, Default)]
|
|
struct Cli {
|
|
metrics_log: Option<PathBuf>,
|
|
log: Option<PathBuf>,
|
|
dev: bool,
|
|
append: bool,
|
|
}
|
|
|
|
/// Handles the `--flag=value` spelling of a path flag.
|
|
fn inline(cli: &mut Cli, argument: &str) -> Result<(), ConfigError> {
|
|
if let Some(value) = argument.strip_prefix("--metrics-log=") {
|
|
cli.metrics_log = Some(PathBuf::from(value));
|
|
return Ok(());
|
|
}
|
|
if let Some(value) = argument.strip_prefix("--log=") {
|
|
cli.log = Some(PathBuf::from(value));
|
|
return Ok(());
|
|
}
|
|
Err(ConfigError::Unknown(argument.to_owned()))
|
|
}
|
|
|
|
/// The value after a path flag.
|
|
fn path_value(
|
|
arguments: &mut std::slice::Iter<'_, String>,
|
|
flag: &'static str,
|
|
) -> Result<PathBuf, ConfigError> {
|
|
arguments
|
|
.next()
|
|
.filter(|value| !value.is_empty())
|
|
.map(PathBuf::from)
|
|
.ok_or(ConfigError::MissingValue(flag))
|
|
}
|
|
|
|
/// Folds command line and environment into the final configuration.
|
|
fn resolve(cli: Cli, env: &Environment) -> Config {
|
|
let dev = cli.dev || env.dev;
|
|
let metrics_log = cli
|
|
.metrics_log
|
|
.or_else(|| env.metrics_log.clone())
|
|
.or_else(|| dev.then(|| PathBuf::from(DEFAULT_METRICS)));
|
|
let log = cli
|
|
.log
|
|
.or_else(|| env.log.clone())
|
|
.or_else(|| dev.then(|| PathBuf::from(DEFAULT_LOG)));
|
|
Config {
|
|
metrics_log,
|
|
log,
|
|
dev,
|
|
append: cli.append,
|
|
}
|
|
}
|
|
|
|
/// A path environment variable, treating an empty value as unset.
|
|
fn path_variable(name: &str) -> Option<PathBuf> {
|
|
std::env::var_os(name)
|
|
.map(PathBuf::from)
|
|
.filter(|path| !path.as_os_str().is_empty())
|
|
}
|