datumesh-core 0.3.1

Datumesh protocol core: strict JSON and JCS, canonical forms, cell URIs, receipts, keys, envelopes and offline verification.
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//! Lexical forms: names (2), scalars (3), hex, licences, key references (9.1) and units (7.1).
//!
//! Every check takes the path of the value it checks, for the error it may return. Each value
//! is checked against its lexical form first and its length limit second (SPEC 4.5).

use crate::error::{err, ErrorCode, Result};
use crate::tables;

/// Limits of SPEC 4.5.
pub mod limits {
    /// Characters in a function name, path segment, argument name or code language.
    pub const NAME: usize = 64;
    /// Characters in `<namespace>/<function>`.
    pub const FUNCTION_NAME: usize = 512;
    /// Arguments per function or cell.
    pub const ARGS: usize = 32;
    /// Characters in a number.
    pub const NUMBER: usize = 128;
    /// UTF-8 bytes in a string value.
    pub const STRING: usize = 1024;
    /// UTF-8 bytes in a receipt summary.
    pub const SUMMARY: usize = 1024;
    /// UTF-8 bytes in a function body.
    pub const BODY: usize = 1_048_576;
    /// Entries in a receipt's or descriptor's `inputs`.
    pub const INPUTS: usize = 4096;
    /// Characters in an SPDX licence identifier.
    pub const LICENCE_SPDX: usize = 64;
    /// Characters in a licence URL.
    pub const LICENCE_URL: usize = 512;
    /// Keys in one key publication.
    pub const KEYS: usize = 64;
    /// Characters in a blob media type (3.7).
    pub const MEDIA_TYPE: usize = 128;
    /// Characters in an index unit (`units.md` 2.4).
    pub const INDEX_UNIT: usize = 64;
    /// The largest blob length, 2^53 - 1.
    pub const BLOB_LENGTH: u64 = 9_007_199_254_740_991;
}

fn is_lower_digit(c: u8) -> bool {
    c.is_ascii_lowercase() || c.is_ascii_digit()
}

/// The grammar of 2.2 without its length limit.
pub(crate) fn is_name_grammar(s: &str) -> bool {
    let b = s.as_bytes();
    match b.first() {
        Some(c) if c.is_ascii_lowercase() => {}
        _ => return false,
    }
    let mut prev_underscore = false;
    for &c in &b[1..] {
        if c == b'_' {
            if prev_underscore {
                return false;
            }
            prev_underscore = true;
        } else if is_lower_digit(c) {
            prev_underscore = false;
        } else {
            return false;
        }
    }
    !prev_underscore
}

/// A name (2.2): function names, path segments and code languages. `E_CANON_NAME`, then `E_CANON_LIMIT`.
pub fn check_name(s: &str, path: &str) -> Result<()> {
    if !is_name_grammar(s) {
        return err(ErrorCode::CanonName, path, format!("not a lowercase snake_case name: {s:?}"));
    }
    if s.len() > limits::NAME {
        return err(ErrorCode::CanonLimit, path, "name longer than 64 characters");
    }
    Ok(())
}

/// An argument name (2.4): grammar (`E_CANON_ARG_NAME`), length (`E_CANON_LIMIT`), then reserved names.
pub fn check_arg_name(s: &str, path: &str) -> Result<()> {
    if !is_name_grammar(s) {
        return err(ErrorCode::CanonArgName, path, format!("not a lowercase snake_case name: {s:?}"));
    }
    if s.len() > limits::NAME {
        return err(ErrorCode::CanonLimit, path, "name longer than 64 characters");
    }
    if s == "asof" || s.starts_with("dm_") {
        return err(ErrorCode::CanonArgName, path, format!("reserved argument name {s:?}"));
    }
    Ok(())
}

/// A DNS name (2.1). Every failure is `E_CANON_NAME`.
pub fn check_dns_name(s: &str, path: &str) -> Result<()> {
    if s.len() > 253 {
        return err(ErrorCode::CanonName, path, "DNS name longer than 253 characters");
    }
    let labels: Vec<&str> = s.split('.').collect();
    if labels.len() < 2 {
        return err(ErrorCode::CanonName, path, format!("DNS name needs at least two labels: {s:?}"));
    }
    for l in &labels {
        let b = l.as_bytes();
        let ok = !b.is_empty()
            && b.len() <= 63
            && b.iter().all(|&c| is_lower_digit(c) || c == b'-')
            && b[0] != b'-'
            && b[b.len() - 1] != b'-';
        if !ok {
            return err(ErrorCode::CanonName, path, format!("bad DNS label {l:?}"));
        }
    }
    if labels.last().is_some_and(|l| l.bytes().all(|c| c.is_ascii_digit())) {
        return err(ErrorCode::CanonName, path, "top-level label is numeric");
    }
    Ok(())
}

