add url address type
This commit is contained in:
parent
b095f900f6
commit
01029053c9
1 changed files with 21 additions and 1 deletions
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue