diff --git a/Cargo.lock b/Cargo.lock index 15921e9..7071c72 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,99 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cpufeatures" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e4c1eaa2012c47becbbad2ab175484c2a84d1185b566fb2cc5b8707343dfe58" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f" +dependencies = [ + "block-buffer", + "crypto-common", +] + [[package]] name = "gateway" version = "0.1.0" +dependencies = [ + "hex", + "sha2", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "libc" +version = "0.2.142" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317" + +[[package]] +name = "sha2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "typenum" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" diff --git a/Cargo.toml b/Cargo.toml index c033627..ba2b721 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +sha2 = "0.10.6" +hex = "0.4.3" diff --git a/src/main.rs b/src/main.rs index e7a11a9..cb8a4ca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,13 @@ +mod origin; + +use crate::origin::Descr; + fn main() { - println!("Hello, world!"); + + let d = Descr::Git{ + url: "foo", + branch: "bar", + }; + + println!("{}", d.id()); } diff --git a/src/origin.rs b/src/origin.rs new file mode 100644 index 0000000..6fbd881 --- /dev/null +++ b/src/origin.rs @@ -0,0 +1,44 @@ +use sha2::{Sha256, Digest}; +use hex::ToHex; + +/// A unique description of an origin, from where a domain might be served. +pub enum Descr<'a> { + Git{url: &'a str, branch: &'a str}, +} + +impl<'a> Descr<'a> { + + /// Returns a globally unique string for the Descr. + pub fn id(&'a 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} => { + h_update("git"); + h_update(url); + h_update(branch); + } + } + + h.finalize().encode_hex::() + } +} + +#[cfg(test)] +mod descr_tests { + + #[test] + fn id() { + + assert_eq!( + super::Descr::Git{url:"foo", branch:"bar"}.id(), + "424130b9e6c1675c983b4b97579877e16d8a6377e4fe10970e6d210811c3b7ac", + ) + } +//} diff --git a/src/origin/descr.rs b/src/origin/descr.rs new file mode 100644 index 0000000..32964a0 --- /dev/null +++ b/src/origin/descr.rs @@ -0,0 +1,45 @@ +use sha2::{Sha256, Digest}; +use hex::ToHex; + +/// A unique description of an origin, from where a domain might be served. +pub enum Descr<'a> { + Git{url: &'a str, branch: &'a str}, +} + +impl<'a> Descr<'a> { + + /// Returns a globally unique string for the Descr. + pub fn id(&'a 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} => { + h_update("git"); + h_update(url); + h_update(branch); + } + } + + h.finalize().encode_hex::() + } +} + +#[cfg(test)] +mod descr_tests { + + #[test] + fn id() { + + assert_eq!( + super::Descr::Git{url:"foo", branch:"bar"}.id(), + "424130b9e6c1675c983b4b97579877e16d8a6377e4fe10970e6d210811c3b7ac", + ) + } +} +