Skip to content

Commit

Permalink
Remove identifier macro (#1128)
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Kovar <[email protected]>
  • Loading branch information
mirgee authored Feb 21, 2024
1 parent bc10bae commit cfc29ff
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 123 deletions.
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 aries/misc/anoncreds_types/src/data_types/identifiers/issuer_id.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,79 @@
use crate::impl_anoncreds_object_identifier;
use crate::{
error::Error,
utils::validation::{Validatable, LEGACY_DID_IDENTIFIER, URI_IDENTIFIER},
};

impl_anoncreds_object_identifier!(IssuerId);
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize, Default)]
pub struct IssuerId(pub String);

impl IssuerId {
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 IssuerId {
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.",
"IssuerId", self.0
),
))
}
}

impl From<IssuerId> for String {
fn from(i: IssuerId) -> Self {
i.0
}
}

impl TryFrom<String> for IssuerId {
type Error = Error;
fn try_from(value: String) -> Result<Self, Self::Error> {
IssuerId::new(value)
}
}

impl TryFrom<&str> for IssuerId {
type Error = Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
IssuerId::new(value.to_owned())
}
}

impl std::fmt::Display for IssuerId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

#[cfg(test)]
mod test_issuer_identifiers {
Expand Down
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 aries/misc/anoncreds_types/src/data_types/identifiers/schema_id.rs
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)
}
}
Loading

0 comments on commit cfc29ff

Please sign in to comment.