1use crate::hunks::{EditHunk, text_edit_hunk};
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub struct WriteFileOutcome {
5 pub path: String,
6 pub content: String,
7 pub hunks: Vec<EditHunk>,
8}
9
10pub fn write_file(
11 path: impl Into<String>,
12 previous: Option<&str>,
13 content: String,
14) -> WriteFileOutcome {
15 let path = path.into();
16 let hunks = previous
17 .map(|old| vec![text_edit_hunk(&path, old, &content, 0)])
18 .unwrap_or_default();
19 WriteFileOutcome {
20 path,
21 content,
22 hunks,
23 }
24}
25
26#[cfg(test)]
27mod tests {
28 use super::*;
29
30 #[test]
31 fn write_existing_file_produces_hunk() {
32 let outcome = write_file("a.txt", Some("old"), "new".to_string());
33 assert_eq!(outcome.hunks.len(), 1);
34 }
35}