Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: reuse parse_numalgo to avoid duplication #1020

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions did_peer/src/peer_did/numalgos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,23 @@ pub enum NumalgoKind {
DidShortening(Numalgo3),
}

impl Display for NumalgoKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl NumalgoKind {
pub fn to_char(&self) -> char {
match self {
NumalgoKind::InceptionKeyWithoutDoc(_) => Numalgo0::NUMALGO_CHAR.fmt(f),
NumalgoKind::GenesisDoc(_) => Numalgo1::NUMALGO_CHAR.fmt(f),
NumalgoKind::MultipleInceptionKeys(_) => Numalgo2::NUMALGO_CHAR.fmt(f),
NumalgoKind::DidShortening(_) => Numalgo3::NUMALGO_CHAR.fmt(f),
NumalgoKind::InceptionKeyWithoutDoc(_) => Numalgo0::NUMALGO_CHAR,
NumalgoKind::GenesisDoc(_) => Numalgo1::NUMALGO_CHAR,
NumalgoKind::MultipleInceptionKeys(_) => Numalgo2::NUMALGO_CHAR,
NumalgoKind::DidShortening(_) => Numalgo3::NUMALGO_CHAR,
}
}
}

impl Display for NumalgoKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.to_char().fmt(f)
}
}

impl TryFrom<char> for NumalgoKind {
type Error = DidPeerError;

Expand Down
13 changes: 2 additions & 11 deletions did_peer/src/peer_did/numalgos/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use did_parser::Did;
use super::numalgo3::Numalgo3;
use crate::{
error::DidPeerError,
peer_did::{validate::validate, PeerDid},
peer_did::{parse::parse_numalgo, validate::validate, PeerDid},
peer_did_resolver::options::PublicKeyEncoding,
};

Expand All @@ -18,20 +18,11 @@ pub trait Numalgo: Sized + Default {
<Did as TryFrom<T>>::Error: Into<DidPeerError>,
{
let did: Did = did.try_into().map_err(Into::into)?;

let numalgo_char = did.id().chars().next().ok_or_else(|| {
DidPeerError::DidValidationError(format!(
"Invalid did: unable to read numalgo character in did {}",
did.did()
))
})?;

let numalgo_char = parse_numalgo(&did)?.to_char();
if numalgo_char != Self::NUMALGO_CHAR {
return Err(DidPeerError::InvalidNumalgoCharacter(numalgo_char));
}

validate(&did)?;

Ok(PeerDid::from_parts(did, Self::default()))
}
}
Expand Down