-
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.
Signed-off-by: Miroslav Kovar <[email protected]>
- Loading branch information
Showing
6 changed files
with
303 additions
and
123 deletions.
There are no files selected for viewing
77 changes: 75 additions & 2 deletions
77
aries/misc/anoncreds_types/src/data_types/identifiers/cred_def_id.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,76 @@ | ||
use crate::impl_anoncreds_object_identifier; | ||
use crate::{ | ||
error::Error, | ||
utils::validation::{Validatable, LEGACY_CRED_DEF_IDENTIFIER, URI_IDENTIFIER}, | ||
}; | ||
|
||
impl_anoncreds_object_identifier!(CredentialDefinitionId); | ||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize, Default)] | ||
pub struct CredentialDefinitionId(pub String); | ||
|
||
impl CredentialDefinitionId { | ||
pub fn new_unchecked(s: impl Into<String>) -> Self { | ||
Self(s.into()) | ||
} | ||
|
||
pub fn new(s: impl Into<String>) -> Result<Self, Error> { | ||
let s = Self(s.into()); | ||
Validatable::validate(&s)?; | ||
Ok(s) | ||
} | ||
|
||
pub fn is_legacy_cred_def_identifier(&self) -> bool { | ||
LEGACY_CRED_DEF_IDENTIFIER.captures(&self.0).is_some() | ||
} | ||
|
||
pub fn is_uri(&self) -> bool { | ||
URI_IDENTIFIER.captures(&self.0).is_some() | ||
} | ||
} | ||
|
||
impl Validatable for CredentialDefinitionId { | ||
fn validate(&self) -> Result<(), Error> { | ||
if crate::utils::validation::URI_IDENTIFIER | ||
.captures(&self.0) | ||
.is_some() | ||
{ | ||
return Ok(()); | ||
} | ||
|
||
if LEGACY_CRED_DEF_IDENTIFIER.captures(&self.0).is_some() { | ||
return Ok(()); | ||
} | ||
|
||
Err(crate::Error::from_msg( | ||
crate::ErrorKind::ConversionError, | ||
format!( | ||
"type: {}, identifier: {} is invalid. It MUST be a URI or legacy identifier.", | ||
"CredentialDefinitionId", self.0 | ||
), | ||
)) | ||
} | ||
} | ||
|
||
impl From<CredentialDefinitionId> for String { | ||
fn from(i: CredentialDefinitionId) -> Self { | ||
i.0 | ||
} | ||
} | ||
|
||
impl TryFrom<String> for CredentialDefinitionId { | ||
type Error = Error; | ||
fn try_from(value: String) -> Result<Self, Self::Error> { | ||
CredentialDefinitionId::new(value) | ||
} | ||
} | ||
|
||
impl TryFrom<&str> for CredentialDefinitionId { | ||
type Error = Error; | ||
fn try_from(value: &str) -> Result<Self, Self::Error> { | ||
CredentialDefinitionId::new(value.to_owned()) | ||
} | ||
} | ||
|
||
impl std::fmt::Display for CredentialDefinitionId { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.0) | ||
} | ||
} |
77 changes: 75 additions & 2 deletions
77
aries/misc/anoncreds_types/src/data_types/identifiers/issuer_id.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
77 changes: 75 additions & 2 deletions
77
aries/misc/anoncreds_types/src/data_types/identifiers/rev_reg_def_id.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,76 @@ | ||
use crate::impl_anoncreds_object_identifier; | ||
use crate::{ | ||
error::Error, | ||
utils::validation::{Validatable, LEGACY_DID_IDENTIFIER, URI_IDENTIFIER}, | ||
}; | ||
|
||
impl_anoncreds_object_identifier!(RevocationRegistryDefinitionId); | ||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize, Default)] | ||
pub struct RevocationRegistryDefinitionId(pub String); | ||
|
||
impl RevocationRegistryDefinitionId { | ||
pub fn new_unchecked(s: impl Into<String>) -> Self { | ||
Self(s.into()) | ||
} | ||
|
||
pub fn new(s: impl Into<String>) -> Result<Self, Error> { | ||
let s = Self(s.into()); | ||
Validatable::validate(&s)?; | ||
Ok(s) | ||
} | ||
|
||
pub fn is_legacy(&self) -> bool { | ||
LEGACY_DID_IDENTIFIER.captures(&self.0).is_some() | ||
} | ||
|
||
pub fn is_uri(&self) -> bool { | ||
URI_IDENTIFIER.captures(&self.0).is_some() | ||
} | ||
} | ||
|
||
impl Validatable for RevocationRegistryDefinitionId { | ||
fn validate(&self) -> Result<(), Error> { | ||
if crate::utils::validation::URI_IDENTIFIER | ||
.captures(&self.0) | ||
.is_some() | ||
{ | ||
return Ok(()); | ||
} | ||
|
||
if LEGACY_DID_IDENTIFIER.captures(&self.0).is_some() { | ||
return Ok(()); | ||
} | ||
|
||
Err(crate::Error::from_msg( | ||
crate::ErrorKind::ConversionError, | ||
format!( | ||
"type: {}, identifier: {} is invalid. It MUST be a URI or legacy identifier.", | ||
"RevocationRegistryDefinitionId", self.0 | ||
), | ||
)) | ||
} | ||
} | ||
|
||
impl From<RevocationRegistryDefinitionId> for String { | ||
fn from(i: RevocationRegistryDefinitionId) -> Self { | ||
i.0 | ||
} | ||
} | ||
|
||
impl TryFrom<String> for RevocationRegistryDefinitionId { | ||
type Error = Error; | ||
fn try_from(value: String) -> Result<Self, Self::Error> { | ||
RevocationRegistryDefinitionId::new(value) | ||
} | ||
} | ||
|
||
impl TryFrom<&str> for RevocationRegistryDefinitionId { | ||
type Error = Error; | ||
fn try_from(value: &str) -> Result<Self, Self::Error> { | ||
RevocationRegistryDefinitionId::new(value.to_owned()) | ||
} | ||
} | ||
|
||
impl std::fmt::Display for RevocationRegistryDefinitionId { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.0) | ||
} | ||
} |
80 changes: 78 additions & 2 deletions
80
aries/misc/anoncreds_types/src/data_types/identifiers/schema_id.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,79 @@ | ||
use crate::impl_anoncreds_object_identifier; | ||
// use crate::impl_anoncreds_object_identifier; | ||
// | ||
// impl_anoncreds_object_identifier!(SchemaId); | ||
use crate::{ | ||
error::Error, | ||
utils::validation::{Validatable, LEGACY_SCHEMA_IDENTIFIER, URI_IDENTIFIER}, | ||
}; | ||
|
||
impl_anoncreds_object_identifier!(SchemaId); | ||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize, Default)] | ||
pub struct SchemaId(pub String); | ||
|
||
impl SchemaId { | ||
pub fn new_unchecked(s: impl Into<String>) -> Self { | ||
Self(s.into()) | ||
} | ||
|
||
pub fn new(s: impl Into<String>) -> Result<Self, Error> { | ||
let s = Self(s.into()); | ||
Validatable::validate(&s)?; | ||
Ok(s) | ||
} | ||
|
||
pub fn is_legacy(&self) -> bool { | ||
LEGACY_SCHEMA_IDENTIFIER.captures(&self.0).is_some() | ||
} | ||
|
||
pub fn is_uri(&self) -> bool { | ||
URI_IDENTIFIER.captures(&self.0).is_some() | ||
} | ||
} | ||
|
||
impl Validatable for SchemaId { | ||
fn validate(&self) -> Result<(), Error> { | ||
if crate::utils::validation::URI_IDENTIFIER | ||
.captures(&self.0) | ||
.is_some() | ||
{ | ||
return Ok(()); | ||
} | ||
|
||
if LEGACY_SCHEMA_IDENTIFIER.captures(&self.0).is_some() { | ||
return Ok(()); | ||
} | ||
|
||
Err(crate::Error::from_msg( | ||
crate::ErrorKind::ConversionError, | ||
format!( | ||
"type: {}, identifier: {} is invalid. It MUST be a URI or legacy identifier.", | ||
"SchemaId", self.0 | ||
), | ||
)) | ||
} | ||
} | ||
|
||
impl From<SchemaId> for String { | ||
fn from(i: SchemaId) -> Self { | ||
i.0 | ||
} | ||
} | ||
|
||
impl TryFrom<String> for SchemaId { | ||
type Error = Error; | ||
fn try_from(value: String) -> Result<Self, Self::Error> { | ||
SchemaId::new(value) | ||
} | ||
} | ||
|
||
impl TryFrom<&str> for SchemaId { | ||
type Error = Error; | ||
fn try_from(value: &str) -> Result<Self, Self::Error> { | ||
SchemaId::new(value.to_owned()) | ||
} | ||
} | ||
|
||
impl std::fmt::Display for SchemaId { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.0) | ||
} | ||
} |
Oops, something went wrong.