Skip to content

Commit

Permalink
rename from uri to url
Browse files Browse the repository at this point in the history
  • Loading branch information
TheButlah committed May 20, 2024
1 parent bb315cb commit 354dd6e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 39 deletions.
12 changes: 8 additions & 4 deletions crates/did-simple/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@

use std::str::FromStr;

pub mod key_algos;
pub(crate) mod key_algos;
pub mod methods;
pub mod uri;
pub mod url;
pub mod utf8bytes;
pub mod varint;
mod varint;

pub use crate::key_algos::KeyAlgo;
pub use crate::methods::DidDyn;
pub use crate::url::DidUrl;

pub trait Did: FromStr {
fn uri(&self) -> self::uri::DidUri;
fn url(&self) -> self::url::DidUrl;
}
38 changes: 19 additions & 19 deletions crates/did-simple/src/methods/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::fmt::Display;

use crate::{
key_algos::{Ed25519, KeyAlgo, StaticKeyAlgo},
uri::{DidMethod, DidUri},
url::{DidMethod, DidUrl},
utf8bytes::Utf8Bytes,
varint::decode_varint,
};
Expand All @@ -29,17 +29,17 @@ pub const PREFIX: &str = "did:key:";
impl DidKey {
pub const PREFIX: &'static str = PREFIX;

/// Gets the buffer representing the did:key uri as a str.
/// Gets the buffer representing the did:key url as a str.
pub fn as_str(&self) -> &str {
self.s.as_str()
}

/// Gets the buffer representing the did:key uri as a byte slice.
/// Gets the buffer representing the did:key url as a byte slice.
pub fn as_slice(&self) -> &[u8] {
self.s.as_slice()
}

/// Gets the buffer representing the did:key uri as a reference counted slice
/// Gets the buffer representing the did:key url as a reference counted slice
/// that is guaranteed to be utf8.
pub fn as_utf8_bytes(&self) -> &Utf8Bytes {
&self.s
Expand Down Expand Up @@ -87,13 +87,13 @@ pub enum MultibaseDecodeError {
Bs58(#[from] bs58::decode::Error),
}

impl TryFrom<DidUri> for DidKey {
type Error = FromUriError;
impl TryFrom<DidUrl> for DidKey {
type Error = FromUrlError;

fn try_from(value: DidUri) -> Result<Self, Self::Error> {
fn try_from(value: DidUrl) -> Result<Self, Self::Error> {
let m = value.method();
if m != DidMethod::Key {
return Err(FromUriError::WrongMethod(m));
return Err(FromUrlError::WrongMethod(m));
}
debug_assert_eq!(
value.as_slice().len() - value.method_specific_id().as_slice().len(),
Expand All @@ -109,11 +109,11 @@ impl TryFrom<DidUri> for DidKey {
let (multicodec_key_algo, tail_bytes) = decode_varint(&decoded_multibase)?;
let (key_algo, pub_key_len) = match multicodec_key_algo {
Ed25519::MULTICODEC_VALUE => (KeyAlgo::Ed25519, Ed25519::PUB_KEY_LEN),
_ => return Err(FromUriError::UnknownKeyAlgo(multicodec_key_algo)),
_ => return Err(FromUrlError::UnknownKeyAlgo(multicodec_key_algo)),
};

if tail_bytes.len() != pub_key_len {
return Err(FromUriError::MismatchedPubkeyLen(key_algo, pub_key_len));
return Err(FromUrlError::MismatchedPubkeyLen(key_algo, pub_key_len));
}

let pubkey_bytes = (decoded_multibase.len() - pub_key_len)..;
Expand All @@ -128,7 +128,7 @@ impl TryFrom<DidUri> for DidKey {
}

#[derive(thiserror::Error, Debug)]
pub enum FromUriError {
pub enum FromUrlError {
#[error("Expected \"key\" method but got {0:?}")]
WrongMethod(DidMethod),
#[error(transparent)]
Expand Down Expand Up @@ -165,15 +165,15 @@ mod test {
}

#[test]
fn test_try_from_uri() -> eyre::Result<()> {
fn test_try_from_url() -> eyre::Result<()> {
for &example in ed25519_examples() {
let uri = DidUri::from_str(example)
.wrap_err_with(|| format!("failed to parse DidUri from {example}"))?;
assert_eq!(example, uri.as_str());
let key_from_uri = DidKey::try_from(uri.clone())
.wrap_err_with(|| format!("failed to parse DidKey from {uri}"))?;
assert_eq!(example, key_from_uri.as_str());
assert_eq!(key_from_uri.key_algo(), Ed25519);
let url = Did::from_str(example)
.wrap_err_with(|| format!("failed to parse DidUrl from {example}"))?;
assert_eq!(example, url.as_str());
let key_from_url = DidKey::try_from(url.clone())
.wrap_err_with(|| format!("failed to parse DidKey from {url}"))?;
assert_eq!(example, key_from_url.as_str());
assert_eq!(key_from_url.key_algo(), Ed25519);
}
Ok(())
}
Expand Down
33 changes: 17 additions & 16 deletions crates/did-simple/src/uri.rs → crates/did-simple/src/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ impl FromStr for DidMethod {
}
}

/// Helper type to access data in the method-specific-id of a [`DidUri`].
pub struct MethodSpecificId<'a>(&'a DidUri);
/// Helper type to access data in the method-specific-id of a [`DidUrl`].
pub struct MethodSpecificId<'a>(&'a DidUrl);

impl MethodSpecificId<'_> {
pub fn as_str(&self) -> &str {
Expand All @@ -38,27 +38,28 @@ impl MethodSpecificId<'_> {
}
}

/// A Decentralized Identifier, including any path information, as a url.
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub struct DidUri {
pub struct DidUrl {
method: DidMethod,
/// The string representation of the DID.
s: Utf8Bytes,
/// The substring for method-specific-id. This is a range index into `s`.
method_specific_id: std::ops::RangeFrom<usize>,
}

impl DidUri {
/// Gets the buffer representing the uri as a str.
impl DidUrl {
/// Gets the buffer representing the url as a str.
pub fn as_str(&self) -> &str {
self.s.as_str()
}

/// Gets the buffer representing the uri as a byte slice.
/// Gets the buffer representing the url as a byte slice.
pub fn as_slice(&self) -> &[u8] {
self.s.as_slice()
}

/// Gets the buffer representing the uri as a reference counted slice that
/// Gets the buffer representing the url as a reference counted slice that
/// is guaranteed to be utf8.
pub fn as_utf8_bytes(&self) -> &Utf8Bytes {
&self.s
Expand All @@ -75,7 +76,7 @@ impl DidUri {
}
}

impl FromStr for DidUri {
impl FromStr for DidUrl {
type Err = ParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand All @@ -87,15 +88,15 @@ impl FromStr for DidUri {
let method = DidMethod::from_str(method)?;
let start_idx = s.len() - remaining.len();

Ok(DidUri {
Ok(DidUrl {
method,
s: Utf8Bytes::from(s.to_owned()),
method_specific_id: (start_idx..),
})
}
}

impl TryFrom<String> for DidUri {
impl TryFrom<String> for DidUrl {
type Error = ParseError;

fn try_from(s: String) -> Result<Self, Self::Error> {
Expand All @@ -107,7 +108,7 @@ impl TryFrom<String> for DidUri {
let method = DidMethod::from_str(method)?;
let start_idx = s.len() - remaining.len();

Ok(DidUri {
Ok(DidUrl {
method,
s: Utf8Bytes::from(s),
method_specific_id: (start_idx..),
Expand All @@ -125,7 +126,7 @@ pub enum ParseError {
UnknownMethod,
}

impl Display for DidUri {
impl Display for DidUrl {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.as_str().fmt(f)
}
Expand All @@ -136,8 +137,8 @@ mod test {
use super::*;
use eyre::{Result, WrapErr};

fn common_test_cases() -> Vec<DidUri> {
vec![DidUri {
fn common_test_cases() -> Vec<DidUrl> {
vec![DidUrl {
method: DidMethod::Key,
s: String::from("did:key:123456").into(),
method_specific_id: (8..),
Expand All @@ -148,8 +149,8 @@ mod test {
fn test_parse() -> Result<()> {
for expected in common_test_cases() {
let s = expected.s.as_str().to_owned();
let from_str = DidUri::from_str(&s).wrap_err("failed to from_str")?;
let try_from = DidUri::try_from(s).wrap_err("failed to try_from")?;
let from_str = DidUrl::from_str(&s).wrap_err("failed to from_str")?;
let try_from = DidUrl::try_from(s).wrap_err("failed to try_from")?;
assert_eq!(from_str, try_from);
assert_eq!(from_str, expected);
}
Expand Down

0 comments on commit 354dd6e

Please sign in to comment.