/// A namespace (2.3): a DNS name, then path segments that are names.
pub fn check_namespace(s: &str, path: &str) -> Result<()> {
    let mut parts = s.split('/');
    check_dns_name(parts.next().unwrap_or(""), path)?;
    for seg in parts {
        check_name(seg, path)?;
    }
    Ok(())
}

/// The host of a namespace: its DNS name.
pub(crate) fn host_of(namespace: &str) -> &str {
    namespace.split('/').next().unwrap_or(namespace)
}

/// A fully qualified function name (2.3): namespace, `/`, name; at most 512 characters.
pub fn check_function_name(s: &str, path: &str) -> Result<()> {
    let Some((ns, f)) = s.rsplit_once('/') else {
        return err(ErrorCode::CanonName, path, format!("function name has no namespace: {s:?}"));
    };
    check_namespace(ns, path)?;
    check_name(f, path)?;
    if s.len() > limits::FUNCTION_NAME {
        return err(ErrorCode::CanonLimit, path, "function name longer than 512 characters");
    }
    Ok(())
}

/// Lowercase hex of exactly `digits` digits (`E_CANON_HEX`).
pub fn check_hex(s: &str, digits: usize, path: &str) -> Result<()> {
    if s.len() != digits || !s.bytes().all(|c| c.is_ascii_digit() || (b'a'..=b'f').contains(&c)) {
        return err(ErrorCode::CanonHex, path, format!("expected {digits} lowercase hex digits"));
    }
    Ok(())
}

/// A key reference `<host>#<key id>` (9.1).
pub fn check_key_ref(s: &str, path: &str) -> Result<()> {
    let Some((host, id)) = s.split_once('#') else {
        return err(ErrorCode::CanonName, path, "a key reference is <host>#<key id>");
    };
    check_dns_name(host, path)?;
    check_hex(id, 16, path)
}

/// The number grammar of 3.1 without the length limit.
pub(crate) fn is_number_grammar(s: &str) -> bool {
    let b = s.as_bytes();
    let mut i = 0;
    if b.first() == Some(&b'-') {
        i = 1;
    }
    match b.get(i) {
        Some(b'0') => i += 1,
        Some(b'1'..=b'9') => {
            while b.get(i).is_some_and(u8::is_ascii_digit) {
                i += 1;
            }
        }
        _ => return false,
    }
    if b.get(i) == Some(&b'.') {
        let frac = &b[i + 1..];
        if frac.is_empty() || !frac.iter().all(u8::is_ascii_digit) || frac[frac.len() - 1] == b'0' {
            return false;
        }
        i = b.len();
    }
    i == b.len() && s != "-0"
}

/// A number (3.1): `E_CANON_NUMBER`, then `E_CANON_LIMIT`.
pub fn check_number(s: &str, path: &str) -> Result<()> {
    if !is_number_grammar(s) {
        return err(ErrorCode::CanonNumber, path, format!("not a canonical decimal: {s:?}"));
    }
    if s.len() > limits::NUMBER {
        return err(ErrorCode::CanonLimit, path, "number longer than 128 characters");
    }
    Ok(())
}

fn digits(b: &[u8]) -> Option<u32> {
    if b.is_empty() || !b.iter().all(u8::is_ascii_digit) {
        return None;
    }
    Some(b.iter().fold(0u32, |a, &c| a * 10 + u32::from(c - b'0')))
}

fn days_in_month(y: u32, m: u32) -> u32 {
    match m {
        1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
        4 | 6 | 9 | 11 => 30,
        2 if (y.is_multiple_of(4) && !y.is_multiple_of(100)) || y.is_multiple_of(400) => 29,
        2 => 28,
        _ => 0,
    }
}

/// `YYYY-MM-DD`, a real day in years 0001-9999.
pub(crate) fn is_date(s: &str) -> bool {
    let b = s.as_bytes();
    if b.len() != 10 || b[4] != b'-' || b[7] != b'-' {
        return false;
    }
    let (Some(y), Some(m), Some(d)) = (digits(&b[0..4]), digits(&b[5..7]), digits(&b[8..10])) else {
        return false;
    };
    (1..=9999).contains(&y) && (1..=12).contains(&m) && d >= 1 && d <= days_in_month(y, m)
}

/// A date (3.2), `E_CANON_DATE`.
pub fn check_date(s: &str, path: &str) -> Result<()> {
    if !is_date(s) {
        return err(ErrorCode::CanonDate, path, format!("not a YYYY-MM-DD calendar date: {s:?}"));
    }
    Ok(())
}

