-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Turn numalgos into child module of peer_did module
Signed-off-by: Patrik Stas <[email protected]>
- Loading branch information
1 parent
8ed979d
commit 08b44d1
Showing
29 changed files
with
201 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
extern crate display_as_json; | ||
|
||
pub mod error; | ||
mod numalgos; | ||
pub mod peer_did; | ||
pub mod peer_did_resolver; | ||
pub mod resolver; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::fmt::Display; | ||
|
||
use crate::{ | ||
error::DidPeerError, | ||
peer_did::numalgos::{ | ||
numalgo0::Numalgo0, numalgo1::Numalgo1, numalgo2::Numalgo2, numalgo3::Numalgo3, Numalgo, | ||
}, | ||
}; | ||
|
||
#[derive(Clone, Copy, Debug, PartialEq)] | ||
pub enum NumalgoKind { | ||
InceptionKeyWithoutDoc(Numalgo0), | ||
GenesisDoc(Numalgo1), | ||
MultipleInceptionKeys(Numalgo2), | ||
DidShortening(Numalgo3), | ||
} | ||
|
||
impl NumalgoKind { | ||
pub fn to_char(&self) -> char { | ||
match self { | ||
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; | ||
|
||
fn try_from(value: char) -> Result<Self, Self::Error> { | ||
match value { | ||
Numalgo0::NUMALGO_CHAR => Ok(NumalgoKind::InceptionKeyWithoutDoc(Numalgo0)), | ||
Numalgo1::NUMALGO_CHAR => Ok(NumalgoKind::GenesisDoc(Numalgo1)), | ||
Numalgo2::NUMALGO_CHAR => Ok(NumalgoKind::MultipleInceptionKeys(Numalgo2)), | ||
Numalgo3::NUMALGO_CHAR => Ok(NumalgoKind::DidShortening(Numalgo3)), | ||
c => Err(DidPeerError::InvalidNumalgoCharacter(c)), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,43 @@ | ||
pub mod kind; | ||
pub mod numalgo0; | ||
pub mod numalgo1; | ||
pub mod numalgo2; | ||
pub mod numalgo3; | ||
|
||
pub(super) mod traits; | ||
|
||
use std::fmt::Display; | ||
|
||
use numalgo0::Numalgo0; | ||
use numalgo1::Numalgo1; | ||
use numalgo2::Numalgo2; | ||
use numalgo3::Numalgo3; | ||
|
||
use self::traits::Numalgo; | ||
use crate::error::DidPeerError; | ||
|
||
#[derive(Clone, Copy, Debug, PartialEq)] | ||
pub enum NumalgoKind { | ||
InceptionKeyWithoutDoc(Numalgo0), | ||
GenesisDoc(Numalgo1), | ||
MultipleInceptionKeys(Numalgo2), | ||
DidShortening(Numalgo3), | ||
} | ||
|
||
impl NumalgoKind { | ||
pub fn to_char(&self) -> char { | ||
match self { | ||
NumalgoKind::InceptionKeyWithoutDoc(_) => Numalgo0::NUMALGO_CHAR, | ||
NumalgoKind::GenesisDoc(_) => Numalgo1::NUMALGO_CHAR, | ||
NumalgoKind::MultipleInceptionKeys(_) => Numalgo2::NUMALGO_CHAR, | ||
NumalgoKind::DidShortening(_) => Numalgo3::NUMALGO_CHAR, | ||
use did_doc::schema::did_doc::DidDocument; | ||
use did_doc_sov::extra_fields::ExtraFieldsSov; | ||
use did_parser::Did; | ||
|
||
use crate::{ | ||
error::DidPeerError, | ||
peer_did::{parse::parse_numalgo, validate::validate, FromDidDoc, PeerDid}, | ||
resolver::options::PublicKeyEncoding, | ||
}; | ||
|
||
pub trait Numalgo: Sized + Default { | ||
const NUMALGO_CHAR: char; | ||
|
||
fn parse<T>(did: T) -> Result<PeerDid<Self>, DidPeerError> | ||
where | ||
Did: TryFrom<T>, | ||
<Did as TryFrom<T>>::Error: Into<DidPeerError>, | ||
{ | ||
let did: Did = did.try_into().map_err(Into::into)?; | ||
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())) | ||
} | ||
} | ||
|
||
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; | ||
|
||
fn try_from(value: char) -> Result<Self, Self::Error> { | ||
match value { | ||
Numalgo0::NUMALGO_CHAR => Ok(NumalgoKind::InceptionKeyWithoutDoc(Numalgo0)), | ||
Numalgo1::NUMALGO_CHAR => Ok(NumalgoKind::GenesisDoc(Numalgo1)), | ||
Numalgo2::NUMALGO_CHAR => Ok(NumalgoKind::MultipleInceptionKeys(Numalgo2)), | ||
Numalgo3::NUMALGO_CHAR => Ok(NumalgoKind::DidShortening(Numalgo3)), | ||
c => Err(DidPeerError::InvalidNumalgoCharacter(c)), | ||
} | ||
} | ||
pub trait ResolvableNumalgo: Numalgo { | ||
fn resolve( | ||
&self, | ||
did: &Did, | ||
public_key_encoding: PublicKeyEncoding, | ||
) -> Result<DidDocument<ExtraFieldsSov>, DidPeerError>; | ||
} |
2 changes: 1 addition & 1 deletion
2
did_peer/src/peer_did/numalgos/numalgo0.rs → ...eer/src/peer_did/numalgos/numalgo0/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
did_peer/src/peer_did/numalgos/numalgo1.rs → ...eer/src/peer_did/numalgos/numalgo1/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.