Skip to main content

ryo_executor/executor/
context.rs

1//! ExecutionContext: 実行時コンテキスト
2
3use std::path::PathBuf;
4use std::time::Duration;
5
6/// 実行コンテキスト
7#[derive(Debug, Clone)]
8pub struct ExecutionContext {
9    /// 作業ディレクトリ
10    pub working_dir: PathBuf,
11
12    /// タイムアウト
13    pub timeout: Duration,
14
15    /// 出力の最大行数
16    pub max_output_lines: usize,
17
18    /// ripgrep パス
19    pub rg_path: String,
20
21    /// fd パス
22    pub fd_path: String,
23}
24
25impl ExecutionContext {
26    /// 新しいコンテキストを作成
27    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    /// タイムアウトを設定
38    pub fn with_timeout(mut self, timeout: Duration) -> Self {
39        self.timeout = timeout;
40        self
41    }
42
43    /// 最大出力行数を設定
44    pub fn with_max_lines(mut self, max_lines: usize) -> Self {
45        self.max_output_lines = max_lines;
46        self
47    }
48
49    /// ripgrep パスを設定
50    pub fn with_rg_path(mut self, path: impl Into<String>) -> Self {
51        self.rg_path = path.into();
52        self
53    }
54
55    /// fd パスを設定
56    pub fn with_fd_path(mut self, path: impl Into<String>) -> Self {
57        self.fd_path = path.into();
58        self
59    }
60
61    /// パスを解決(相対パスを絶対パスに)
62    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        // 相対パス
98        assert_eq!(
99            ctx.resolve_path("src/lib.rs"),
100            PathBuf::from("/home/user/project/src/lib.rs")
101        );
102
103        // 絶対パス
104        assert_eq!(ctx.resolve_path("/etc/hosts"), PathBuf::from("/etc/hosts"));
105    }
106}