use hex::ToHex; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] /// A unique description of an origin, from where a domain might be served. pub enum Descr { Git { url: String, branch_name: String }, } impl Descr { /// Returns a globally unique string for the Descr. pub fn id(&self) -> String { let mut h = Sha256::new(); let mut h_update = |b: &str| { h.update(b); h.update("\n"); }; match self { Descr::Git { url, branch_name } => { h_update("git"); h_update(url); h_update(branch_name); } } h.finalize().encode_hex::() } } #[cfg(test)] mod descr_tests { #[test] fn id() { assert_eq!( super::Descr::Git { url: String::from("foo"), branch_name: String::from("bar"), } .id(), "424130b9e6c1675c983b4b97579877e16d8a6377e4fe10970e6d210811c3b7ac", ) } }