1use crate::error::Result;
6
7pub fn validate_file_path(path: &str) -> Result<()> {
9 use std::path::Path;
10
11 let path_obj = Path::new(path);
12
13 if !path_obj.exists() {
15 return Err(crate::error::CleanroomError::validation_error(format!(
16 "Path does not exist: {}",
17 path
18 )));
19 }
20
21 if path_obj.is_file() {
23 std::fs::metadata(path_obj)?;
25 } else if path_obj.is_dir() {
26 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
38pub fn parse_toml_config(content: &str) -> Result<serde_json::Value> {
40 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 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
56pub fn generate_session_id() -> String {
58 format!("session_{}", uuid::Uuid::new_v4())
59}
60
61pub 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
72pub fn validate_regex(pattern: &str) -> Result<()> {
74 use regex::Regex;
75
76 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
87pub fn execute_regex_match(text: &str, pattern: &str) -> Result<bool> {
89 use regex::Regex;
90
91 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}