clankerdiff_core/models/
diff_document.rs1use super::{FileDiff, RepoPath};
2use crate::{DiffError, RepoPathError};
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
8pub struct DiffDocument {
9 pub repo_root: String,
10 pub files: Vec<FileDiff>,
11}
12
13impl DiffDocument {
14 #[must_use]
15 pub fn empty() -> Self {
16 Self {
17 repo_root: String::new(),
18 files: Vec::new(),
19 }
20 }
21
22 pub fn from_texts<'a, T, U>(files: U) -> Result<Self, DiffError>
24 where
25 T: TryInto<RepoPath>,
26 T::Error: Into<RepoPathError>,
27 U: IntoIterator<Item = (T, &'a str, &'a str)>,
28 {
29 let files = files
30 .into_iter()
31 .map(|(path, old, new)| FileDiff::from_texts(path, old, new))
32 .collect::<Result<_, _>>()?;
33 Ok(Self {
34 repo_root: String::new(),
35 files,
36 })
37 }
38
39 #[must_use]
40 pub fn file_index(&self, path: &RepoPath) -> Option<usize> {
41 self.files.iter().position(|file| &file.path == path)
42 }
43}