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
use std::fmt;
use serde_derive::{Serialize, Deserialize};
use crate::errors::Error;
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum Algorithm {
Sha512,
Sha384,
Sha256,
Sha1,
}
impl fmt::Display for Algorithm {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", format!("{:?}", self).to_lowercase())
}
}
impl std::str::FromStr for Algorithm {
type Err = Error;
fn from_str(s: &str) -> Result<Algorithm, Self::Err> {
match s {
"sha1" => Ok(Algorithm::Sha1),
"sha256" => Ok(Algorithm::Sha256),
"sha384" => Ok(Algorithm::Sha384),
"sha512" => Ok(Algorithm::Sha512),
_ => Err(Error::ParseIntegrityError)
}
}
}
#[cfg(test)]
mod tests {
use super::Algorithm::*;
#[test]
fn algorithm_formatting() {
assert_eq!(format!("{}", Sha1), "sha1");
assert_eq!(format!("{}", Sha256), "sha256");
assert_eq!(format!("{}", Sha384), "sha384");
assert_eq!(format!("{}", Sha512), "sha512");
}
#[test]
fn ordering() {
let mut arr = [
Sha1,
Sha256,
Sha384,
Sha512,
];
arr.sort_unstable();
assert_eq!(arr, [
Sha512,
Sha384,
Sha256,
Sha1,
])
}
}