use argenv_resolve::*;
use std::path::PathBuf;
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize)]
#[serde(into = "f32")]
pub struct RenderScale(u16);
impl RenderScale {
pub const FULL: RenderScale = RenderScale::from_permille(1000);
pub const fn from_permille(p: u16) -> RenderScale {
assert!(
p >= 100 && p <= 1000,
"render scale must be within 0.1..=1.0"
);
RenderScale(p)
}
pub fn as_f32(self) -> f32 {
self.0 as f32 / 1000.0
}
}
impl From<RenderScale> for f32 {
fn from(r: RenderScale) -> f32 {
r.as_f32()
}
}
impl FromRaw for RenderScale {
fn from_raw(s: &str) -> Option<RenderScale> {
let v: f32 = s.trim().parse().ok()?;
if !(0.1..=1.0).contains(&v) {
return None;
}
Some(RenderScale((v * 1000.0).round() as u16))
}
}
fn hud_replacement() -> &'static str {
Contract::HUD.key
}
contract! {
LOG_LEVEL: LogLevel = Input {
key: "log_level",
ty: Type::Enum,
default: Some(LogLevel::Info),
allowed: LogLevel::TOKENS,
env: Some(Env::new("MYAPP_LOG_LEVEL")),
arg: Some(Arg { value_name: "LEVEL", ..Arg::pair("log-level", 'l') }),
stability: Stability::Stable,
since: Since::This,
group: "logging",
example: "--log-level warn",
reviewed: Some(ReviewDate::parse("2026-07-21")),
summary: "Log verbosity",
..Input::EMPTY
};
HDR: bool = Input {
key: "hdr",
ty: Type::Bool,
default: Some(false),
env: Some(Env::new("MYAPP_HDR")),
arg: Some(Arg { negatable: true, ..Arg::long("hdr") }),
stability: Stability::Stable,
since: Since::This,
group: "display",
maps_to: Some(ConfigKeyRef::new("display.enableHDR")),
example: "--hdr",
observe: Some("hdr_enabled"),
reviewed: Some(ReviewDate::parse("2026-07-21")),
summary: "Expose HDR output to the application",
..Input::EMPTY
};
HUD: String = Input {
key: "hud",
ty: Type::List,
allowed: &["fps", "frametimes", "gpuload", "memory", "version"],
separators: &[',', ';'],
env: Some(Env::new("MYAPP_HUD")),
arg: Some(Arg { repeatable: true, value_name: "ELEMENT", ..Arg::long("hud") }),
stability: Stability::Stable,
since: Since::This,
group: "display",
example: "--hud fps --hud gpuload",
reviewed: Some(ReviewDate::parse("2026-07-21")),
summary: "Overlay elements to display",
..Input::EMPTY
};
LOG_PATH: PathBuf = Input {
key: "log_path",
ty: Type::Path,
env: Some(Env { name: "MYAPP_LOG_PATH", aliases: &["MYAPP_LOGFILE"] }),
group: "logging",
..Input::EMPTY
};
RENDER_SCALE: RenderScale = Input {
key: "render_scale",
ty: Type::Float,
default: Some(RenderScale::FULL),
arg: Some(Arg { value_name: "FACTOR", ..Arg::long("render-scale") }),
stability: Stability::Stable,
since: Since::This,
group: "display",
example: "--render-scale 0.75",
reviewed: Some(ReviewDate::parse("2026-07-21")),
summary: "Internal render scale before upscaling",
..Input::EMPTY
};
ACCEL: Tristate = Input {
key: "accel",
ty: Type::Enum,
default: Some(Tristate::Auto),
allowed: Tristate::TOKENS,
env: Some(Env::new("MYAPP_ACCEL")),
arg: Some(Arg { value_name: "MODE", ..Arg::long("accel") }),
stability: Stability::Stable,
since: Since::This,
group: "display",
example: "--accel auto",
reviewed: Some(ReviewDate::parse("2026-07-21")),
summary: "Hardware acceleration; auto enables it when available",
..Input::EMPTY
};
PROFILE: bool = Input {
key: "profile",
ty: Type::Bool,
default: Some(false),
env: Some(Env::new("MYAPP_PROFILE")),
arg: Some(Arg::long("profile")),
stability: Stability::Deprecated,
since: Since::This,
deprecation: Some(Deprecation {
since: THIS_VERSION,
replaced_by: Some(hud_replacement),
migration: "use --hud gpuload,frametimes instead",
}),
group: "display",
reviewed: Some(ReviewDate::parse("2026-07-21")),
summary: "Deprecated profiling overlay",
..Input::EMPTY
};
}
fn main() {
let resolved = Contract::parse_and_lint(OnProblems::FailOnError);
let level = Contract::LOG_LEVEL.get_from_or_default(&resolved);
let hdr = Contract::HDR.get_from_or_default(&resolved);
eprintln!(
"log_level = {level:?} (from {:?}), hdr = {hdr:?} (from {:?})",
resolved.source("log_level"),
resolved.source("hdr")
);
println!(
"{}",
serde_json::to_string_pretty(&document("myapp@a1b2c3d", &Contract::records())).unwrap()
);
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::BTreeMap;
#[test]
fn the_model_satisfies_every_rule() {
assert!(
Contract::problems().is_empty(),
"{:#?}",
Contract::problems()
);
}
#[test]
fn an_argument_beats_an_environment_variable() {
let env: BTreeMap<String, String> =
[("MYAPP_LOG_LEVEL".to_string(), "error".to_string())].into();
let args = vec!["--log-level".to_string(), "warn".to_string()];
let r = Invocation {
args: &args,
env: &env,
}
.resolve(&Contract::records());
assert_eq!(Contract::LOG_LEVEL.get_from(&r), Some(LogLevel::Warn));
assert_eq!(r.source("log_level"), Some(Source::Arg));
}
}