1extern crate strum;
2
3use std::fmt;
4use strum_macros::{Display, EnumString, IntoStaticStr};
5
6#[derive(Clone, Debug, Eq, PartialEq)]
7pub struct Checksum {
8 pub algorithm: Algorithm,
9 pub value: String,
10}
11
12impl Checksum {
13 pub fn new(algorithm: Algorithm, value: &str) -> Checksum {
26 Checksum {
27 algorithm,
28 value: value.to_owned(),
29 }
30 }
31}
32
33impl fmt::Display for Checksum {
34 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35 write!(
36 f,
37 "{algorithm}:{value}",
38 algorithm = &self.algorithm.to_string(),
39 value = &self.value,
40 )
41 }
42}
43
44#[derive(Clone, EnumString, Debug, Display, Eq, PartialEq, IntoStaticStr)]
45#[strum(serialize_all = "kebab_case")]
46pub enum Algorithm {
47 Md5,
48 Sha1,
49 Sha256,
50 Sha384,
51 Sha512,
52}
53
54impl Algorithm {
56 pub fn variants() -> &'static [&'static str] {
57 &["md5", "sha1", "sha256", "sha384", "sha512"]
58 }
59}