ryo_executor/executor/
context.rs1use std::path::PathBuf;
4use std::time::Duration;
5
6#[derive(Debug, Clone)]
8pub struct ExecutionContext {
9 pub working_dir: PathBuf,
11
12 pub timeout: Duration,
14
15 pub max_output_lines: usize,
17
18 pub rg_path: String,
20
21 pub fd_path: String,
23}
24
25impl ExecutionContext {
26 pub fn new(working_dir: PathBuf) -> Self {
28 Self {
29 working_dir,
30 timeout: Duration::from_secs(30),
31 max_output_lines: 500,
32 rg_path: "rg".to_string(),
33 fd_path: "fd".to_string(),
34 }
35 }
36
37 pub fn with_timeout(mut self, timeout: Duration) -> Self {
39 self.timeout = timeout;
40 self
41 }
42
43 pub fn with_max_lines(mut self, max_lines: usize) -> Self {
45 self.max_output_lines = max_lines;
46 self
47 }
48
49 pub fn with_rg_path(mut self, path: impl Into<String>) -> Self {
51 self.rg_path = path.into();
52 self
53 }
54
55 pub fn with_fd_path(mut self, path: impl Into<String>) -> Self {
57 self.fd_path = path.into();
58 self
59 }
60
61 pub fn resolve_path(&self, path: &str) -> PathBuf {
63 let p = PathBuf::from(path);
64 if p.is_absolute() {
65 p
66 } else {
67 self.working_dir.join(p)
68 }
69 }
70}
71
72impl Default for ExecutionContext {
73 fn default() -> Self {
74 Self::new(PathBuf::from("."))
75 }
76}
77
78#[cfg(test)]
79mod tests {
80 use super::*;
81
82 #[test]
83 fn test_context_creation() {
84 let ctx = ExecutionContext::new(PathBuf::from("/tmp"))
85 .with_timeout(Duration::from_secs(60))
86 .with_max_lines(1000);
87
88 assert_eq!(ctx.working_dir, PathBuf::from("/tmp"));
89 assert_eq!(ctx.timeout, Duration::from_secs(60));
90 assert_eq!(ctx.max_output_lines, 1000);
91 }
92
93 #[test]
94 fn test_resolve_path() {
95 let ctx = ExecutionContext::new(PathBuf::from("/home/user/project"));
96
97 assert_eq!(
99 ctx.resolve_path("src/lib.rs"),
100 PathBuf::from("/home/user/project/src/lib.rs")
101 );
102
103 assert_eq!(ctx.resolve_path("/etc/hosts"), PathBuf::from("/etc/hosts"));
105 }
106}