clnrm_core/
utils.rs

1//! Utility functions and helpers for the cleanroom framework
2//!
3//! Contains common utilities used throughout the framework.
4
5use crate::error::Result;
6
7/// Validate a file path exists and is readable
8pub fn validate_file_path(path: &str) -> Result<()> {
9    use std::path::Path;
10
11    let path_obj = Path::new(path);
12
13    // Check if path exists
14    if !path_obj.exists() {
15        return Err(crate::error::CleanroomError::validation_error(format!(
16            "Path does not exist: {}",
17            path
18        )));
19    }
20
21    // Check if path is readable (file) or accessible (directory)
22    if path_obj.is_file() {
23        // For files, check if we can read metadata
24        std::fs::metadata(path_obj)?;
25    } else if path_obj.is_dir() {
26        // For directories, check if we can read the directory
27        std::fs::read_dir(path_obj)?;
28    } else {
29        return Err(crate::error::CleanroomError::validation_error(format!(
30            "Path is neither a file nor directory: {}",
31            path
32        )));
33    }
34
35    Ok(())
36}
37
38/// Parse a TOML configuration file
39pub fn parse_toml_config(content: &str) -> Result<serde_json::Value> {
40    // Parse TOML content
41    let toml_value: toml::Value = toml::from_str(content).map_err(|e| {
42        crate::error::CleanroomError::validation_error(format!("Invalid TOML syntax: {}", e))
43    })?;
44
45    // Convert TOML to JSON for consistent handling
46    let json_value = serde_json::to_value(toml_value).map_err(|e| {
47        crate::error::CleanroomError::internal_error(format!(
48            "Failed to convert TOML to JSON: {}",
49            e
50        ))
51    })?;
52
53    Ok(json_value)
54}
55
56/// Generate a unique session ID
57pub fn generate_session_id() -> String {
58    format!("session_{}", uuid::Uuid::new_v4())
59}
60
61/// Format duration for display
62pub fn format_duration(duration: std::time::Duration) -> String {
63    if duration.as_secs() > 0 {
64        format!("{:.2}s", duration.as_secs_f64())
65    } else if duration.as_millis() > 0 {
66        format!("{}ms", duration.as_millis())
67    } else {
68        format!("{}μs", duration.as_micros())
69    }
70}
71
72/// Validate regex pattern
73pub fn validate_regex(pattern: &str) -> Result<()> {
74    use regex::Regex;
75
76    // Try to compile the regex pattern
77    Regex::new(pattern).map_err(|e| {
78        crate::error::CleanroomError::validation_error(format!(
79            "Invalid regex pattern '{}': {}",
80            pattern, e
81        ))
82    })?;
83
84    Ok(())
85}
86
87/// Execute regex pattern matching
88pub fn execute_regex_match(text: &str, pattern: &str) -> Result<bool> {
89    use regex::Regex;
90
91    // Compile and execute regex
92    let regex = Regex::new(pattern).map_err(|e| {
93        crate::error::CleanroomError::validation_error(format!(
94            "Invalid regex pattern '{}': {}",
95            pattern, e
96        ))
97    })?;
98
99    Ok(regex.is_match(text))
100}