icydb-core 0.180.37

IcyDB — A schema-first typed query engine and persistence runtime for Internet Computer canisters
Documentation
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Module: visitor::context
//! Responsibility: visitor issue-reporting context and path scoping helpers.
//! Does not own: concrete sanitize/validate traversal behavior.
//! Boundary: shared diagnostics context passed through visitor entrypoints.

use crate::{sanitize::SanitizeWriteContext, types::Decimal};
use serde::Deserialize;
use std::fmt;

///
/// VisitorContext
///
/// Narrow interface exposed to visitors for reporting non-fatal issues.
/// Implemented by adapters via a short-lived context object.
///

pub trait VisitorContext {
    fn add_issue(&mut self, issue: Issue);
    fn add_issue_at(&mut self, seg: PathSegment, issue: Issue);

    fn sanitize_write_context(&self) -> Option<SanitizeWriteContext> {
        None
    }
}

impl dyn VisitorContext + '_ {
    pub fn issue(&mut self, issue: impl Into<Issue>) {
        self.add_issue(issue.into());
    }

    pub fn issue_at(&mut self, seg: PathSegment, issue: impl Into<Issue>) {
        self.add_issue_at(seg, issue.into());
    }
}

/// VisitorContext that pins all issues to a single path segment.
pub struct ScopedContext<'a> {
    ctx: &'a mut dyn VisitorContext,
    seg: PathSegment,
}

impl<'a> ScopedContext<'a> {
    #[must_use]
    pub fn new(ctx: &'a mut dyn VisitorContext, seg: PathSegment) -> Self {
        Self { ctx, seg }
    }
}

impl VisitorContext for ScopedContext<'_> {
    fn add_issue(&mut self, issue: Issue) {
        self.ctx.add_issue_at(self.seg.clone(), issue);
    }

    fn add_issue_at(&mut self, _seg: PathSegment, issue: Issue) {
        self.ctx.add_issue_at(self.seg.clone(), issue);
    }

    fn sanitize_write_context(&self) -> Option<SanitizeWriteContext> {
        self.ctx.sanitize_write_context()
    }
}

///
/// Issue
///

#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
pub enum Issue {
    UlidNil,
    Float32NonFinite,
    Float64NonFinite,
    Utf8Invalid,
    LengthEqual {
        actual: usize,
        expected: usize,
    },
    LengthMin {
        actual: usize,
        min: usize,
    },
    LengthMax {
        actual: usize,
        max: usize,
    },
    LengthRange {
        actual: usize,
        min: usize,
        max: usize,
    },
    NumericNotRepresentableAsDecimal,
    NumericComparison {
        actual: Decimal,
        op: IssueComparisonOp,
        expected: Decimal,
    },
    NumericRange {
        actual: Decimal,
        min: Decimal,
        max: Decimal,
    },
    NumericMultipleOfZero,
    NumericMultipleOf {
        actual: Decimal,
        target: Decimal,
    },
    DecimalScaleMax {
        actual: u32,
        max: u32,
    },
    CollectionValueNotAllowed {
        allowed_count: usize,
    },
    Sha256Length {
        actual: usize,
    },
    Sha256NonHex,
    MimeSlashCount,
    MimeInvalidChars,
    UrlScheme,
    TextPattern {
        pattern: IssueTextPattern,
    },
    ColorHex {
        width: u8,
    },
    PhoneMissingPlus,
    PhoneDigitCount {
        digits: usize,
    },
    Iso3166CountryCode,
    Iso639LanguageCode,
    SanitizerRejected,
    UnspecifiedEnumVariant,
    Custom {
        message: String,
    },
}

