Debian Codegraph @

src/lib.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // (c) 2014, Ethan Pailes
2 // All Rights Reserved
3 //
4 // Licensed under the "Revised" BSD 3-clause licence. See LICENCE file at the
5 // root directory of this repository.
6
7 //! File Diff
8 //!
9 //! This module provides an atomic file diffing function for use in unit tests.
10 //!
11 //! The diff_files() function takes two file handles and determines returns true
12 //! if they point to identical files.
13 //!
14 //! ```
15 //! use file_diff::{diff_files};
16 //! use std::fs::{File};
17 //!
18 //! let mut file1 = match File::open("./src/lib.rs") {
19 //! Ok(f) => f,
20 //! Err(e) => panic!("{}", e),
21 //! };
22 //! let mut file2 = match File::open("./src/lib.rs") {
23 //! Ok(f) => f,
24 //! Err(e) => panic!("{}", e),
25 //! };
26 //!
27 //! diff_files(&mut file1, &mut file2);
28 //! ```
29 //!
30 //! The diff() function takes string representations of the files and returns true
31 //! if the strings represent real files and those files are identical.
32 //!
33 //! ```
34 //! use file_diff::{diff};
35 //!
36 //! diff("./src/lib.rs", "./src/lib.rs");
37 //! ```
38
39 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#;
40
41
42
43 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::{Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#};
44
45
46 /// Takes two file arguments and returns true if the two files are identical.
47 pub fn diff_filesrust-analyzer cargo file_diff 1.0.0 diff_files().(f1: &mut Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#, f2: &mut Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#) -> bool {
48
49 let mut buff1 : &mut [u8] = &mut [0; 1024];
50 let mut buff2 : &mut [u8] = &mut [0; 1024];
51
52 loop {
53
54 match f1.readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Read]read().(buff1) {
55 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => return false,
56 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(f1_read_len) => match f2.readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Read]read().(buff2) {
57 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => return false,
58 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(f2_read_len) => {
59 if f1_read_len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().f2_read_len {
60 return false;
61 }
62 if f1_read_len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 {
63 return true;
64 }
65 if &buff1[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#f1_read_len]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne().&buff2[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#f2_read_len]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). {
66 return false;
67 }
68 }
69 }
70 }
71 }
72 }
73
74 /// Takes two string filepaths and returns true if the two files are identical and exist.
75 pub fn diffrust-analyzer cargo file_diff 1.0.0 diff().(f1: &str, f2: &str) -> bool {
76 let mut fh1 = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(f1);
77 let mut fh2 = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(f2);
78
79 fh1.as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]as_mut().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then().(|file1|
80 fh2.as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]as_mut().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then().(|file2|
81 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(diff_filesrust-analyzer cargo file_diff 1.0.0 diff_files().(file1, file2)))).unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or().(false)
82 }
83
84 #[cfg(test)]
85 mod testsrust-analyzer cargo file_diff 1.0.0 tests/ {
86 const LIB_RSrust-analyzer cargo file_diff 1.0.0 tests/LIB_RS.: &'static str = "./src/lib.rs";
87 const CARGO_FILErust-analyzer cargo file_diff 1.0.0 tests/CARGO_FILE.: &'static str = "./Cargo.toml";
•88 /*
89 const RUST_BIN_FILE: &'static str = "./testdata/rust_hello";
90 const C_BIN_FILE: &'static str = "./testdata/c_hello";
91 const C_BIN_FILE_COPY: &'static str = "./testdata/c_hello_copy";
•92 */
93
94 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
95 fn diff_the_same_text_filerust-analyzer cargo file_diff 1.0.0 tests/diff_the_same_text_file().() { assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(superrust-analyzer cargo file_diff 1.0.0 crate/::diffrust-analyzer cargo file_diff 1.0.0 diff().(LIB_RSrust-analyzer cargo file_diff 1.0.0 tests/LIB_RS., LIB_RSrust-analyzer cargo file_diff 1.0.0 tests/LIB_RS.)); }
96
•97 /*
98 #[test]
99 fn diff_the_same_binary_file() { assert!(super::diff(RUST_BIN_FILE, RUST_BIN_FILE)); }
100
101 #[test]
102 fn diff_identical_binary_files() { assert!(super::diff(C_BIN_FILE, C_BIN_FILE_COPY)); }
•103 */
104
105 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
106 fn diff_different_text_filesrust-analyzer cargo file_diff 1.0.0 tests/diff_different_text_files().() { assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!( !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().superrust-analyzer cargo file_diff 1.0.0 crate/::diffrust-analyzer cargo file_diff 1.0.0 diff().(LIB_RSrust-analyzer cargo file_diff 1.0.0 tests/LIB_RS., CARGO_FILErust-analyzer cargo file_diff 1.0.0 tests/CARGO_FILE.)); }
107
•108 /*
109 #[test]
110 fn diff_different_binary_files() { assert!( !super::diff(RUST_BIN_FILE, C_BIN_FILE)); }
•111 */
112 }
113