Skip to content

Commit 156c15e

Browse files
format code
1 parent f33556c commit 156c15e

24 files changed

Lines changed: 589 additions & 563 deletions

src/algorithms.rs

Lines changed: 65 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,74 +21,80 @@ use std::str::FromStr;
2121
///
2222
/// ```
2323
/// # use std::str::FromStr;
24-
/// assert_eq!(quickdash::Algorithm::from_str("BLAKE3"), Ok(quickdash::Algorithm::BLAKE3));
25-
/// assert_eq!(quickdash::Algorithm::from_str("MD5"), Ok(quickdash::Algorithm::MD5));
24+
/// assert_eq!(
25+
/// quickdash::Algorithm::from_str("BLAKE3"),
26+
/// Ok(quickdash::Algorithm::BLAKE3)
27+
/// );
28+
/// assert_eq!(
29+
/// quickdash::Algorithm::from_str("MD5"),
30+
/// Ok(quickdash::Algorithm::MD5)
31+
/// );
2632
/// ```
2733
2834
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
2935
pub enum Algorithm {
30-
SHA1,
31-
SHA2224,
32-
SHA2256,
33-
SHA2384,
34-
SHA2512,
35-
SHA3224,
36-
SHA3256,
37-
SHA3384,
38-
SHA3512,
39-
XXH32,
40-
XXH64,
41-
XXH3,
42-
CRC32,
43-
MD5,
44-
WhirlPool,
45-
BLAKE2B,
46-
BLAKE2S,
47-
BLAKE3,
36+
SHA1,
37+
SHA2224,
38+
SHA2256,
39+
SHA2384,
40+
SHA2512,
41+
SHA3224,
42+
SHA3256,
43+
SHA3384,
44+
SHA3512,
45+
XXH32,
46+
XXH64,
47+
XXH3,
48+
CRC32,
49+
MD5,
50+
WhirlPool,
51+
BLAKE2B,
52+
BLAKE2S,
53+
BLAKE3,
4854
}
4955

5056
impl Algorithm {
51-
/// Length, in bytes, of the algorithm's output hex string
52-
pub fn hexlen(&self) -> usize {
53-
match *self {
54-
Algorithm::CRC32 | Algorithm::XXH32 => 8,
55-
Algorithm::XXH3 | Algorithm::XXH64 => 16,
56-
Algorithm::MD5 => 32,
57-
Algorithm::SHA3256 | Algorithm::SHA2256 | Algorithm::BLAKE2S | Algorithm::BLAKE3 => 64,
58-
Algorithm::SHA1 => 40,
59-
Algorithm::SHA2224 | Algorithm::SHA3224 => 56,
60-
Algorithm::SHA2384 | Algorithm::SHA3384 => 96,
61-
Algorithm::BLAKE2B | Algorithm::SHA3512 | Algorithm::SHA2512 | Algorithm::WhirlPool => {
62-
128
63-
}
64-
}
65-
}
57+
/// Length, in bytes, of the algorithm's output hex string
58+
pub fn hexlen(&self) -> usize {
59+
match *self {
60+
Algorithm::CRC32 | Algorithm::XXH32 => 8,
61+
Algorithm::XXH3 | Algorithm::XXH64 => 16,
62+
Algorithm::MD5 => 32,
63+
Algorithm::SHA3256 | Algorithm::SHA2256 | Algorithm::BLAKE2S | Algorithm::BLAKE3 => 64,
64+
Algorithm::SHA1 => 40,
65+
Algorithm::SHA2224 | Algorithm::SHA3224 => 56,
66+
Algorithm::SHA2384 | Algorithm::SHA3384 => 96,
67+
Algorithm::BLAKE2B | Algorithm::SHA3512 | Algorithm::SHA2512 | Algorithm::WhirlPool => {
68+
128
69+
}
70+
}
71+
}
6672
}
6773

