Skip to content

Commit fc02ccd

Browse files
committed
add diff wrapper
1 parent 0d4f462 commit fc02ccd

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

check_diff/src/lib.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use diffy;
1+
use diffy::{self};
22
use std::env;
3-
use std::fmt::Debug;
3+
use std::fmt::{Debug, Display};
44
use std::io::{self, Write};
55
use std::path::{Path, PathBuf};
66
use std::process::{Command, Stdio};
@@ -61,6 +61,25 @@ impl From<io::Error> for GitError {
6161
}
6262
}
6363

64+
pub struct Diff {
65+
src_format: String,
66+
feature_format: String,
67+
}
68+
69+
impl Display for Diff {
70+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71+
let patch = diffy::create_patch(self.src_format.as_str(), self.feature_format.as_str());
72+
write!(f, "{}", patch)
73+
}
74+
}
75+
76+
impl Diff {
77+
pub fn is_empty(&self) -> bool {
78+
let patch = diffy::create_patch(self.src_format.as_str(), self.feature_format.as_str());
79+
patch.hunks().is_empty()
80+
}
81+
}
82+
6483
// will be used in future PRs, just added to make the compiler happy
6584
pub struct CheckDiffRunners {
6685
pub feature_runner: RustfmtRunner,
@@ -78,16 +97,14 @@ impl CheckDiffRunners {
7897
&self,
7998
path: &Path,
8099
additional_configs: &Option<Vec<String>>,
81-
) -> Result<String, CheckDiffError> {
100+
) -> Result<Diff, CheckDiffError> {
82101
let code = std::fs::read_to_string(path)?;
83102
let src_format = self.src_runner.format_code(&code, additional_configs)?;
84103
let feature_format = self.feature_runner.format_code(&code, additional_configs)?;
85-
let diff = diffy::create_patch(src_format.as_str(), feature_format.as_str());
86-
if diff.hunks().is_empty() {
87-
Ok(String::new())
88-
} else {
89-
Ok(format!("{diff}"))
90-
}
104+
Ok(Diff {
105+
src_format,
106+
feature_format,
107+
})
91108
}
92109
}
93110

0 commit comments

Comments
 (0)