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
//!
//! # Spielrs Diff
//! It is a library which compare two tree direcories asynchronously through [tokio](https://tokio.rs)
//! and return true in case that both are different. Useful to create watchers in the servers
//!
//! ## How install it
//! 1. add the dependency in the Cargo.toml file of the project:
//!
//! ```toml
//! spielrs_diff = "0.1"
//! ```
//!
//! ## Example
//! ```rust
//! use spielrs_diff::dir_diff;
//!
//! #[tokio::test]
//! async fn should_return_true_if_both_dir_tree_are_different() {
//!    let diff = dir_diff(
//!        "./mocks/dir_one".to_string(),
//!        "./mocks/dir_three".to_string(),
//!    )
//!    .await;
//!
//!    assert_eq!(diff, true);
//! }
//! ```
//!
//!
pub mod tree;

use tree::{Tree, TreeBuilder};

/// Compare two tree directories and return true if both are different
///
/// # Example
/// ```rust
/// use spielrs_diff::dir_diff;
///
/// #[tokio::test]
/// async fn should_return_true_if_both_dir_tree_are_different() {
///    let diff = dir_diff(
///        "./mocks/dir_one".to_string(),
///        "./mocks/dir_three".to_string(),
///    )
///    .await;
///
///    assert_eq!(diff, true);
/// }
/// ```
///
pub async fn dir_diff(dir_path: String, dir_path_comp: String) -> bool {
    let tree_one: Vec<Tree> = Tree::build_tree(dir_path).await;
    let tree_two: Vec<Tree> = Tree::build_tree(dir_path_comp).await;
    if Tree::tree_diff(tree_one.clone(), tree_two.clone()) {
        return true;
    }

    let content_one: Vec<String> = Tree::get_content_files(tree_one).await;
    let content_two: Vec<String> = Tree::get_content_files(tree_two).await;
    !Tree::compare_dir_content(content_one, content_two)
}

#[tokio::test]
async fn should_return_true_if_both_dir_tree_are_different() {
    let diff = dir_diff(
        "./mocks/dir_one".to_string(),
        "./mocks/dir_three".to_string(),
    )
    .await;
    assert_eq!(diff, true);
}

#[tokio::test]
async fn should_return_false_if_both_dir_tree_are_equal() {
    let diff = dir_diff("./mocks/dir_one".to_string(), "./mocks/dir_two".to_string()).await;
    assert_eq!(diff, false);
}

#[tokio::test]
async fn should_return_true_if_both_dir_tree_have_different_content() {
    let diff = dir_diff(
        "./mocks/dir_one".to_string(),
        "./mocks/dir_four".to_string(),
    )
    .await;
    assert_eq!(diff, true);
}