-
Notifications
You must be signed in to change notification settings - Fork 55
/
macros.rs
107 lines (89 loc) · 3.45 KB
/
macros.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#[macro_export]
macro_rules! impl_anoncreds_object_identifier {
($i:ident) => {
use $crate::error::ValidationError;
use $crate::utils::validation::{
Validatable, LEGACY_CRED_DEF_IDENTIFIER, LEGACY_DID_IDENTIFIER,
LEGACY_REV_REG_DEF_IDENTIFIER, LEGACY_SCHEMA_IDENTIFIER, URI_IDENTIFIER,
};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize, Default)]
pub struct $i(pub String);
impl $i {
pub fn new_unchecked(s: impl Into<String>) -> Self {
Self(s.into())
}
pub fn new(s: impl Into<String>) -> Result<Self, ValidationError> {
let s = Self(s.into());
Validatable::validate(&s)?;
Ok(s)
}
pub fn is_legacy_did_identifier(&self) -> bool {
LEGACY_DID_IDENTIFIER.captures(&self.0).is_some()
}
pub fn is_legacy_cred_def_identifier(&self) -> bool {
LEGACY_CRED_DEF_IDENTIFIER.captures(&self.0).is_some()
}
pub fn is_legacy_rev_reg_def_identifier(&self) -> bool {
LEGACY_REV_REG_DEF_IDENTIFIER.captures(&self.0).is_some()
}
pub fn is_legacy_schema_identifier(&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 $i {
fn validate(&self) -> Result<(), ValidationError> {
let legacy_regex = match stringify!($i) {
"IssuerId" => &LEGACY_DID_IDENTIFIER,
"CredentialDefinitionId" => &LEGACY_CRED_DEF_IDENTIFIER,
"SchemaId" => &LEGACY_SCHEMA_IDENTIFIER,
"RevocationRegistryDefinitionId" => &LEGACY_REV_REG_DEF_IDENTIFIER,
invalid_name => {
return Err($crate::invalid!(
"type: {} does not have a validation regex",
invalid_name,
))
}
};
if $crate::utils::validation::URI_IDENTIFIER
.captures(&self.0)
.is_some()
{
return Ok(());
}
if legacy_regex.captures(&self.0).is_some() {
return Ok(());
}
Err($crate::invalid!(
"type: {}, identifier: {} is invalid. It MUST be a URI or legacy identifier.",
stringify!($i),
self.0
))
}
}
impl From<$i> for String {
fn from(i: $i) -> Self {
i.0
}
}
impl TryFrom<String> for $i {
type Error = ValidationError;
fn try_from(value: String) -> Result<Self, Self::Error> {
$i::new(value)
}
}
impl TryFrom<&str> for $i {
type Error = ValidationError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
$i::new(value.to_owned())
}
}
impl std::fmt::Display for $i {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
};
}