6874
impl FromStr for Algorithm {
69-
type Err = String;
75+
type Err = String;
7076

71-
fn from_str(s: &str) -> Result<Self, Self::Err> {
72-
match &s.replace("_", "-").to_lowercase()[..] {
73-
"sha-1" | "sha1" => Ok(Algorithm::SHA1),
74-
"sha2224" | "sha-224" | "sha-2-224" => Ok(Algorithm::SHA2224),
75-
"sha2256" | "sha-256" | "sha-2-256" => Ok(Algorithm::SHA2256),
76-
"sha2384" | "sha-384" | "sha-2-384" => Ok(Algorithm::SHA2384),
77-
"sha2512" | "sha-512" | "sha-2-512" => Ok(Algorithm::SHA2512),
78-
"sha3224" | "sha3-224" | "sha-3-224" => Ok(Algorithm::SHA3224),
79-
"sha3256" | "sha3-256" | "sha-3-256" => Ok(Algorithm::SHA3256),
80-
"sha3384" | "sha3-384" | "sha-3-384" => Ok(Algorithm::SHA3384),
81-
"sha3512" | "sha3-512" | "sha-3-512" => Ok(Algorithm::SHA3512),
82-
"crc32" => Ok(Algorithm::CRC32),
83-
"xxhash64" | "xxh64" => Ok(Algorithm::XXH64),
84-
"xxhash32" | "xxh32" => Ok(Algorithm::XXH32),
85-
"xxhash3" | "xxh3" => Ok(Algorithm::XXH3),
86-
"md5" => Ok(Algorithm::MD5),
87-
"blake2b" => Ok(Algorithm::BLAKE2B),
88-
"blake2s" => Ok(Algorithm::BLAKE2S),
89-
"blake3" => Ok(Algorithm::BLAKE3),
90-
"whirlpool" => Ok(Algorithm::WhirlPool),
91-
_ => Err(format!("\"{}\" is not a recognised hashing algorithm", s)),
92-
}
93-
}
77+
fn from_str(s: &str) -> Result<Self, Self::Err> {
78+
match &s.replace("_", "-").to_lowercase()[..] {
79+
"sha-1" | "sha1" => Ok(Algorithm::SHA1),
80+
"sha2224" | "sha-224" | "sha-2-224" => Ok(Algorithm::SHA2224),
81+
"sha2256" | "sha-256" | "sha-2-256" => Ok(Algorithm::SHA2256),
82+
"sha2384" | "sha-384" | "sha-2-384" => Ok(Algorithm::SHA2384),
83+
"sha2512" | "sha-512" | "sha-2-512" => Ok(Algorithm::SHA2512),
84+
"sha3224" | "sha3-224" | "sha-3-224" => Ok(Algorithm::SHA3224),
85+
"sha3256" | "sha3-256" | "sha-3-256" => Ok(Algorithm::SHA3256),
86+
"sha3384" | "sha3-384" | "sha-3-384" => Ok(Algorithm::SHA3384),
87+
"sha3512" | "sha3-512" | "sha-3-512" => Ok(Algorithm::SHA3512),
88+
"crc32" => Ok(Algorithm::CRC32),
89+
"xxhash64" | "xxh64" => Ok(Algorithm::XXH64),
90+
"xxhash32" | "xxh32" => Ok(Algorithm::XXH32),
91+
"xxhash3" | "xxh3" => Ok(Algorithm::XXH3),
92+
"md5" => Ok(Algorithm::MD5),
93+
"blake2b" => Ok(Algorithm::BLAKE2B),
94+
"blake2s" => Ok(Algorithm::BLAKE2S),
95+
"blake3" => Ok(Algorithm::BLAKE3),
96+
"whirlpool" => Ok(Algorithm::WhirlPool),
97+
_ => Err(format!("\"{}\" is not a recognised hashing algorithm", s)),
98+
}
99+
}
94100
}

src/error.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,39 @@
1616
/// Enum representing each way the appication can fail.
1717
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
1818
pub enum Error {
19-
/// No errors occured, everything executed correctly.
20-
NoError,
21-
/// Parsing of command-line options failed.
22-
OptionParsingError,
23-
/// Selected and saved hash lengths differ.
24-
HashLengthDiffers,
25-
/// Parsing the hashes file failed.
26-
HashesFileParsingFailure,
27-
/// The specified amount of files do not match.
28-
NFilesDiffer(i32),
19+
/// No errors occured, everything executed correctly.
20+
NoError,
21+
/// Parsing of command-line options failed.
22+
OptionParsingError,
23+
/// Selected and saved hash lengths differ.
24+
HashLengthDiffers,
25+
/// Parsing the hashes file failed.
26+
HashesFileParsingFailure,
27+
/// The specified amount of files do not match.
28+
NFilesDiffer(i32),
2929
}
3030

