Skip to main content

harness_grep/
types.rs

1use harness_core::{PermissionPolicy, ToolError};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
5#[serde(rename_all = "snake_case")]
6pub enum GrepOutputMode {
7    FilesWithMatches,
8    Content,
9    Count,
10}
11
12impl Default for GrepOutputMode {
13    fn default() -> Self {
14        Self::FilesWithMatches
15    }
16}
17
18#[derive(Debug, Clone)]
19pub struct GrepSessionConfig {
20    pub cwd: String,
21    pub permissions: PermissionPolicy,
22    pub default_head_limit: Option<usize>,
23    pub max_bytes: Option<usize>,
24    pub max_line_length: Option<usize>,
25    pub max_filesize: Option<u64>,
26    pub timeout_ms: Option<u64>,
27}
28
29impl GrepSessionConfig {
30    pub fn new(cwd: impl Into<String>, permissions: PermissionPolicy) -> Self {
31        Self {
32            cwd: cwd.into(),
33            permissions,
34            default_head_limit: None,
35            max_bytes: None,
36            max_line_length: None,
37            max_filesize: None,
38            timeout_ms: None,
39        }
40    }
41}
42
43/// One matching line from the engine. Mirrors TS `RgMatch`.
44#[derive(Debug, Clone)]
45pub struct RgMatch {
46    pub path: String,
47    pub line_number: u64,
48    pub text: String,
49    pub is_context: bool,
50}
51
52/// Per-file count from the engine. Mirrors TS `RgCount`.
53#[derive(Debug, Clone, Serialize, Deserialize)]
54pub struct RgCount {
55    pub path: String,
56    pub count: u64,
57}
58
59// ---- Result union ----
60
61#[derive(Debug, Clone, Serialize, Deserialize)]
62#[serde(tag = "kind", rename_all = "snake_case")]
63pub enum GrepResult {
64    #[serde(rename = "files_with_matches")]
65    FilesWithMatches(FilesMatchResult),
66    #[serde(rename = "content")]
67    Content(ContentResult),
68    #[serde(rename = "count")]
69    Count(CountResult),
70    #[serde(rename = "error")]
71    Error(ErrorResult),
72}
73
74#[derive(Debug, Clone, Serialize, Deserialize)]
75pub struct FilesMatchResult {
76    pub output: String,
77    pub paths: Vec<String>,
78    pub meta: FilesMatchMeta,
79}
80
81#[derive(Debug, Clone, Serialize, Deserialize)]
82pub struct FilesMatchMeta {
83    pub pattern: String,
84    pub total: usize,
85    pub returned: usize,
86    pub offset: usize,
87    pub head_limit: usize,
88    pub more: bool,
89}
90
91#[derive(Debug, Clone, Serialize, Deserialize)]
92pub struct ContentResult {
93    pub output: String,
94    pub meta: ContentMeta,
95}
96
97#[derive(Debug, Clone, Serialize, Deserialize)]
98pub struct ContentMeta {
99    pub pattern: String,
100    pub total_matches: usize,
101    pub total_files: usize,
102    pub returned_matches: usize,
103    pub offset: usize,
104    pub head_limit: usize,
105    pub more: bool,
106    pub byte_cap: bool,
107}
108
109#[derive(Debug, Clone, Serialize, Deserialize)]
110pub struct CountResult {
111    pub output: String,
112    pub counts: Vec<RgCount>,
113    pub meta: CountMeta,
114}
115
116#[derive(Debug, Clone, Serialize, Deserialize)]
117pub struct CountMeta {
118    pub pattern: String,
119    pub total_files: usize,
120    pub returned_files: usize,
121    pub offset: usize,
122    pub head_limit: usize,
123    pub more: bool,
124}
125
126#[derive(Debug, Clone, Serialize, Deserialize)]
127pub struct ErrorResult {
128    pub error: ToolError,
129}