forked from assert-rs/dir-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
125 lines (106 loc) · 3.12 KB
/
lib.rs
File metadata and controls
125 lines (106 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//! Determine if two directories have different contents.
//!
//! For now, only one function exists: are they different, or not? In the future,
//! more functionality to actually determine the difference may be added.
//!
//! # Examples
//!
//! ```no_run
//! extern crate dir_diff;
//!
//! assert!(dir_diff::is_different("dir/a", "dir/b").unwrap());
//! ```
#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(clippy::print_stderr)]
#![warn(clippy::print_stdout)]
#[doc = include_str!("../README.md")]
#[cfg(doctest)]
pub struct ReadmeDoctests;
use std::cmp::Ordering;
use std::path::Path;
use walkdir::{DirEntry, WalkDir};
/// The various errors that can happen when diffing two directories
#[allow(clippy::exhaustive_enums)] // breaking change
#[allow(missing_docs)]
#[derive(Debug)]
pub enum Error {
Io(std::io::Error),
StripPrefix(std::path::StripPrefixError),
WalkDir(walkdir::Error),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Io(inner) => write!(f, "I/O error: {inner}"),
Error::StripPrefix(inner) => write!(f, "Strip prefix error: {inner}"),
Error::WalkDir(inner) => write!(f, "Walk dir error: {inner}"),
}
}
}
impl std::error::Error for Error {}
/// Are the contents of two directories different?
///
/// # Examples
///
/// ```no_run
/// extern crate dir_diff;
///
/// assert!(dir_diff::is_different("dir/a", "dir/b").unwrap());
/// ```
pub fn is_different<A: AsRef<Path>, B: AsRef<Path>>(a_base: A, b_base: B) -> Result<bool, Error> {
let mut a_walker = walk_dir(a_base)?;
let mut b_walker = walk_dir(b_base)?;
for (a, b) in (&mut a_walker).zip(&mut b_walker) {
let a = a?;
let b = b?;
if a.depth() != b.depth()
|| a.file_type() != b.file_type()
|| a.file_name() != b.file_name()
|| (a.file_type().is_file() && std::fs::read(a.path())? != std::fs::read(b.path())?)
{
return Ok(true);
}
}
Ok(a_walker.next().is_some() || b_walker.next().is_some())
}
fn walk_dir<P: AsRef<Path>>(path: P) -> Result<walkdir::IntoIter, std::io::Error> {
let mut walkdir = WalkDir::new(path).sort_by(compare_by_file_name).into_iter();
if let Some(Err(e)) = walkdir.next() {
Err(e.into())
} else {
Ok(walkdir)
}
}
fn compare_by_file_name(a: &DirEntry, b: &DirEntry) -> Ordering {
a.file_name().cmp(b.file_name())
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Error {
Error::Io(e)
}
}
impl From<std::path::StripPrefixError> for Error {
fn from(e: std::path::StripPrefixError) -> Error {
Error::StripPrefix(e)
}
}
impl From<walkdir::Error> for Error {
fn from(e: walkdir::Error) -> Error {
Error::WalkDir(e)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_display() {
use std::io::ErrorKind;
assert_eq!(
format!(
"{}",
Error::Io(std::io::Error::new(ErrorKind::Other, "oh no!"))
),
"I/O error: oh no!"
);
}
}