3131
impl Error {
32-
/// Get the executable exit value from an `Error` instance.
33-
pub fn exit_value(&self) -> i32 {
34-
match *self {
35-
Error::NoError => 0,
36-
Error::OptionParsingError => 1,
37-
Error::HashLengthDiffers => 2,
38-
Error::HashesFileParsingFailure => 3,
39-
Error::NFilesDiffer(i) => i + 3,
40-
}
41-
}
32+
/// Get the executable exit value from an `Error` instance.
33+
pub fn exit_value(&self) -> i32 {
34+
match *self {
35+
Error::NoError => 0,
36+
Error::OptionParsingError => 1,
37+
Error::HashLengthDiffers => 2,
38+
Error::HashesFileParsingFailure => 3,
39+
Error::NFilesDiffer(i) => i + 3,
40+
}
41+
}
4242
}
4343

4444
impl From<i32> for Error {
45-
fn from(i: i32) -> Self {
46-
match i {
47-
0 => Error::NoError,
48-
1 => Error::OptionParsingError,
49-
2 => Error::HashLengthDiffers,
50-
3 => Error::HashesFileParsingFailure,
51-
i => Error::NFilesDiffer(i - 3),
52-
}
53-
}
45+
fn from(i: i32) -> Self {
46+
match i {
47+
0 => Error::NoError,
48+
1 => Error::OptionParsingError,
49+
2 => Error::HashLengthDiffers,
50+
3 => Error::HashesFileParsingFailure,
51+
i => Error::NFilesDiffer(i - 3),
52+
}
53+
}
5454
}

src/hashing/blake3.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
use crate::hash_string;
1616

1717
hash_func!(
18-
blake3::Hasher::new(),
19-
|blake: &mut blake3::Hasher, buffer: &[u8]| {
20-
blake.update(buffer);
21-
},
22-
|blake: blake3::Hasher| hash_string(blake.finalize().as_bytes())
18+
blake3::Hasher::new(),
19+
|blake: &mut blake3::Hasher, buffer: &[u8]| {
20+
blake.update(buffer);
21+
},
22+
|blake: blake3::Hasher| hash_string(blake.finalize().as_bytes())
2323
);

src/hashing/crc32.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515

1616
hash_func!(
17-
crc32fast::Hasher::new(),
18-
|crc: &mut crc32fast::Hasher, buffer: &[u8]| crc.update(buffer),
19-
|crc: crc32fast::Hasher| format!("{:08X}", (&crc.finalize()))
17+
crc32fast::Hasher::new(),
18+
|crc: &mut crc32fast::Hasher, buffer: &[u8]| crc.update(buffer),
19+
|crc: crc32fast::Hasher| format!("{:08X}", (&crc.finalize()))
2020
);

src/hashing/md5.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
* limitations under the License.
1414
*/
1515

16-
use crate::hash_string;
1716
use md5::{Digest, Md5};
1817

18+
use crate::hash_string;
19+
1920
hash_func_write!(Md5::new(), |ctx: Md5| hash_string(&*ctx.finalize()));

src/hashing/mod.rs

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,43 @@
1414
*/
1515

1616
macro_rules! hash_func {
17-
($ctx:expr, $update:expr, $convert:expr) => {
18-
use std::io::Read;
17+
($ctx:expr, $update:expr, $convert:expr) => {
18+
use std::io::Read;
1919

20-
pub fn hash<R: Read>(reader: &mut R) -> String {
21-
let mut buffer = vec![0; 4096];
20+
pub fn hash<R: Read>(reader: &mut R) -> String {
21+
let mut buffer = vec![0; 4096];
2222

23-
let mut ctx = $ctx;
24-
loop {
25-
let read = reader.read(&mut buffer[..]).unwrap();
23+
let mut ctx = $ctx;
24+
loop {
25+
let read = reader.read(&mut buffer[..]).unwrap();
2626

27-
if read == 0 {
28-
break;
29-
}
27+
if read == 0 {
28+
break;
29+
}
3030

31-
$update(&mut ctx, &buffer[..read]);
32-
}
31+
$update(&mut ctx, &buffer[..read]);
32+
}
3333

34-
$convert(ctx)
35-
}
36-
};
34+
$convert(ctx)
35+
}
36+
};
3737
}
3838

3939
macro_rules! hash_func_write {
40-
($ctx:expr, $convert:expr) => {
41-
use std::io::{self, Read};
40+
($ctx:expr, $convert:expr) => {
41+
use std::io::{self, Read};
4242

43-
pub fn hash<R: Read>(reader: &mut R) -> String {
44-
let mut ctx = $ctx;
45-
io::copy(reader, &mut ctx).unwrap();
46-
$convert(ctx)
47-
}
48-
};
43+
pub fn hash<R: Read>(reader: &mut R) -> String {
44+
let mut ctx = $ctx;
45+
io::copy(reader, &mut ctx).unwrap();
46+
$convert(ctx)
47+
}
48+
};
4949
}
5050

