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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! Bounded source-level constraint expressions.
use std::collections::BTreeSet;
use crate::{
Account, Blob, Date, Decimal, Duration, FieldSourceKey, Float32, Float64,
MAX_PROPOSAL_LITERAL_BYTES, MAX_SOURCE_CHECK_INSTRUCTIONS, Principal, ScalarKind,
SchemaContractError, Subaccount, Timestamp, TypeSourceKey, U256, Ulid, Unit,
};
/// One canonical scalar literal carried by a schema proposal.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ScalarLiteral {
/// Account identifier.
Account(Account),
/// Bounded binary value.
Blob(Blob),
/// Boolean value.
Bool(bool),
/// Days since the Unix epoch.
Date(Date),
/// Canonical fixed-point decimal.
Decimal(Decimal),
/// Millisecond duration.
Duration(Duration),
/// Source-keyed unit-enum variant.
EnumUnit {
/// Immutable enum type identity.
enum_type: TypeSourceKey,
/// Immutable unit-variant identity.
variant: TypeSourceKey,
},
/// Finite 32-bit float.
Float32(Float32),
/// Finite 64-bit float.
Float64(Float64),
/// Signed fixed-width integer.
Int(i128),
/// Unsigned fixed-width integer.
Nat(u128),
/// Fixed-width unsigned 256-bit integer.
U256(U256),
/// Principal value.
Principal(Principal),
/// Fixed-width subaccount.
Subaccount(Subaccount),
/// Bounded text value.
Text(String),
/// Unix-millisecond timestamp.
Timestamp(Timestamp),
/// Canonical ULID.
Ulid(Ulid),
/// Explicit unit value.
Unit(Unit),
}
impl ScalarLiteral {
/// Return the declared scalar kind represented by this literal.
#[must_use]
pub const fn kind(&self) -> ScalarKind {
match self {
Self::Account(_) => ScalarKind::Account,
Self::Blob(_) => ScalarKind::Blob,
Self::Bool(_) => ScalarKind::Bool,
Self::Date(_) => ScalarKind::Date,
Self::Decimal(_) => ScalarKind::Decimal,
Self::Duration(_) => ScalarKind::Duration,
Self::EnumUnit { .. } => ScalarKind::Enum,
Self::Float32(_) => ScalarKind::Float32,
Self::Float64(_) => ScalarKind::Float64,
Self::Int(_) => ScalarKind::Int128,
Self::Nat(_) => ScalarKind::Nat128,
Self::U256(_) => ScalarKind::U256,
Self::Principal(_) => ScalarKind::Principal,
Self::Subaccount(_) => ScalarKind::Subaccount,
Self::Text(_) => ScalarKind::Text,
Self::Timestamp(_) => ScalarKind::Timestamp,
Self::Ulid(_) => ScalarKind::Ulid,
Self::Unit(_) => ScalarKind::Unit,
}
}
pub(crate) const fn validate(&self) -> Result<(), SchemaContractError> {
match self {
Self::Blob(value) if value.len() > MAX_PROPOSAL_LITERAL_BYTES => {
Err(SchemaContractError::InvalidLiteral)
}
Self::Text(value) if value.len() > MAX_PROPOSAL_LITERAL_BYTES => {
Err(SchemaContractError::InvalidLiteral)
}
_ => Ok(()),
}
}
}
/// One instruction in a bounded source-level postfix check expression.
///
/// This is an AST transport, not accepted bytecode: field references remain
/// typed current-name keys and IcyDB still owns accepted binding and compilation.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum SourceCheckInstruction {
/// Push one field value.
Field(FieldSourceKey),
/// Push one admitted proposal literal.
Literal(ScalarLiteral),
/// SQL equality.
Equal,
/// SQL inequality.
NotEqual,
/// SQL less-than.
LessThan,
/// SQL less-than-or-equal.
LessThanOrEqual,
/// SQL greater-than.
GreaterThan,
/// SQL greater-than-or-equal.
GreaterThanOrEqual,
/// SQL three-valued conjunction.
And,
/// SQL three-valued disjunction.
Or,
/// SQL three-valued negation.
Not,
/// Two-valued null test.
IsNull,
/// Two-valued non-null test.
IsNotNull,
/// Bounded scalar/collection length.
Length,
}
/// Bounded canonical source expression.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SourceCheckExpr {
instructions: Vec<SourceCheckInstruction>,
}
impl SourceCheckExpr {
/// Construct and validate one source expression.
///
/// # Errors
///
/// Returns a typed expression error for empty, oversized, malformed-stack,
/// or invalid-literal input.
pub fn try_new(instructions: Vec<SourceCheckInstruction>) -> Result<Self, SchemaContractError> {
let expression = Self { instructions };
expression.validate()?;
Ok(expression)
}
/// Borrow canonical postfix instructions.
#[must_use]
pub fn instructions(&self) -> &[SourceCheckInstruction] {
&self.instructions
}
/// Derive referenced field source keys from the expression.
#[must_use]
pub fn dependencies(&self) -> BTreeSet<FieldSourceKey> {
self.instructions
.iter()
.filter_map(|instruction| match instruction {
SourceCheckInstruction::Field(field) => Some(field.clone()),
_ => None,
})
.collect()
}
pub(crate) fn validate(&self) -> Result<(), SchemaContractError> {
if self.instructions.is_empty() || self.instructions.len() > MAX_SOURCE_CHECK_INSTRUCTIONS {
return Err(SchemaContractError::InvalidExpression);
}
let mut stack_depth = 0usize;
for instruction in &self.instructions {
match instruction {
SourceCheckInstruction::Field(_) => {
stack_depth = stack_depth
.checked_add(1)
.ok_or(SchemaContractError::InvalidExpression)?;
}
SourceCheckInstruction::Literal(literal) => {
literal.validate()?;
stack_depth = stack_depth
.checked_add(1)
.ok_or(SchemaContractError::InvalidExpression)?;
}
SourceCheckInstruction::Not
| SourceCheckInstruction::IsNull
| SourceCheckInstruction::IsNotNull
| SourceCheckInstruction::Length => {
if stack_depth < 1 {
return Err(SchemaContractError::InvalidExpression);
}
}
SourceCheckInstruction::Equal
| SourceCheckInstruction::NotEqual
| SourceCheckInstruction::LessThan
| SourceCheckInstruction::LessThanOrEqual
| SourceCheckInstruction::GreaterThan
| SourceCheckInstruction::GreaterThanOrEqual
| SourceCheckInstruction::And
| SourceCheckInstruction::Or => {
if stack_depth < 2 {
return Err(SchemaContractError::InvalidExpression);
}
stack_depth -= 1;
}
}
}
if stack_depth != 1 {
return Err(SchemaContractError::InvalidExpression);
}
Ok(())
}
}