From 72a136cfef2d623067a0415bbdade11e463ed74b Mon Sep 17 00:00:00 2001 From: spieljs Date: Sun, 17 May 2020 12:18:25 +0200 Subject: [PATCH] implement diff two files --- Cargo.toml | 2 +- README.md | 22 +++- .../purpose/{feature.txt => purpose.txt} | 0 src/diff.rs | 9 +- src/lib.rs | 103 ++++++++++++++---- 5 files changed, 113 insertions(+), 23 deletions(-) rename mocks/dir_five/vlang/purpose/{feature.txt => purpose.txt} (100%) diff --git a/Cargo.toml b/Cargo.toml index 890a5c4..21006d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "spielrs_diff" -description= "It is a library which compare two tree direcories asynchronously" +description= "It is a library which compare two tree direcories or two files asynchronously" version = "0.2.0" documentation = "https://docs.rs/crate/spielrs_diff/0.2.0" authors = ["Francisco Jesus Navarro Cortes "] diff --git a/README.md b/README.md index 3d62e00..d9a1b4d 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,17 @@ # Spielrs Diff -It is a library which compare two tree direcories asynchronously through [tokio](https://tokio.rs) +It is a library which compare two tree direcories or two files 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" +spielrs_diff = "0.2" ``` ## Example + +### Dir comparation + ```rust use spielrs_diff::dir_diff; #[tokio::test] @@ -22,6 +25,21 @@ async fn should_return_true_if_both_dir_tree_are_different() { } ``` +### File comparation + +```rust +use spielrs_diff::{file_diff, diff::FileDiff}; +#[tokio::test] +async fn should_return_true_if_both_files_are_not_equal() { + let diff = file_diff(FileDiff { + file: "./mocks/dir_one/vlang/purpose/purpose.txt".to_string(), + file_comp: "./mocks/dir_five/vlang/purpose/purpose.txt".to_string(), + }) + .await; + assert_eq!(diff, true); +} +``` + ## License Spielrs Diff is MIT licensed. See [license](LICENSE) diff --git a/mocks/dir_five/vlang/purpose/feature.txt b/mocks/dir_five/vlang/purpose/purpose.txt similarity index 100% rename from mocks/dir_five/vlang/purpose/feature.txt rename to mocks/dir_five/vlang/purpose/purpose.txt diff --git a/src/diff.rs b/src/diff.rs index 2f0e8b6..6d062a0 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -1,4 +1,4 @@ -pub struct Diff { +pub struct DirDiff { /// directory to compare pub dir: String, /// comparation directory @@ -8,3 +8,10 @@ pub struct Diff { /// exclude recursively or only the from the root path pub recursive_excluding: bool, } + +pub struct FileDiff { + /// file to compare + pub file: String, + /// comparation file + pub file_comp: String, +} diff --git a/src/lib.rs b/src/lib.rs index 8a19f76..ec98796 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,22 +1,24 @@ //! //! # Spielrs Diff -//! It is a library which compare two tree direcories asynchronously through [tokio](https://tokio.rs) +//! It is a library which compare two tree direcories or two files 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" +//! spielrs_diff = "0.2" //! ``` //! //! ## Example +//! +//! ### Dir comparation //! ```rust -//! use spielrs_diff::{dir_diff, diff::Diff}; +//! use spielrs_diff::{dir_diff, diff::DirDiff}; //! //! #[tokio::test] //! async fn should_return_true_if_both_dir_tree_are_different() { -//! let diff = dir_diff(Diff { +//! let diff = dir_diff(DirDiff { //! dir: "./mocks/dir_one".to_string(), //! dir_comp: "./mocks/dir_five".to_string(), //! excluding: Some(vec!["purpose".to_string()]), @@ -28,11 +30,26 @@ //! } //! ``` //! +//! ### File comparation +//! ```rust +//! use spielrs_diff::{file_diff, diff::FileDiff}; +//! +//! #[tokio::test] +//! async fn should_return_true_if_both_files_are_not_equal() { +//! let diff = file_diff(FileDiff { +//! file: "./mocks/dir_one/vlang/purpose/purpose.txt".to_string(), +//! file_comp: "./mocks/dir_five/vlang/purpose/purpose.txt".to_string(), +//! }) +//! .await; //! +//! assert_eq!(diff, true); +//! } +//! ``` pub mod diff; pub mod tree; -use diff::Diff; +use diff::{DirDiff, FileDiff}; +use tokio::fs; use tree::{Tree, TreeBuilder}; /// Compare two tree directories and return true if both are different @@ -41,11 +58,11 @@ use tree::{Tree, TreeBuilder}; /// /// # Example /// ```rust -/// use spielrs_diff::{dir_diff, diff::Diff}; +/// use spielrs_diff::{dir_diff, diff::DirDiff}; /// /// #[tokio::test] /// async fn should_return_true_if_both_dir_tree_are_different() { -/// let diff = dir_diff(Diff { +/// let diff = dir_diff(DirDiff { /// dir: "./mocks/dir_one".to_string(), /// dir_comp: "./mocks/dir_five".to_string(), /// excluding: Some(vec!["purpose".to_string()]), @@ -57,17 +74,17 @@ use tree::{Tree, TreeBuilder}; /// } /// ``` /// -pub async fn dir_diff(diff_options: Diff) -> bool { +pub async fn dir_diff(dir_diff_options: DirDiff) -> bool { let tree_one: Vec = Tree::build_tree( - diff_options.dir, - diff_options.excluding.clone(), - diff_options.recursive_excluding, + dir_diff_options.dir, + dir_diff_options.excluding.clone(), + dir_diff_options.recursive_excluding, ) .await; let tree_two: Vec = Tree::build_tree( - diff_options.dir_comp, - diff_options.excluding, - diff_options.recursive_excluding, + dir_diff_options.dir_comp, + dir_diff_options.excluding, + dir_diff_options.recursive_excluding, ) .await; if Tree::tree_diff(tree_one.clone(), tree_two.clone()) { @@ -79,9 +96,35 @@ pub async fn dir_diff(diff_options: Diff) -> bool { !Tree::compare_dir_content(content_one, content_two) } +/// Compare two files and return true if both are different +/// +/// #Example +/// ```rust +/// use spielrs_diff::{file_diff, diff::FileDiff}; +/// +/// #[tokio::test] +/// async fn should_return_true_if_both_files_are_not_equal() { +/// let diff = file_diff(FileDiff { +/// file: "./mocks/dir_one/vlang/purpose/purpose.txt".to_string(), +/// file_comp: "./mocks/dir_five/vlang/purpose/purpose.txt".to_string(), +/// }) +/// .await; +/// +/// assert_eq!(diff, true); +/// } +/// ``` +pub async fn file_diff(file_diff_options: FileDiff) -> bool { + let file_one = fs::read_to_string(file_diff_options.file).await.unwrap(); + let file_two = fs::read_to_string(file_diff_options.file_comp) + .await + .unwrap(); + + file_one != file_two +} + #[tokio::test] async fn should_return_true_if_both_dir_tree_are_different() { - let diff = dir_diff(Diff { + let diff = dir_diff(DirDiff { dir: "./mocks/dir_one".to_string(), dir_comp: "./mocks/dir_three".to_string(), excluding: None, @@ -93,7 +136,7 @@ async fn should_return_true_if_both_dir_tree_are_different() { #[tokio::test] async fn should_return_false_if_both_dir_tree_are_equal() { - let diff = dir_diff(Diff { + let diff = dir_diff(DirDiff { dir: "./mocks/dir_one".to_string(), dir_comp: "./mocks/dir_two".to_string(), excluding: None, @@ -105,7 +148,7 @@ async fn should_return_false_if_both_dir_tree_are_equal() { #[tokio::test] async fn should_return_true_if_both_dir_tree_have_different_content() { - let diff = dir_diff(Diff { + let diff = dir_diff(DirDiff { dir: "./mocks/dir_one".to_string(), dir_comp: "./mocks/dir_four".to_string(), excluding: None, @@ -117,7 +160,7 @@ async fn should_return_true_if_both_dir_tree_have_different_content() { #[tokio::test] async fn should_return_false_if_both_dir_have_different_subdir_excluded_recursively() { - let diff = dir_diff(Diff { + let diff = dir_diff(DirDiff { dir: "./mocks/dir_one".to_string(), dir_comp: "./mocks/dir_five".to_string(), excluding: Some(vec!["purpose".to_string()]), @@ -129,7 +172,7 @@ async fn should_return_false_if_both_dir_have_different_subdir_excluded_recursiv #[tokio::test] async fn should_return_true_if_both_dir_have_different_subdir_excluded_not_recursively() { - let diff = dir_diff(Diff { + let diff = dir_diff(DirDiff { dir: "./mocks/dir_one".to_string(), dir_comp: "./mocks/dir_five".to_string(), excluding: Some(vec!["purpose".to_string()]), @@ -138,3 +181,25 @@ async fn should_return_true_if_both_dir_have_different_subdir_excluded_not_recur .await; assert_eq!(diff, true); } + +#[tokio::test] +async fn should_return_false_if_both_files_are_equal() { + let diff = file_diff(FileDiff { + file: "./mocks/dir_one/hello.txt".to_string(), + file_comp: "./mocks/dir_two/hello.txt".to_string(), + }) + .await; + + assert_eq!(diff, false); +} + +#[tokio::test] +async fn should_return_true_if_both_files_are_not_equal() { + let diff = file_diff(FileDiff { + file: "./mocks/dir_one/vlang/purpose/purpose.txt".to_string(), + file_comp: "./mocks/dir_five/vlang/purpose/purpose.txt".to_string(), + }) + .await; + + assert_eq!(diff, true); +}