use std::path::PathBuf;
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct ExecutionContext {
pub working_dir: PathBuf,
pub timeout: Duration,
pub max_output_lines: usize,
pub rg_path: String,
pub fd_path: String,
}
impl ExecutionContext {
pub fn new(working_dir: PathBuf) -> Self {
Self {
working_dir,
timeout: Duration::from_secs(30),
max_output_lines: 500,
rg_path: "rg".to_string(),
fd_path: "fd".to_string(),
}
}
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}
pub fn with_max_lines(mut self, max_lines: usize) -> Self {
self.max_output_lines = max_lines;
self
}
pub fn with_rg_path(mut self, path: impl Into<String>) -> Self {
self.rg_path = path.into();
self
}
pub fn with_fd_path(mut self, path: impl Into<String>) -> Self {
self.fd_path = path.into();
self
}
pub fn resolve_path(&self, path: &str) -> PathBuf {
let p = PathBuf::from(path);
if p.is_absolute() {
p
} else {
self.working_dir.join(p)
}
}
}
impl Default for ExecutionContext {
fn default() -> Self {
Self::new(PathBuf::from("."))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_context_creation() {
let ctx = ExecutionContext::new(PathBuf::from("/tmp"))
.with_timeout(Duration::from_secs(60))
.with_max_lines(1000);
assert_eq!(ctx.working_dir, PathBuf::from("/tmp"));
assert_eq!(ctx.timeout, Duration::from_secs(60));
assert_eq!(ctx.max_output_lines, 1000);
}
#[test]
fn test_resolve_path() {
let ctx = ExecutionContext::new(PathBuf::from("/home/user/project"));
assert_eq!(
ctx.resolve_path("src/lib.rs"),
PathBuf::from("/home/user/project/src/lib.rs")
);
assert_eq!(ctx.resolve_path("/etc/hosts"), PathBuf::from("/etc/hosts"));
}
}