use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WorkspacePatchSuccess {
pub ok: bool,
pub tool: String,
pub changes: Vec<WorkspacePatchSuccessChange>,
}
impl WorkspacePatchSuccess {
pub(crate) fn model_text(&self) -> String {
let mut text = "Applied patch:".to_owned();
for change in &self.changes {
let operation = match change.operation() {
WorkspacePatchOperationKind::Add => "added",
WorkspacePatchOperationKind::Update => "updated",
WorkspacePatchOperationKind::Delete => "deleted",
WorkspacePatchOperationKind::Unknown => "changed",
};
text.push_str(&format!("\n{operation} {}", change.path));
if change.ignored_context_hunks > 0 {
text.push_str(&format!(
" (ignored {} context-only hunks)",
change.ignored_context_hunks
));
}
}
text
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WorkspacePatchSuccessChange {
pub path: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub op: Option<WorkspacePatchOperationKind>,
pub hunks: usize,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub lines_before: Option<usize>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub lines_after: Option<usize>,
pub bytes_before: usize,
pub bytes_after: usize,
#[serde(default, skip_serializing_if = "is_zero")]
pub ignored_context_hunks: usize,
#[serde(default)]
pub lines: Vec<WorkspacePatchSuccessLine>,
}
impl WorkspacePatchSuccessChange {
#[must_use]
pub fn operation(&self) -> WorkspacePatchOperationKind {
self.op.unwrap_or(WorkspacePatchOperationKind::Update)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WorkspacePatchOperationKind {
Add,
Update,
Delete,
#[serde(other)]
Unknown,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WorkspacePatchSuccessLine {
pub kind: WorkspacePatchSuccessLineKind,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub old_line: Option<usize>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub new_line: Option<usize>,
pub text: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WorkspacePatchSuccessLineKind {
Context,
Remove,
Add,
#[serde(other)]
Unknown,
}
fn is_zero(value: &usize) -> bool {
*value == 0
}
#[cfg(test)]
mod tests {
use super::{
WorkspacePatchOperationKind, WorkspacePatchSuccess, WorkspacePatchSuccessChange,
WorkspacePatchSuccessLine, WorkspacePatchSuccessLineKind,
};
#[test]
fn success_envelope_round_trips_every_field() {
let envelope = WorkspacePatchSuccess {
ok: true,
tool: "apply_patch".to_owned(),
changes: vec![WorkspacePatchSuccessChange {
path: "dir/note.txt".to_owned(),
op: Some(WorkspacePatchOperationKind::Update),
hunks: 1,
lines_before: Some(3),
lines_after: Some(3),
bytes_before: 22,
bytes_after: 24,
ignored_context_hunks: 2,
lines: vec![WorkspacePatchSuccessLine {
kind: WorkspacePatchSuccessLineKind::Add,
old_line: None,
new_line: Some(2),
text: "newer".to_owned(),
}],
}],
};
let json = serde_json::to_string(&envelope).expect("envelope should serialize");
let restored: WorkspacePatchSuccess =
serde_json::from_str(&json).expect("serialized envelope should deserialize");
assert_eq!(restored, envelope);
assert_eq!(
restored.changes[0].operation(),
WorkspacePatchOperationKind::Update
);
}
#[test]
fn success_envelope_reads_a_legacy_entry_as_an_update() {
let json = r#"{"ok":true,"tool":"apply_patch","changes":[
{"path":"hello.txt","hunks":1,"bytes_before":0,"bytes_after":12}
]}"#;
let restored: WorkspacePatchSuccess =
serde_json::from_str(json).expect("legacy envelope should deserialize");
let change = &restored.changes[0];
assert_eq!(change.operation(), WorkspacePatchOperationKind::Update);
assert_eq!(change.lines_before, None);
assert_eq!(change.lines_after, None);
assert!(change.lines.is_empty());
}
#[test]
fn success_envelope_keeps_an_unknown_line_kind_readable() {
let json = r#"{"ok":true,"tool":"apply_patch","changes":[{"path":"note.txt","hunks":1,"bytes_before":1,"bytes_after":2,
"lines":[{"kind":"something-new","text":"line"}]}]}"#;
let restored: WorkspacePatchSuccess =
serde_json::from_str(json).expect("future line kinds should not fail the result");
assert_eq!(
restored.changes[0].lines[0].kind,
WorkspacePatchSuccessLineKind::Unknown
);
}
#[test]
fn success_envelope_keeps_an_unknown_operation_readable() {
let json = r#"{"ok":true,"tool":"apply_patch","changes":[{"path":"note.txt","op":"something-new","hunks":1,"bytes_before":1,"bytes_after":2}]}"#;
let restored: WorkspacePatchSuccess =
serde_json::from_str(json).expect("future operations should not fail the result");
assert_eq!(
restored.changes[0].operation(),
WorkspacePatchOperationKind::Unknown
);
}
#[test]
fn success_envelope_omits_absent_optional_fields_when_serializing() {
let envelope = WorkspacePatchSuccess {
ok: true,
tool: "apply_patch".to_owned(),
changes: vec![WorkspacePatchSuccessChange {
path: "gone.txt".to_owned(),
op: Some(WorkspacePatchOperationKind::Delete),
hunks: 0,
lines_before: Some(4),
lines_after: Some(0),
bytes_before: 30,
bytes_after: 0,
ignored_context_hunks: 0,
lines: Vec::new(),
}],
};
let value = serde_json::to_value(&envelope).expect("envelope should serialize");
assert_eq!(
value,
serde_json::json!({
"ok": true,
"tool": "apply_patch",
"changes": [{
"path": "gone.txt",
"op": "delete",
"hunks": 0,
"lines_before": 4,
"lines_after": 0,
"bytes_before": 30,
"bytes_after": 0,
"lines": []
}]
}),
"the wire shape must stay stable for existing consumers"
);
}
}