Skip to main content

harness_write/
types.rs

1use harness_core::{PermissionPolicy, ToolError};
2use serde::{Deserialize, Serialize};
3use std::sync::Arc;
4
5use crate::ledger::Ledger;
6
7#[derive(Clone)]
8pub struct WriteSessionConfig {
9    pub cwd: String,
10    pub permissions: PermissionPolicy,
11    pub ledger: Arc<dyn Ledger>,
12    pub max_file_size: Option<u64>,
13}
14
15impl WriteSessionConfig {
16    pub fn new(
17        cwd: impl Into<String>,
18        permissions: PermissionPolicy,
19        ledger: Arc<dyn Ledger>,
20    ) -> Self {
21        Self {
22            cwd: cwd.into(),
23            permissions,
24            ledger,
25            max_file_size: None,
26        }
27    }
28}
29
30impl std::fmt::Debug for WriteSessionConfig {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        f.debug_struct("WriteSessionConfig")
33            .field("cwd", &self.cwd)
34            .field("permissions", &self.permissions)
35            .finish()
36    }
37}
38
39#[derive(Debug, Clone, Serialize, Deserialize)]
40pub struct MatchLocation {
41    pub line: usize,
42    pub preview: String,
43    pub context_before: Vec<String>,
44    pub context_after: Vec<String>,
45}
46
47#[derive(Debug, Clone, Serialize, Deserialize)]
48pub struct FuzzyCandidate {
49    pub line: usize,
50    pub score: f64,
51    pub preview: String,
52    pub context_before: Vec<String>,
53    pub context_after: Vec<String>,
54}
55
56#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct WriteMeta {
58    pub path: String,
59    pub bytes_written: u64,
60    pub sha256: String,
61    pub mtime_ms: u64,
62    pub created: bool,
63    #[serde(default, skip_serializing_if = "Option::is_none")]
64    pub previous_sha256: Option<String>,
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct EditMeta {
69    pub path: String,
70    pub replacements: usize,
71    pub bytes_delta: i64,
72    pub sha256: String,
73    pub mtime_ms: u64,
74    pub previous_sha256: String,
75    #[serde(default, skip_serializing_if = "Option::is_none")]
76    pub warnings: Option<Vec<String>>,
77}
78
79#[derive(Debug, Clone, Serialize, Deserialize)]
80pub struct MultiEditMeta {
81    pub path: String,
82    pub edits_applied: usize,
83    pub total_replacements: usize,
84    pub bytes_delta: i64,
85    pub sha256: String,
86    pub mtime_ms: u64,
87    pub previous_sha256: String,
88    #[serde(default, skip_serializing_if = "Option::is_none")]
89    pub warnings: Option<Vec<String>>,
90}
91
92#[derive(Debug, Clone, Serialize, Deserialize)]
93pub struct PreviewMeta {
94    pub path: String,
95    pub would_write_bytes: u64,
96    pub bytes_delta: i64,
97    pub previous_sha256: String,
98}
99
100#[derive(Debug, Clone, Serialize, Deserialize)]
101#[serde(untagged)]
102pub enum AnyMeta {
103    Write(WriteMeta),
104    Edit(EditMeta),
105    MultiEdit(MultiEditMeta),
106}
107
108#[derive(Debug, Clone, Serialize, Deserialize)]
109pub struct TextWriteResult {
110    pub output: String,
111    pub meta: AnyMeta,
112}
113
114#[derive(Debug, Clone, Serialize, Deserialize)]
115pub struct PreviewResult {
116    pub output: String,
117    pub diff: String,
118    pub meta: PreviewMeta,
119}
120
121#[derive(Debug, Clone, Serialize, Deserialize)]
122pub struct ErrorResult {
123    pub error: ToolError,
124}
125
126#[derive(Debug, Clone, Serialize, Deserialize)]
127#[serde(tag = "kind", rename_all = "snake_case")]
128pub enum WriteResult {
129    #[serde(rename = "text")]
130    Text(TextWriteResult),
131    #[serde(rename = "error")]
132    Error(ErrorResult),
133}
134
135#[derive(Debug, Clone, Serialize, Deserialize)]
136#[serde(tag = "kind", rename_all = "snake_case")]
137pub enum EditResult {
138    #[serde(rename = "text")]
139    Text(TextWriteResult),
140    #[serde(rename = "preview")]
141    Preview(PreviewResult),
142    #[serde(rename = "error")]
143    Error(ErrorResult),
144}
145
146#[derive(Debug, Clone, Serialize, Deserialize)]
147#[serde(tag = "kind", rename_all = "snake_case")]
148pub enum MultiEditResult {
149    #[serde(rename = "text")]
150    Text(TextWriteResult),
151    #[serde(rename = "preview")]
152    Preview(PreviewResult),
153    #[serde(rename = "error")]
154    Error(ErrorResult),
155}