From 01029053c9a5827672e435c5ffd7353682e46b0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Thu, 27 Jan 2022 10:51:26 +0100 Subject: [PATCH] add url address type --- src/addressing.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/addressing.rs b/src/addressing.rs index d1099c5..5c9bbb6 100644 --- a/src/addressing.rs +++ b/src/addressing.rs @@ -15,6 +15,7 @@ pub enum Address { Hash(Hash), Uuid(Uuid), Attribute(String), + Url(String), } // multihash KangarooTwelve @@ -29,6 +30,7 @@ impl Address { Self::Hash(hash) => (KANGAROO_TWELVE, hash.0.clone()), Self::Uuid(uuid) => (IDENTITY, [vec![b'U'], uuid.as_bytes().to_vec()].concat()), Self::Attribute(attribute) => (IDENTITY, [&[b'A'], attribute.as_bytes()].concat()), + Self::Url(url) => (IDENTITY, [&[b'X'], url.as_bytes()].concat()), }; let mut result = Cursor::new(vec![0u8; 0]); @@ -59,6 +61,7 @@ impl Address { digest_content.as_slice(), )?)), b'A' => Ok(Self::Attribute(String::from_utf8(digest_content)?)), + b'X' => Ok(Self::Url(String::from_utf8(digest_content)?)), _ => Err(anyhow!("Unknown identity marker.")), } } @@ -114,7 +117,11 @@ impl FromStr for Address { impl std::fmt::Display for Address { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", b58_encode(self.encode().map_err(|_| std::fmt::Error)?)) + write!( + f, + "{}", + b58_encode(self.encode().map_err(|_| std::fmt::Error)?) + ) } } @@ -175,4 +182,17 @@ mod tests { assert_eq!(addr, decoded.unwrap()); } + + #[test] + fn test_url_codec() { + let addr = Address::Url(String::from("https://upendproject.net")); + + let encoded = addr.encode(); + assert!(encoded.is_ok()); + + let decoded = Address::decode(&encoded.unwrap()); + assert!(decoded.is_ok()); + + assert_eq!(addr, decoded.unwrap()); + } }