use similar::TextDiff;
#[derive(Debug, Clone, serde::Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct EditOperation {
pub old_text: String,
pub new_text: String,
}
pub fn apply_edits(content: &str, edits: &[EditOperation]) -> Result<String, String> {
let mut modified = content.to_owned();
for edit in edits {
if edit.old_text.is_empty() {
return Err("oldText must not be empty".into());
}
let mut matches = modified.match_indices(&edit.old_text);
let Some((start, _)) = matches.next() else {
return Err(format!("oldText not found:\n{}", edit.old_text));
};
if matches.next().is_some() {
return Err(format!(
"oldText has an ambiguous match:\n{}",
edit.old_text
));
}
modified.replace_range(start..start + edit.old_text.len(), &edit.new_text);
}
Ok(modified)
}
pub fn render_diff(original: &str, modified: &str) -> String {
let diff = TextDiff::from_lines(original, modified)
.unified_diff()
.context_radius(3)
.header("original", "modified")
.to_string();
let mut backticks = "```".to_string();
while diff.contains(&backticks) {
backticks.push('`');
}
format!("{backticks}diff\n{diff}{backticks}\n")
}
#[cfg(test)]
mod tests;