impl Issue {
    #[must_use]
    pub const fn code(&self) -> IssueCode {
        match self {
            Self::UlidNil => IssueCode::UlidNil,
            Self::Float32NonFinite => IssueCode::Float32NonFinite,
            Self::Float64NonFinite => IssueCode::Float64NonFinite,
            Self::Utf8Invalid => IssueCode::Utf8Invalid,
            Self::LengthEqual { .. } => IssueCode::LengthEqual,
            Self::LengthMin { .. } => IssueCode::LengthMin,
            Self::LengthMax { .. } => IssueCode::LengthMax,
            Self::LengthRange { .. } => IssueCode::LengthRange,
            Self::NumericNotRepresentableAsDecimal => IssueCode::NumericNotRepresentableAsDecimal,
            Self::NumericComparison { .. } => IssueCode::NumericComparison,
            Self::NumericRange { .. } => IssueCode::NumericRange,
            Self::NumericMultipleOfZero => IssueCode::NumericMultipleOfZero,
            Self::NumericMultipleOf { .. } => IssueCode::NumericMultipleOf,
            Self::DecimalScaleMax { .. } => IssueCode::DecimalScaleMax,
            Self::CollectionValueNotAllowed { .. } => IssueCode::CollectionValueNotAllowed,
            Self::Sha256Length { .. } => IssueCode::Sha256Length,
            Self::Sha256NonHex => IssueCode::Sha256NonHex,
            Self::MimeSlashCount => IssueCode::MimeSlashCount,
            Self::MimeInvalidChars => IssueCode::MimeInvalidChars,
            Self::UrlScheme => IssueCode::UrlScheme,
            Self::TextPattern { .. } => IssueCode::TextPattern,
            Self::ColorHex { .. } => IssueCode::ColorHex,
            Self::PhoneMissingPlus => IssueCode::PhoneMissingPlus,
            Self::PhoneDigitCount { .. } => IssueCode::PhoneDigitCount,
            Self::Iso3166CountryCode => IssueCode::Iso3166CountryCode,
            Self::Iso639LanguageCode => IssueCode::Iso639LanguageCode,
            Self::SanitizerRejected => IssueCode::SanitizerRejected,
            Self::UnspecifiedEnumVariant => IssueCode::UnspecifiedEnumVariant,
            Self::Custom { .. } => IssueCode::Custom,
        }
    }

    #[must_use]
    pub fn custom(message: impl Into<String>) -> Self {
        Self::Custom {
            message: message.into(),
        }
    }
}

impl From<String> for Issue {
    fn from(message: String) -> Self {
        Self::Custom { message }
    }
}

impl From<&str> for Issue {
    fn from(message: &str) -> Self {
        Self::Custom {
            message: message.to_string(),
        }
    }
}

impl fmt::Display for Issue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UlidNil => f.write_str("ulid must not be nil"),
            Self::Float32NonFinite => f.write_str("Float32 must be finite"),
            Self::Float64NonFinite => f.write_str("Float64 must be finite"),
            Self::Utf8Invalid => f.write_str("bytes must be valid UTF-8"),
            Self::LengthEqual { actual, expected } => {
                write!(f, "length {actual} must equal {expected}")
            }
            Self::LengthMin { actual, min } => {
                write!(f, "length {actual} must be at least {min}")
            }
            Self::LengthMax { actual, max } => {
                write!(f, "length {actual} must be at most {max}")
            }
            Self::LengthRange { actual, min, max } => {
                write!(f, "length {actual} must be between {min} and {max}")
            }
            Self::NumericNotRepresentableAsDecimal => {
                f.write_str("numeric value cannot be represented as Decimal")
            }
            Self::NumericComparison {
                actual,
                op,
                expected,
            } => write!(
                f,
                "numeric value {actual} must be {} {expected}",
                op.symbol()
            ),
            Self::NumericRange { actual, min, max } => {
                write!(f, "numeric value {actual} must be between {min} and {max}")
            }
            Self::NumericMultipleOfZero => f.write_str("multipleOf target must be non-zero"),
            Self::NumericMultipleOf { actual, target } => {
                write!(f, "numeric value {actual} must be a multiple of {target}")
            }
            Self::DecimalScaleMax { actual, max } => {
                write!(f, "decimal scale {actual} must be at most {max}")
            }
            Self::CollectionValueNotAllowed { allowed_count } => {
                write!(f, "value must be one of {allowed_count} allowed values")
            }
            Self::Sha256Length { actual } => {
                write!(f, "SHA-256 hex digest length {actual} must be 64")
            }
            Self::Sha256NonHex => {
                f.write_str("SHA-256 digest must contain only hexadecimal characters")
            }
            Self::MimeSlashCount => f.write_str("MIME type must contain exactly one slash"),
            Self::MimeInvalidChars => f.write_str("MIME type contains invalid token characters"),
            Self::UrlScheme => f.write_str("URL must start with http:// or https://"),
            Self::TextPattern { pattern } => write!(f, "text must be {}", pattern.label()),
            Self::ColorHex { width } => {
                write!(f, "hex color must contain {width} hexadecimal digits")
            }
            Self::PhoneMissingPlus => f.write_str("phone number must start with +"),
            Self::PhoneDigitCount { digits } => {
                write!(f, "phone number has {digits} digits; expected 7 to 15")
            }
            Self::Iso3166CountryCode => {
                f.write_str("value must be an ISO 3166-1 alpha-2 country code")
            }
            Self::Iso639LanguageCode => f.write_str("value must be an ISO 639-1 language code"),
            Self::SanitizerRejected => f.write_str("sanitizer rejected value"),
            Self::UnspecifiedEnumVariant => f.write_str("unspecified enum variant is not valid"),
            Self::Custom { message } => f.write_str(message),
        }
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
#[repr(u16)]
pub enum IssueCode {
    UlidNil = 1,
    Float32NonFinite = 2,
    Float64NonFinite = 3,
    Utf8Invalid = 4,
    LengthEqual = 5,
    LengthMin = 6,
    LengthMax = 7,
    LengthRange = 8,
    NumericNotRepresentableAsDecimal = 9,
    NumericComparison = 10,
    NumericRange = 11,
    NumericMultipleOfZero = 12,
    NumericMultipleOf = 13,
    DecimalScaleMax = 14,
    CollectionValueNotAllowed = 15,
    Sha256Length = 16,
    Sha256NonHex = 17,
    MimeSlashCount = 18,
    MimeInvalidChars = 19,
    UrlScheme = 20,
    TextPattern = 21,
    ColorHex = 22,
    PhoneMissingPlus = 23,
    PhoneDigitCount = 24,
    Iso3166CountryCode = 25,
    Iso639LanguageCode = 26,
    SanitizerRejected = 27,
    UnspecifiedEnumVariant = 28,
    Custom = 29,
}

