Repository navigation
Expand file tree
/
Copy pathvalue.rs
More file actions
392 lines (365 loc) · 13.2 KB
/
Copy pathvalue.rs
File metadata and controls
392 lines (365 loc) · 13.2 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
//! Managed interpreter values. Authority is deliberately not a value variant.
use std::collections::BTreeMap;
use std::fmt;
use crate::error::{Error, Result};
#[derive(serde::Serialize, Clone, Debug, PartialEq, Eq)]
pub(crate) enum Type {
Int,
Decimal,
Timestamp,
Duration,
Bool,
Text,
BoundedText(usize, usize),
Unit,
Html,
Url,
Response,
Id(String),
Blob(String),
Secret(String),
Stream(String),
Record(String),
Enum(String),
List(Box<Self>),
DataError,
Result(Box<Self>),
AppResult(Box<Self>, Box<Self>),
Action(Box<Self>, Box<Self>),
Success(Box<Self>),
Option(Box<Self>),
Never,
}
impl Type {
pub fn accepts(&self, other: &Self) -> bool {
self == other
|| matches!(
(self, other),
(Self::Response, Self::Html | Self::Url | Self::Text)
)
|| matches!(other, Self::Never)
|| matches!((self, other), (Self::Text, Self::BoundedText(_, _)))
|| match (self, other) {
(Self::AppResult(a, e), Self::AppResult(b, f))
| (Self::Action(a, e), Self::Action(b, f)) => a.accepts(b) && e.accepts(f),
(Self::AppResult(a, _) | Self::Result(a) | Self::Success(a), Self::Success(b))
| (Self::Result(a), Self::Result(b))
| (Self::Option(a), Self::Option(b))
| (Self::List(a), Self::List(b)) => a.accepts(b),
_ => false,
}
}
}
impl fmt::Display for Type {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Int => f.write_str("Int"),
Self::Decimal => f.write_str("Decimal"),
Self::Timestamp => f.write_str("Timestamp"),
Self::Duration => f.write_str("Duration"),
Self::AppResult(a, e) => write!(f, "Result<{a}, {e}>"),
Self::Action(a, e) => write!(f, "Action<{a}, {e}>"),
Self::Success(a) => write!(f, "Result<{a}, inferred error>"),
Self::Bool => f.write_str("Bool"),
Self::Text => f.write_str("Text"),
Self::BoundedText(a, b) => write!(f, "Text<length: {a}..{b}>"),
Self::Unit => f.write_str("Unit"),
Self::Html => f.write_str("Html"),
Self::Url => f.write_str("Url"),
Self::Response => f.write_str("Response"),
Self::Secret(tag) => write!(f, "Secret<{tag:?}>"),
Self::Blob(binding) => write!(f, "Blob<{binding:?}>"),
Self::Stream(channel) => write!(f, "Stream<{channel:?}>"),
Self::Id(tag) => write!(f, "Id<{tag:?}>"),
Self::Record(name) | Self::Enum(name) => f.write_str(name),
Self::List(ty) => write!(f, "List<{ty}>"),
Self::DataError => f.write_str("DataError"),
Self::Result(ty) => write!(f, "Result<{ty}, DataError>"),
Self::Option(ty) => write!(f, "Option<{ty}>"),
Self::Never => f.write_str("Never"),
}
}
}
/// An immutable result returned by a checked Diamond program.
///
/// ```
/// let program = diamond::compile("fn main() -> Int:\n 6 * 7\n")?;
/// assert_eq!(program.evaluate()?.display_text()?, "42");
/// # Ok::<(), diamond::Error>(())
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Value(pub(crate) ValueKind);
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum ValueKind {
Int(i64),
Decimal(i64),
Timestamp(i64),
Duration(i64),
Bool(bool),
Text(String),
Unit,
Html(String),
Url(String),
Response(u16, String),
Id(String, String),
Stream(String, String),
Secret(crate::integrations::SecretHandle),
Record(String, BTreeMap<String, Value>),
Variant(String, String, Vec<Value>),
List(Vec<Value>),
DataError(Failure),
Ok(Box<Value>),
AppErr(Box<Value>),
Err(Failure),
Some(Box<Value>),
None,
}
/// Expected failures retain their safe transport class while used in Diamond.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum Failure {
Denied,
InvalidInput,
Aborted,
Conflict,
Message(String),
}
impl Failure {
pub fn from_error(error: Error) -> Result<Self> {
match error {
Error::Denied => Ok(Self::Denied),
Error::Input(_) => Ok(Self::InvalidInput),
Error::Aborted => Ok(Self::Aborted),
Error::Conflict => Ok(Self::Conflict),
Error::Propagation(message) => Ok(Self::Message(message)),
other => Err(other),
}
}
pub fn into_error(self) -> Error {
match self {
Self::Denied => Error::Denied,
Self::InvalidInput => {
Error::Input("input does not satisfy its declared shape or bounds".into())
}
Self::Aborted => Error::Aborted,
Self::Conflict => Error::Conflict,
Self::Message(message) => Error::Propagation(message),
}
}
pub fn message(&self) -> &str {
match self {
Self::Denied => "Access denied or resource not found.",
Self::InvalidInput => "Input does not satisfy its declared shape or bounds.",
Self::Aborted => "Transaction aborted.",
Self::Conflict => "Transaction aborted by a concurrency conflict. Retry explicitly.",
Self::Message(message) => message,
}
}
}
// Weight units include conservative per-node/map overhead and owned bytes. These
// are interpreter work limits, not an allocator accounting or host-memory quota.
pub(crate) const VALUE_NODE_WEIGHT: usize = 128;
const MAX_VALUE_WEIGHT: usize = 4 * 1_048_576;
const MAX_VALUE_DEPTH: usize = 64;
const EVALUATION_VALUE_WORK: usize = 64 * 1_048_576;
#[derive(Debug)]
pub(crate) struct ValueBudget {
remaining: usize,
}
impl ValueBudget {
pub fn new() -> Self {
Self {
remaining: EVALUATION_VALUE_WORK,
}
}
pub fn check_weight(weight: usize) -> Result<()> {
if weight > MAX_VALUE_WEIGHT {
return Err(Error::Limit("managed value size"));
}
Ok(())
}
pub fn spend(&mut self, weight: usize) -> Result<()> {
self.remaining = self
.remaining
.checked_sub(weight)
.ok_or(Error::Limit("managed value work"))?;
Ok(())
}
pub fn charge(&mut self, value: &Value) -> Result<()> {
self.spend(value.weight()?)
}
pub fn copy(&mut self, value: &Value) -> Result<Value> {
self.charge(value)?;
Ok(value.clone())
}
pub fn copy_fields(
&mut self,
values: &BTreeMap<String, Value>,
) -> Result<BTreeMap<String, Value>> {
let mut copied = BTreeMap::new();
for (name, value) in values {
self.spend(VALUE_NODE_WEIGHT + name.len())?;
copied.insert(name.clone(), self.copy(value)?);
}
Ok(copied)
}
}
fn failure_weight(failure: &Failure) -> usize {
if let Failure::Message(message) = failure {
message.len()
} else {
0
}
}
fn value_weight(value: &Value, depth: usize, total: &mut usize) -> Result<()> {
if depth > MAX_VALUE_DEPTH {
return Err(Error::Limit("managed value depth"));
}
*total = total.saturating_add(VALUE_NODE_WEIGHT);
match &value.0 {
ValueKind::Text(text)
| ValueKind::Html(text)
| ValueKind::Url(text)
| ValueKind::Response(_, text) => *total = total.saturating_add(text.len()),
ValueKind::Id(tag, id) | ValueKind::Stream(tag, id) => {
*total = total.saturating_add(tag.len() + id.len());
}
ValueKind::Record(tag, fields) => {
*total = total.saturating_add(tag.len());
for (name, field) in fields {
*total = total.saturating_add(name.len());
ValueBudget::check_weight(*total)?;
value_weight(field, depth + 1, total)?;
}
}
ValueKind::Variant(tag, name, values) => {
*total = total.saturating_add(tag.len() + name.len());
for value in values {
value_weight(value, depth + 1, total)?;
}
}
ValueKind::List(values) => {
for value in values {
value_weight(value, depth + 1, total)?;
}
}
ValueKind::AppErr(value) | ValueKind::Ok(value) | ValueKind::Some(value) => {
value_weight(value, depth + 1, total)?;
}
ValueKind::DataError(failure) | ValueKind::Err(failure) => {
*total = total.saturating_add(failure_weight(failure));
}
ValueKind::Secret(_)
| ValueKind::Decimal(_)
| ValueKind::Timestamp(_)
| ValueKind::Duration(_)
| ValueKind::Int(_)
| ValueKind::Bool(_)
| ValueKind::Unit
| ValueKind::None => {}
}
ValueBudget::check_weight(*total)
}
impl Value {
pub(crate) fn weight(&self) -> Result<usize> {
let mut weight = 0;
value_weight(self, 1, &mut weight)?;
Ok(weight)
}
/// Unwrap a Diamond Result returned by `main`, preserving an explicit error.
/// Ordinary non-Result values pass through unchanged.
///
/// # Errors
/// Returns a propagated Diamond error for `Err` values.
pub fn into_result(self) -> Result<Self> {
match self.0 {
ValueKind::Ok(value) => Ok(*value),
ValueKind::Err(failure) => Err(failure.into_error()),
ValueKind::AppErr(value) => Err(Error::Application(*value)),
_ => Ok(self),
}
}
pub(crate) fn operation(result: Result<Self>) -> Result<Self> {
Ok(Self(match result {
Ok(value) => ValueKind::Ok(Box::new(value)),
Err(error) => ValueKind::Err(Failure::from_error(error)?),
}))
}
/// Return a scalar's human-readable representation without implicit coercion in Diamond.
///
/// # Errors
/// Returns an input error for records and compound values.
pub fn display_text(&self) -> Result<String> {
match &self.0 {
ValueKind::Int(value) => Ok(value.to_string()),
ValueKind::Bool(value) => Ok(value.to_string()),
ValueKind::Text(value)
| ValueKind::Html(value)
| ValueKind::Url(value)
| ValueKind::Id(_, value) => Ok(value.clone()),
ValueKind::Unit => Ok(String::new()),
_ => Err(Error::Input(
"compound values need an explicit representation".into(),
)),
}
}
pub(crate) fn boolean(&self) -> Result<bool> {
match self.0 {
ValueKind::Bool(value) => Ok(value),
_ => Err(Error::Input("expected Bool".into())),
}
}
pub(crate) fn fits(&self, ty: &Type) -> bool {
match (&self.0, ty) {
(ValueKind::Variant(enumeration, tag, fields), Type::Action(success, error)) => {
enumeration == crate::action::ENUM
&& crate::action::cases(success, error)
.get(tag)
.is_some_and(|types| {
fields.len() == types.len()
&& fields.iter().zip(types).all(|(value, ty)| value.fits(ty))
})
}
(ValueKind::Decimal(_), Type::Decimal)
| (ValueKind::Timestamp(_), Type::Timestamp)
| (ValueKind::Duration(_), Type::Duration)
| (ValueKind::Int(_), Type::Int)
| (ValueKind::Bool(_), Type::Bool)
| (ValueKind::Text(_), Type::Text)
| (ValueKind::Unit, Type::Unit)
| (ValueKind::Html(_), Type::Html)
| (ValueKind::Url(_), Type::Url)
| (ValueKind::None, Type::Option(_))
| (ValueKind::Err(_), Type::Result(_))
| (ValueKind::DataError(_), Type::DataError)
| (
ValueKind::Response(_, _)
| ValueKind::Html(_)
| ValueKind::Url(_)
| ValueKind::Text(_),
Type::Response,
) => true,
(ValueKind::Text(value), Type::BoundedText(minimum, maximum)) => {
(*minimum..=*maximum).contains(&value.chars().count())
}
(ValueKind::Secret(handle), Type::Secret(expected)) => handle.name == *expected,
(ValueKind::Stream(channel, parent), Type::Stream(expected)) => {
channel == expected && (parent.is_empty() || crate::auth::valid_id(parent))
}
(ValueKind::Id(tag, value), Type::Id(expected)) => {
tag == expected && crate::auth::valid_id(value)
}
(ValueKind::Record(tag, _), Type::Record(expected))
| (ValueKind::Variant(tag, _, _), Type::Enum(expected)) => tag == expected,
(ValueKind::List(values), Type::List(inner)) => {
values.iter().all(|value| value.fits(inner))
}
(ValueKind::AppErr(value), Type::AppResult(_, error)) => value.fits(error),
(ValueKind::Ok(value), Type::AppResult(inner, _) | Type::Success(inner)) => {
value.fits(inner)
}
(ValueKind::Some(value), Type::Option(inner))
| (ValueKind::Ok(value), Type::Result(inner)) => value.fits(inner),
_ => false,
}
}
}