/// `YYYY-MM-DDTHH:MM:SS.ffffffZ`.
pub(crate) fn is_instant(s: &str) -> bool {
    let b = s.as_bytes();
    if b.len() != 27 || !s.is_char_boundary(10) || !is_date(&s[..10]) {
        return false;
    }
    if b[10] != b'T' || b[13] != b':' || b[16] != b':' || b[19] != b'.' || b[26] != b'Z' {
        return false;
    }
    let (Some(h), Some(mi), Some(se), Some(_)) =
        (digits(&b[11..13]), digits(&b[14..16]), digits(&b[17..19]), digits(&b[20..26]))
    else {
        return false;
    };
    h < 24 && mi < 60 && se < 60
}

/// An instant (3.3), `E_CANON_INSTANT`.
pub fn check_instant(s: &str, path: &str) -> Result<()> {
    if !is_instant(s) {
        return err(ErrorCode::CanonInstant, path, format!("not YYYY-MM-DDTHH:MM:SS.ffffffZ: {s:?}"));
    }
    Ok(())
}

fn is_restricted_name(s: &str) -> bool {
    let b = s.as_bytes();
    !b.is_empty() && is_lower_digit(b[0]) && b.iter().all(|&c| is_lower_digit(c) || b"!#$&-^_.+".contains(&c))
}

/// A blob (3.7): its sha256, length and media type. Every failure is `E_CANON_BLOB`.
pub(crate) fn parse_blob(s: &str) -> core::result::Result<([u8; 32], u64, &str), String> {
    let mut parts = s.split(':');
    let (Some(h), Some(len), Some(mt), None) = (parts.next(), parts.next(), parts.next(), parts.next()) else {
        return Err("a blob is <sha256>:<length>:<media type>".into());
    };
    let hash = match decode_hex_lower::<32>(h) {
        Some(x) => x,
        None => return Err("the hash is 64 lowercase hex digits".into()),
    };
    let lb = len.as_bytes();
    if lb.is_empty() || !lb.iter().all(u8::is_ascii_digit) || (lb.len() > 1 && lb[0] == b'0') || lb.len() > 16 {
        return Err("the length is a canonical non-negative integer".into());
    }
    let n: u64 = len.parse().map_err(|_| String::from("bad length"))?;
    if n > limits::BLOB_LENGTH {
        return Err("length above 9007199254740991".into());
    }
    let ok_mt = match mt.split_once('/') {
        Some((t, st)) => is_restricted_name(t) && is_restricted_name(st),
        None => false,
    };
    if !ok_mt {
        return Err(format!("not a lowercase type/subtype media type: {mt:?}"));
    }
    if mt.len() > limits::MEDIA_TYPE {
        return Err("media type longer than 128 characters".into());
    }
    Ok((hash, n, mt))
}

/// A blob (3.7), `E_CANON_BLOB`.
pub fn check_blob(s: &str, path: &str) -> Result<()> {
    match parse_blob(s) {
        Ok(_) => Ok(()),
        Err(d) => err(ErrorCode::CanonBlob, path, d),
    }
}

/// Decodes exactly `N` bytes of lowercase hex.
pub(crate) fn decode_hex_lower<const N: usize>(s: &str) -> Option<[u8; N]> {
    let b = s.as_bytes();
    if b.len() != 2 * N {
        return None;
    }
    let mut out = [0u8; N];
    for (i, o) in out.iter_mut().enumerate() {
        let hi = hexval(b[2 * i])?;
        let lo = hexval(b[2 * i + 1])?;
        *o = (hi << 4) | lo;
    }
    Some(out)
}

fn hexval(c: u8) -> Option<u8> {
    match c {
        b'0'..=b'9' => Some(c - b'0'),
        b'a'..=b'f' => Some(c - b'a' + 10),
        _ => None,
    }
}

/// Lowercase hex of bytes.
pub fn to_hex(bytes: &[u8]) -> String {
    const HEX: &[u8; 16] = b"0123456789abcdef";
    let mut s = String::with_capacity(bytes.len() * 2);
    for &b in bytes {
        s.push(char::from(HEX[usize::from(b >> 4)]));
        s.push(char::from(HEX[usize::from(b & 0xF)]));
    }
    s
}

/// Whether a code point is assigned in Unicode 15.1, by the spec's own table.
pub fn is_assigned_15_1(c: char) -> bool {
    let cp = c as u32;
    tables::ASSIGNED
        .binary_search_by(|&(lo, hi)| {
            if hi < cp {
                core::cmp::Ordering::Less
            } else if lo > cp {
                core::cmp::Ordering::Greater
            } else {
                core::cmp::Ordering::Equal
            }
        })
        .is_ok()
}