impl IssueCode {
    #[must_use]
    pub const fn raw(self) -> u16 {
        self as u16
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
#[repr(u8)]
pub enum IssueComparisonOp {
    Lt,
    Gt,
    Lte,
    Gte,
    Eq,
    Ne,
}

impl IssueComparisonOp {
    #[must_use]
    pub const fn symbol(self) -> &'static str {
        match self {
            Self::Lt => "<",
            Self::Gt => ">",
            Self::Lte => "<=",
            Self::Gte => ">=",
            Self::Eq => "==",
            Self::Ne => "!=",
        }
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
#[repr(u8)]
pub enum IssueTextPattern {
    AlphabeticUnderscore,
    AlphanumericUnderscore,
    Ascii,
    Camel,
    Kebab,
    Lower,
    LowerUnderscore,
    Sentence,
    Snake,
    Title,
    Upper,
    UpperCamel,
    UpperKebab,
    UpperSnake,
}

impl IssueTextPattern {
    #[must_use]
    pub const fn label(self) -> &'static str {
        match self {
            Self::AlphabeticUnderscore => "alphabetic or underscore",
            Self::AlphanumericUnderscore => "alphanumeric or underscore",
            Self::Ascii => "ASCII",
            Self::Camel => "camel case",
            Self::Kebab => "kebab case",
            Self::Lower => "lower case",
            Self::LowerUnderscore => "lowercase or underscore",
            Self::Sentence => "sentence case",
            Self::Snake => "snake case",
            Self::Title => "title case",
            Self::Upper => "upper case",
            Self::UpperCamel => "upper camel case",
            Self::UpperKebab => "upper kebab case",
            Self::UpperSnake => "upper snake case",
        }
    }
}

///
/// PathSegment
///

#[derive(Clone, Debug)]
pub enum PathSegment {
    Empty,
    Field(&'static str),
    Index(usize),
}

impl From<&'static str> for PathSegment {
    fn from(s: &'static str) -> Self {
        Self::Field(s)
    }
}

impl From<usize> for PathSegment {
    fn from(i: usize) -> Self {
        Self::Index(i)
    }
}

impl From<Option<&'static str>> for PathSegment {
    fn from(opt: Option<&'static str>) -> Self {
        match opt {
            Some(s) if !s.is_empty() => Self::Field(s),
            _ => Self::Empty,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{Issue, IssueCode, IssueComparisonOp, IssueTextPattern};

    #[test]
    fn custom_issue_preserves_message() {
        let issue = Issue::from("pet name is reserved");

        assert_eq!(issue.code(), IssueCode::Custom);
        assert_eq!(issue.to_string(), "pet name is reserved");
    }

    #[test]
    fn structured_issue_renders_context() {
        let issue = Issue::LengthRange {
            actual: 8,
            min: 10,
            max: 30,
        };

        assert_eq!(issue.to_string(), "length 8 must be between 10 and 30");
    }

    #[test]
    fn numeric_issue_renders_operator_and_values() {
        let issue = Issue::NumericComparison {
            actual: "1".parse().expect("valid decimal"),
            op: IssueComparisonOp::Gte,
            expected: "2".parse().expect("valid decimal"),
        };

        assert_eq!(issue.to_string(), "numeric value 1 must be >= 2");
    }

    #[test]
    fn text_pattern_issue_renders_pattern_name() {
        let issue = Issue::TextPattern {
            pattern: IssueTextPattern::UpperSnake,
        };

        assert_eq!(issue.to_string(), "text must be upper snake case");
    }
}