use std::env;
pub fn now_iso8601() -> String {
chrono::Local::now()
.format("%Y-%m-%dT%H:%M:%S%.6f%z")
.to_string()
}
pub fn is_zero_fs() -> bool {
crate::layer0::environment::var(crate::layer0::vocabulary_env::ENV_CLM_ZERO_FS).unwrap_or_default() == "true"
}
pub fn assert_within(measured: std::time::Duration, bound: std::time::Duration, what: &str) {
if measured < bound {
return;
}
let message = format!("{what}: {measured:?}, bound < {bound:?}");
if crate::layer0::environment::var(crate::layer0::vocabulary_env::ENV_CLM_SKIP_TIMING_ASSERTIONS).as_deref() == Ok("1") {
println!("[timing not asserted under CLM_SKIP_TIMING_ASSERTIONS=1] {message}");
} else {
panic!("{message}");
}
}
pub fn indent_lines(s: &str, spaces: usize) -> String {
let indent = " ".repeat(spaces);
let mut result = String::new();
for line in s.lines() {
if line.trim().is_empty() {
result.push('\n');
} else {
result.push_str(&indent);
result.push_str(line);
result.push('\n');
}
}
result
}
pub fn collect_clm_mock_vars() -> Vec<(String, String)> {
crate::layer0::environment::vars()
.filter(|(k, _)| k.starts_with(crate::layer0::vocabulary_env::ENV_CLM_MOCK))
.collect()
}
pub fn capture_process_output(output: &std::process::Output) -> (String, String) {
(
String::from_utf8_lossy(&output.stdout).into_owned(),
String::from_utf8_lossy(&output.stderr).into_owned(),
)
}
pub fn set_env_var(key: &str, value: &str) {
unsafe {
env::set_var(key, value);
}
}
pub fn remove_env_var(key: &str) {
unsafe {
env::remove_var(key);
}
}
pub type EvaluationContext = crate::cross_cutting::context::CLMContext;
pub use crate::cross_cutting::context::CLMContext;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_evaluation_context_isolation() {
let mut ctx1 = EvaluationContext::new().with_nested_exec(true);
ctx1.set_var("LOCAL_FLAG", "value1");
let mut ctx2 = EvaluationContext::new().with_nested_exec(false);
ctx2.set_var("LOCAL_FLAG", "value2");
assert_eq!(ctx1.get_var("LOCAL_FLAG"), Some("value1".to_string()));
assert_eq!(ctx2.get_var("LOCAL_FLAG"), Some("value2".to_string()));
assert!(ctx1.is_nested());
assert!(!ctx2.is_nested());
}
}