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 {
env::var("CLM_ZERO_FS").unwrap_or_default() == "true"
}
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)> {
env::vars()
.filter(|(k, _)| k.starts_with("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);
}
}
use std::collections::HashMap;
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, Default)]
pub struct EvaluationContext {
clm_root: Option<PathBuf>,
nested_exec: bool,
zero_fs: bool,
env_vars: HashMap<String, String>,
}
impl EvaluationContext {
pub fn new() -> Self {
Self::default()
}
pub fn with_root(mut self, root: impl Into<PathBuf>) -> Self {
self.clm_root = Some(root.into());
self
}
pub fn with_nested_exec(mut self, nested: bool) -> Self {
self.nested_exec = nested;
self
}
pub fn with_zero_fs(mut self, zero_fs: bool) -> Self {
self.zero_fs = zero_fs;
self
}
pub fn root(&self) -> Option<&Path> {
self.clm_root.as_deref()
}
pub fn is_nested(&self) -> bool {
self.nested_exec
}
pub fn is_zero_fs(&self) -> bool {
self.zero_fs || is_zero_fs()
}
pub fn set_var(&mut self, key: impl Into<String>, value: impl Into<String>) {
self.env_vars.insert(key.into(), value.into());
}
pub fn get_var(&self, key: &str) -> Option<String> {
if let Some(val) = self.env_vars.get(key) {
return Some(val.clone());
}
env::var(key).ok()
}
pub fn remove_var(&mut self, key: &str) {
self.env_vars.remove(key);
}
}
#[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());
}
}