use std::sync::OnceLock;
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct ProfileFlags {
pub memory: bool,
pub cpu: bool,
pub cascade: bool,
pub heap: bool,
pub jsonl: bool,
pub detail: bool,
}
impl ProfileFlags {
fn parse(value: &str) -> Self {
let mut f = Self::default();
for tok in value.split(',') {
let t = tok.trim();
if t.eq_ignore_ascii_case("memory") || t.eq_ignore_ascii_case("mem") {
f.memory = true;
} else if t.eq_ignore_ascii_case("cpu") || t.eq_ignore_ascii_case("perf") {
f.cpu = true;
} else if t.eq_ignore_ascii_case("cascade") || t.eq_ignore_ascii_case("css") {
f.cascade = true;
} else if t.eq_ignore_ascii_case("heap") {
f.heap = true;
} else if t.eq_ignore_ascii_case("jsonl") {
f.jsonl = true;
} else if t.eq_ignore_ascii_case("detail") {
f.detail = true;
}
}
f
}
}
#[inline]
pub fn flags() -> ProfileFlags {
static FLAGS: OnceLock<ProfileFlags> = OnceLock::new();
*FLAGS.get_or_init(|| {
std::env::var("AZ_PROFILE")
.map(|v| ProfileFlags::parse(&v))
.unwrap_or_default()
})
}
#[inline]
pub fn out_path() -> Option<&'static str> {
static PATH: OnceLock<Option<String>> = OnceLock::new();
PATH.get_or_init(|| std::env::var("AZ_PROFILE_OUT").ok())
.as_deref()
}
#[inline]
pub fn memory_enabled() -> bool { flags().memory }
#[inline]
pub fn cpu_enabled() -> bool { flags().cpu }
#[inline]
pub fn cascade_enabled() -> bool { flags().cascade }
#[inline]
pub fn heap_enabled() -> bool { flags().heap }
#[inline]
pub fn jsonl_enabled() -> bool { flags().jsonl }
#[inline]
pub fn detail_enabled() -> bool { flags().detail }
#[cfg(test)]
mod tests {
use super::ProfileFlags;
#[test]
fn parse_single_token() {
let f = ProfileFlags::parse("cpu");
assert!(f.cpu && !f.memory && !f.heap);
}
#[test]
fn parse_multiple_tokens() {
let f = ProfileFlags::parse("heap,jsonl,detail");
assert!(f.heap && f.jsonl && f.detail);
assert!(!f.cpu && !f.memory);
}
#[test]
fn parse_is_case_insensitive_and_trims() {
let f = ProfileFlags::parse(" Heap , JSONL ");
assert!(f.heap && f.jsonl);
}
#[test]
fn parse_ignores_unknown_tokens() {
let f = ProfileFlags::parse("cpu,bogus,heap");
assert!(f.cpu && f.heap);
}
#[test]
fn parse_accepts_aliases() {
let f = ProfileFlags::parse("mem,perf,css");
assert!(f.memory && f.cpu && f.cascade);
}
}