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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use super::*;
use crate::element::*;
use crate::error::{Error, Result};
use serde::{Deserialize, Serialize};
#[inline]
fn is_false(v: &bool) -> bool {
!v
}
#[inline]
fn u32_is_zero(v: &u32) -> bool {
*v == 0
}
#[inline]
fn u32_is_max(v: &u32) -> bool {
*v == u32::MAX
}
macro_rules! lockbox_validator {
($t: ty, $e: ident, $v: ident, $link:expr, $name:expr) => {
#[doc = "Validator for a [`"]
#[doc = $name]
#[doc = "`]["]
#[doc = $link]
#[doc = "].\n\n"]
#[doc = "This validator will only pass a "]
#[doc = $name]
#[doc = " value. Validation passes if:\n\n"]
#[doc = "- The number of bytes in the lockbox is less than or equal to `max_len`\n"]
#[doc = "- The number of bytes in the lockbox is greater than or equal to `min_len`\n"]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields, default)]
pub struct $v {
#[serde(skip_serializing_if = "String::is_empty")]
pub comment: String,
#[serde(skip_serializing_if = "u32_is_max")]
pub max_len: u32,
#[serde(skip_serializing_if = "u32_is_zero")]
pub min_len: u32,
#[serde(skip_serializing_if = "is_false")]
pub size: bool,
}
impl std::default::Default for $v {
fn default() -> Self {
Self {
comment: String::new(),
max_len: u32::MAX,
min_len: u32::MIN,
size: false,
}
}
}
impl $v {
pub fn new() -> Self {
Self::default()
}
pub fn comment(mut self, comment: impl Into<String>) -> Self {
self.comment = comment.into();
self
}
pub fn max_len(mut self, max_len: u32) -> Self {
self.max_len = max_len;
self
}
pub fn min_len(mut self, min_len: u32) -> Self {
self.min_len = min_len;
self
}
pub fn size(mut self, size: bool) -> Self {
self.size = size;
self
}
pub fn build(self) -> Validator {
Validator::$e(self)
}
pub(crate) fn validate(&self, parser: &mut Parser) -> Result<()> {
let elem = parser
.next()
.ok_or_else(|| Error::FailValidate(concat!("Expected a ",$name).to_string()))??;
let elem = if let Element::$e(v) = elem {
v
} else {
return Err(Error::FailValidate(format!(
concat!("Expected ", $name, ", got {}"),
elem.name()
)));
};
let len = elem.as_bytes().len() as u32;
if len > self.max_len {
return Err(Error::FailValidate(
concat!($name, " is longer than max_len").to_string()
));
}
if len < self.min_len {
return Err(Error::FailValidate(
concat!($name, " is shorter than min_len").to_string()
));
}
Ok(())
}
fn query_check_self(&self, other: &Self) -> bool {
self.size || (u32_is_max(&other.max_len) && u32_is_zero(&other.min_len))
}
pub(crate) fn query_check(&self, other: &Validator) -> bool {
match other {
Validator::$e(other) => self.query_check_self(other),
Validator::Multi(list) => list.iter().all(|other| match other {
Validator::$e(other) => self.query_check_self(other),
_ => false,
}),
Validator::Any => true,
_ => false,
}
}
}
};
($t: ty, $e: ident, $v: ident) => {
lockbox_validator!($t, $e, $v, concat!("fog_crypto::lockbox::", stringify!($t)), stringify!($t));
}
}
lockbox_validator!(DataLockbox, DataLockbox, DataLockboxValidator);
lockbox_validator!(IdentityLockbox, IdentityLockbox, IdentityLockboxValidator);
lockbox_validator!(StreamLockbox, StreamLockbox, StreamLockboxValidator);
lockbox_validator!(LockLockbox, LockLockbox, LockLockboxValidator);