/// The string rules of 3.5 in order: characters, NFC, then the byte limit.
/// `allow_tab_lf` relaxes the control rule for function bodies (7).
pub(crate) fn check_text(s: &str, limit: usize, allow_tab_lf: bool, path: &str) -> Result<()> {
    for c in s.chars() {
        let cp = c as u32;
        if (cp < 0x20 && !(allow_tab_lf && (c == '\t' || c == '\n'))) || (0x7F..=0x9F).contains(&cp) {
            return err(ErrorCode::CanonString, path, format!("control character U+{cp:04X}"));
        }
        if !is_assigned_15_1(c) {
            return err(ErrorCode::CanonString, path, format!("U+{cp:04X} is not assigned in Unicode 15.1"));
        }
    }
    if !unicode_normalization::is_nfc(s) {
        return err(ErrorCode::CanonString, path, "not in Normalization Form C");
    }
    if s.len() > limit {
        return err(ErrorCode::CanonLimit, path, format!("longer than {limit} UTF-8 bytes"));
    }
    Ok(())
}

/// A string value (3.5).
pub fn check_string(s: &str, path: &str) -> Result<()> {
    check_text(s, limits::STRING, false, path)
}

/// A licence (8.1): grammar (`E_CANON_LICENCE`), then length (`E_CANON_LIMIT`).
pub fn check_licence(s: &str, path: &str) -> Result<()> {
    if let Some(rest) = s.strip_prefix("https://") {
        if rest.is_empty() || !rest.bytes().all(|c| (0x21..=0x7E).contains(&c)) {
            return err(ErrorCode::CanonLicence, path, "a licence URL is https:// and printable ASCII");
        }
        if s.len() > limits::LICENCE_URL {
            return err(ErrorCode::CanonLimit, path, "licence URL longer than 512 characters");
        }
        return Ok(());
    }
    let b = s.as_bytes();
    let ok = !b.is_empty()
        && b[0].is_ascii_alphanumeric()
        && b.iter().all(|&c| c.is_ascii_alphanumeric() || c == b'.' || c == b'+' || c == b'-');
    if !ok {
        return err(ErrorCode::CanonLicence, path, format!("not an SPDX identifier or https URL: {s:?}"));
    }
    if s.len() > limits::LICENCE_SPDX {
        return err(ErrorCode::CanonLimit, path, "licence identifier longer than 64 characters");
    }
    Ok(())
}

/// Whether `s` is a currency code of `units.md` 2.1 (withdrawn codes included).
pub fn is_currency(s: &str) -> bool {
    tables::CURRENCIES.contains(&s)
}

/// Whether `s` is an index unit (`units.md` 2.4).
pub fn is_index_unit(s: &str) -> bool {
    if s.len() > limits::INDEX_UNIT {
        return false;
    }
    let Some(rest) = s.strip_prefix("index:") else { return false };
    let Some((period, value)) = rest.split_once('=') else { return false };
    let period_ok = match period.len() {
        4 => is_date(&format!("{period}-01-01")),
        7 => is_date(&format!("{period}-01")),
        10 => is_date(period),
        _ => false,
    };
    // Shapes like "2015-1-01" must not slip through the padding above.
    let shape_ok =
        period.bytes().enumerate().all(|(i, c)| if i == 4 || i == 7 { c == b'-' } else { c.is_ascii_digit() });
    period_ok && shape_ok && is_number_grammar(value) && !value.starts_with('-') && value != "0"
}

/// Whether `s` is a literal unit: listed in `units.md` 2.1-2.3, or an index unit.
pub fn is_literal_unit(s: &str) -> bool {
    is_currency(s) || tables::PHYSICAL.contains(&s) || tables::RATIOS.contains(&s) || is_index_unit(s)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn names() {
        for ok in ["fx_rate", "base", "a01", "v2_final"] {
            assert!(is_name_grammar(ok), "{ok}");
        }
        for bad in ["FxRate", "_x", "x_", "a__b", "1st", ""] {
            assert!(!is_name_grammar(bad), "{bad}");
        }
    }

    #[test]
    fn numbers() {
        for ok in ["0", "-1", "1.5", "0.001", "10"] {
            assert!(is_number_grammar(ok), "{ok}");
        }
        for bad in ["-0", "01", "1.", ".5", "1.50", "+1", "1e3", "", "-", "0.0"] {
            assert!(!is_number_grammar(bad), "{bad}");
        }
    }

    #[test]
    fn index_units() {
        assert!(is_index_unit("index:2015=100"));
        assert!(is_index_unit("index:1984-01-03=1000"));
        assert!(is_index_unit("index:2016-02-29=0.5"));
        for bad in [
            "index:0000=1",
            "index:2015=0",
            "index:2015=-1",
            "index:2015-13=1",
            "index:2015-02-29=1",
            "index:2015=1.0",
            "index:15=1",
        ] {
            assert!(!is_index_unit(bad), "{bad}");
        }
    }

    #[test]
    fn instants() {
        assert!(is_instant("2024-03-15T16:00:00.000000Z"));
        assert!(!is_instant("2024-03-15T24:00:00.000000Z"));
        assert!(!is_instant("2024-03-15T23:59:60.000000Z"));
        assert!(!is_instant("2023-02-29T00:00:00.000000Z"));
    }
}