51+
use std::{fmt::Write, fs::File, io::Read, path::Path};
52+
5153
use super::Algorithm;
52-
use std::fmt::Write;
53-
use std::fs::File;
54-
use std::io::Read;
55-
use std::path::Path;
5654

5755
mod blake2b;
5856
mod blake2s;
@@ -75,45 +73,48 @@ mod xxh64;
7573

7674
/// Hash the specified file using the specified hashing algorithm.
7775
pub fn hash_file(algo: Algorithm, path: &Path) -> String {
78-
hash_reader(algo, &mut File::open(path).unwrap())
76+
hash_reader(algo, &mut File::open(path).unwrap())
7977
}
8078

8179
/// Hash the specified byte stream using the specified hashing algorithm.
8280
pub fn hash_reader<R: Read>(algo: Algorithm, data: &mut R) -> String {
83-
match algo {
84-
Algorithm::CRC32 => crc32::hash(data),
85-
Algorithm::SHA1 => sha1::hash(data),
86-
Algorithm::SHA2224 => sha2_224::hash(data),
87-
Algorithm::SHA2256 => sha2_256::hash(data),
88-
Algorithm::SHA2384 => sha2_384::hash(data),
89-
Algorithm::SHA2512 => sha2_512::hash(data),
90-
Algorithm::SHA3224 => sha3_224::hash(data),
91-
Algorithm::SHA3256 => sha3_256::hash(data),
92-
Algorithm::SHA3384 => sha3_384::hash(data),
93-
Algorithm::SHA3512 => sha3_512::hash(data),
94-
Algorithm::MD5 => md5::hash(data),
95-
Algorithm::XXH64 => xxh64::hash(data),
96-
Algorithm::XXH32 => xxh32::hash(data),
97-
Algorithm::XXH3 => xxh3::hash(data),
98-
Algorithm::BLAKE2B => blake2b::hash(data),
99-
Algorithm::BLAKE2S => blake2s::hash(data),
100-
Algorithm::BLAKE3 => blake3::hash(data),
101-
Algorithm::WhirlPool => whirlpool::hash(data),
102-
}
81+
match algo {
82+
Algorithm::CRC32 => crc32::hash(data),
83+
Algorithm::SHA1 => sha1::hash(data),
84+
Algorithm::SHA2224 => sha2_224::hash(data),
85+
Algorithm::SHA2256 => sha2_256::hash(data),
86+
Algorithm::SHA2384 => sha2_384::hash(data),
87+
Algorithm::SHA2512 => sha2_512::hash(data),
88+
Algorithm::SHA3224 => sha3_224::hash(data),
89+
Algorithm::SHA3256 => sha3_256::hash(data),
90+
Algorithm::SHA3384 => sha3_384::hash(data),
91+
Algorithm::SHA3512 => sha3_512::hash(data),
92+
Algorithm::MD5 => md5::hash(data),
93+
Algorithm::XXH64 => xxh64::hash(data),
94+
Algorithm::XXH32 => xxh32::hash(data),
95+
Algorithm::XXH3 => xxh3::hash(data),
96+
Algorithm::BLAKE2B => blake2b::hash(data),
97+
Algorithm::BLAKE2S => blake2s::hash(data),
98+
Algorithm::BLAKE3 => blake3::hash(data),
99+
Algorithm::WhirlPool => whirlpool::hash(data),
100+
}
103101
}
104102

105103
/// Create a hash string out of its raw bytes.
106104
///
107105
/// # Examples
108106
///
109107
/// ```
110-
/// assert_eq!(quickdash::hash_string(&[0x99, 0xAA, 0xBB, 0xCC]), "99AABBCC".to_string());
108+
/// assert_eq!(
109+
/// quickdash::hash_string(&[0x99, 0xAA, 0xBB, 0xCC]),
110+
/// "99AABBCC".to_string()
111+
/// );
111112
/// assert_eq!(quickdash::hash_string(&[0x09, 0x0A]), "090A".to_string());
112113
/// ```
113114
pub fn hash_string(bytes: &[u8]) -> String {
114-
let mut result = String::with_capacity(bytes.len() * 2);
115-
for b in bytes {
116-
write!(result, "{:02X}", b).unwrap();
117-
}
118-
result
115+
let mut result = String::with_capacity(bytes.len() * 2);
116+
for b in bytes {
117+
write!(result, "{:02X}", b).unwrap();
118+
}
119+
result
119120
}

0 commit comments

Comments
 (0)