datumesh-http 0.4.5

The Datumesh engine behind HTTPS: cell envelopes, batches, slices (JSON and Arrow), receipts, jobs, verification and cross-mesh resolution; the datumesh-mesh server.
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
//! What a request names: a cell URI's `<rest>` (SPEC 5.3) or a call (SPEC 5.8), resolved to
//! one canonical reference, pinned to a function hash or not (spec-0.4.2).
//!
//! # The forms `/cell` accepts
//!
//! * **A cell URI's rest**, byte for byte (SPEC 5.3):
//!   `/cell/ecb.europa.eu/fx_rate?base=EUR&date=2024-03-15&quote=USD`, then optionally
//!   `&fn_hash=<64 hex digits>` (a pin, 5.1) and `&asof=<instant>`. It is parsed by the core's
//!   `parse_reference` against the signature of the function the pin names, or else the one
//!   the name is bound to as of that asof (or now), so every code of SPEC 5.4 is reported as it
//!   defines.
//! * **A call** (5.8), percent-encoded as any URL path is
//!   (`/cell/rates.example.org/fx_rate(%22EUR%22,%20%22USD%22,%202024-03-15)`), with an optional
//!   query `?asof=<instant>` and nothing else: a read of the formula dialect whose arguments
//!   are literals, the callee a full name or an `@alias` of the configured map, optionally
//!   pinned (`name@<fn_hash>(...)`). It is parsed by the formula crate's
//!   `datumesh_formula::call::parse_call` in 5.8's order and codes, written as its cell URI and
//!   resolved as above: a call and its URI are one request.
//!
//!   Since 0.2.2 (spec-0.5.0, 5.8, 16.3, 16.7) the call is also a **method call**
//!   (`N(keys).m(args)`, the same request as the plain read `N/m(keys, args)`), takes **unquoted
//!   values** by the position's type, **labels** (`N[label]`, through the class's `lookup`), the
//!   `asof` **default** (the call's `asof`, or the mesh's now, in the cell URI) and a **bare name**
//!   through the alias map. What it resolved is [`Target::resolved`], the answer's `resolved`
//!   (5.5). A key not canonical under its normalisation is `E_KEY_NORMALISATION`, a malformed
//!   encoding `E_KEY_ENCODING` (both `bad_request`). Not accepted: expressions, ranges or units
//!   (`E_PARSE_URI`).
//!
//! An unbound name (or alias) is `not_covered` with `E_NAME_UNBOUND`
//! ([`Rejection::Error`]`(ErrorCode::NameUnbound)`, which the mesh answers so); a pin no
//! descriptor of the mesh has is a plain `not_covered` ([`Rejection::NotCovered`]); a slice
//! marker at `/cell` is `E_SLICE_AT_CELL`.

use std::collections::BTreeMap;
use std::sync::Arc;

use datumesh_core::json::Value;
use datumesh_core::{
    format_reference, lexical, parse_reference, Arg, Cell, CellRef, ClassDescriptor, ErrorCode, FunctionDescriptor,
    Reference, Resolved,
};

/// A function bound to a name as of an instant.
#[derive(Clone, Debug)]
pub struct Bound {
    /// The function hash.
    pub fn_hash: String,
    /// When the binding began.
    pub since: String,
    /// The descriptor.
    pub descriptor: Arc<FunctionDescriptor>,
    /// The descriptor's signature as JSON (what `parse_uri` reads).
    pub signature: Arc<Value>,
}

impl Bound {
    /// A binding of `fn_hash` since `since` to `descriptor`.
    pub fn new(fn_hash: impl Into<String>, since: impl Into<String>, descriptor: Arc<FunctionDescriptor>) -> Bound {
        let signature = Arc::new(descriptor.signature.to_value());
        Bound { fn_hash: fn_hash.into(), since: since.into(), descriptor, signature }
    }
}

/// A class name's binding as of an instant (spec-0.5.0, 9.7): the class hash and the
/// flattened descriptor.
#[derive(Clone, Debug)]
pub struct BoundClass {
    /// The class hash.
    pub class_hash: String,
    /// When the binding began.
    pub since: String,
    /// The flattened class descriptor.
    pub descriptor: Arc<ClassDescriptor>,
}

/// Where names are looked up: the mesh's store (and, for names under other hosts, the
/// cross-mesh resolver in front of it).
pub trait Bindings {
    /// The binding of `fn_name` as of `asof`; `Ok(None)` when it has none. An `Err` is the
    /// code of an `error` envelope (the store or another mesh failed).
    fn binding(&self, fn_name: &str, asof: &str) -> Result<Option<Arc<Bound>>, ErrorCode>;

    /// A target resolved before for the cell URI rest `rest` under function hash `fn_hash`
    /// (parsing depends on nothing else). Provided: none.
    fn cached(&self, fn_hash: &str, rest: &str) -> Option<Arc<Target>> {
        let _ = (fn_hash, rest);
        None
    }

    /// Remembers a resolved target for [`Bindings::cached`]. Provided: nothing.
    fn remember(&self, rest: &str, target: &Arc<Target>) {
        let _ = (rest, target);
    }

    /// The descriptor whose function hash is `fn_hash` (a pin, spec-0.4.2), as a binding of
    /// no time (`since` empty); `Ok(None)` when the mesh has none. Provided: none.
    fn pinned(&self, fn_hash: &str) -> Result<Option<Arc<Bound>>, ErrorCode> {
        let _ = fn_hash;
        Ok(None)
    }

    /// The fully qualified name an alias (without `@`) stands for. Provided: none.
    fn alias(&self, alias: &str) -> Option<String> {
        let _ = alias;
        None
    }

    /// The class `name` is bound to as of `asof` (spec-0.5.0, 9.7, 16.3); `Ok(None)` when it is
    /// not a class (a function name, or unbound). Provided: none.
    fn class(&self, name: &str, asof: &str) -> Result<Option<Arc<BoundClass>>, ErrorCode> {
        let _ = (name, asof);
        Ok(None)
    }

    /// The instant of the alias snapshot [`Bindings::alias`] answers from, for `resolved.aliases_tx`
    /// (16.7); `None` when there is none (the configured map has no snapshot). Provided: none.
    fn aliases_tx(&self) -> Option<String> {
        None
    }

    /// The key encoding the lookup method of class `class` maps `label` to as of `asof`
    /// (16.7, D93): the value of the cell `<class>/<lookup>(label: ...)`. `Ok(None)` when the
    /// class has no lookup or the cell has no value. Provided: none.
    fn lookup(&self, class: &str, label: &str, asof: &str) -> Result<Option<String>, ErrorCode> {
        let _ = (class, label, asof);
        Ok(None)
    }
}

/// A request resolved to its cell (or slice, at `/slice`).
#[derive(Clone, Debug)]
pub struct Target {
    /// The canonical cell URI, `asof` included when the request had one.
    pub uri: String,
    /// The reference (`asof` included).
    pub reference: CellRef,
    /// The cell, with the function hash the name is bound to as of the asof.
    pub cell: Cell,
    id: std::sync::OnceLock<String>,
    key: std::sync::OnceLock<String>,
    /// The binding (for a pinned request, the pinned descriptor).
    pub bound: Arc<Bound>,
    /// The pin (spec-0.4.2): the function hash the request named, if it named one.
    pub pin: Option<String>,
    /// What the request resolved (spec-0.5.0, 5.5, 16.7): an alias, a method, a default or a
    /// label; none for a plain or pinned call of a full name and for a cell URI.
    pub resolved: Option<Resolved>,
}

impl Target {
    /// The function's fully qualified name.
    pub fn fn_name(&self) -> String {
        format!("{}/{}", self.reference.ns, self.reference.fn_name)
    }

    /// The cell's id (SPEC 4.4), computed once.
    pub fn cell_id(&self) -> &str {
        self.id.get_or_init(|| self.cell.id())
    }

    /// The cell's key: its function hash and its URI without `asof` (computed once).
    pub fn cell_key(&self) -> &str {
        self.key.get_or_init(|| {
            let rest = self.rest();
            let base = match self.asof() {
                Some(a) => &rest[..rest.len() - a.len() - "asof=".len() - 1],
                None => rest,
            };
            format!("{} {}", self.bound.fn_hash, base)
        })
    }

    /// The reference with its pin (spec-0.4.2): what the request denotes.
    pub fn pinned_reference(&self) -> Reference {
        let mut r = Reference::from(self.reference.clone());
        r.fn_hash = self.pin.clone();
        r
    }

    /// The requested asof, if any.
    pub fn asof(&self) -> Option<&str> {
        self.reference.asof.as_deref()
    }

    /// The cell URI's `<rest>` (SPEC 5.3), without `cell://`.
    pub fn rest(&self) -> &str {
        &self.uri["cell://".len()..]
    }

    /// The rest with `asof` set to `asof` (replacing one there).
    pub fn rest_asof(&self, asof: &str) -> String {
        let rest = self.rest();
        let base = match self.reference.asof.as_deref() {
            // `asof` is the last parameter of a canonical URI.
            Some(a) => &rest[..rest.len() - a.len() - "asof=".len() - 1],
            None => rest,
        };
        let sep = if base.contains('?') { '&' } else { '?' };
        format!("{base}{sep}asof={asof}")
    }
}

/// Why a request names no cell.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Rejection {
    /// `bad_request` with this code.
    BadRequest(ErrorCode),
    /// `not_covered`: the mesh does not serve the function (a pin no descriptor of it has).
    NotCovered,
    /// `error` with this code: looking the name up failed. `ErrorCode::NameUnbound` stands
    /// for an unbound name, answered `not_covered` with that code (spec-0.4.2; this enum is
    /// exhaustive, so 0.1.1 gives that case no variant of its own).
    Error(ErrorCode),
}

/// Whether `rest` (the text after `/cell/`, query included, as received) is a call rather
/// than a cell URI's rest: its path (before the first `?`) has a `(` or a `%`, or starts with
/// `@`, none of which a cell URI's path (namespace and name) has.
pub fn is_call(rest: &str) -> bool {
    let path = rest.split('?').next().unwrap_or("");
    path.contains('(') || path.contains('%') || path.starts_with('@')
}

/// Resolves a request: a call or a cell URI's rest (see the module documentation). `now` is
/// the asof used to look the name up when the request has none. `slice` allows `*` (and
/// requires one).
pub fn resolve(
    rest: &str,
    now: &str,
    aliases: &BTreeMap<String, String>,
    bindings: &dyn Bindings,
    slice: bool,
) -> Result<Arc<Target>, Rejection> {
    if is_call(rest) {
        resolve_call(rest, now, aliases, bindings, slice)
    } else {
        resolve_uri(rest, now, bindings, slice)
    }
}

/// The parameters of a URI's query, split leniently (for the asof, the pin and a synthetic
/// signature; `parse_reference` checks them properly).
fn params(query: &str) -> Vec<(&str, &str)> {
    if query.is_empty() {
        return vec![];
    }
    query.split('&').map(|p| p.split_once('=').unwrap_or((p, ""))).collect()
}

/// The codes `parse_reference` reports before it reads the signature, or independently of
/// the arguments' types: what a URI whose function the mesh does not know can still be
/// refused with.
fn structural(code: ErrorCode) -> bool {
    matches!(
        code,
        ErrorCode::ParseUri
            | ErrorCode::CanonName
            | ErrorCode::CanonArgName
            | ErrorCode::CanonArgDuplicate
            | ErrorCode::CanonArgOrder
            | ErrorCode::CanonPercent
            | ErrorCode::CanonInstant
            | ErrorCode::CanonHex
    )
}

/// Resolves a cell URI's rest.
pub fn resolve_uri(rest: &str, now: &str, bindings: &dyn Bindings, slice: bool) -> Result<Arc<Target>, Rejection> {
    let (path, query) = match rest.split_once('?') {
        Some((p, q)) => (p, Some(q)),
        None => (rest, None),
    };
    let ps = params(query.unwrap_or(""));
    // The asof the name is looked up at: the last parameter's, when it is an instant; the pin,
    // the last remaining one's when it is named fn_hash and is 64 lowercase hex digits.
    let mut rest_ps: &[(&str, &str)] = &ps;
    let mut asof = now;
    if let Some((("asof", v), before)) = rest_ps.split_last() {
        if lexical::check_instant(v, "$").is_ok() {
            asof = v;
        }
        rest_ps = before;
    }
    let pin = match rest_ps.last() {
        Some(("fn_hash", v)) if lexical::check_hex(v, 64, "$").is_ok() => Some(v.to_string()),
        _ => None,
    };
    let name_ok = lexical::check_function_name(path, "$").is_ok() && !path.contains('#');
    let bound = match (&pin, name_ok) {
        (Some(p), true) => bindings.pinned(p).map_err(Rejection::Error)?,
        (None, true) => bindings.binding(path, asof).map_err(Rejection::Error)?,
        (_, false) => None,
    };
    if let Some(b) = &bound {
        if let Some(t) = bindings.cached(&b.fn_hash, rest) {
            if t.reference.args.values().any(|a| matches!(a, Arg::Slice)) == slice {
                return Ok(t);
            }
        }
    }
    let uri = format!("cell://{rest}");
    let Some(bound) = bound else {
        // No function to parse against: report what the URI's form alone shows (every
        // argument read as a string), else not_covered: the name unbound, or the pin unknown.
        let mut sig = Vec::new();
        for (name, _) in &ps {
            if *name != "asof" && *name != "fn_hash" {
                sig.push((name.to_string(), Value::Object(vec![("type".into(), Value::String("string".into()))])));
            }
        }
        let signature = Value::Object(vec![("args".into(), Value::Object(sig))]);
        // A function name core refuses (2.2, 2.3: a segment or the whole name over its limit, a name
        // that is not canonical) is answered with core's canonical code (spec-0.6.1, 5.4).
        let name_error = if path.contains('#') { None } else { lexical::check_function_name(path, "$").err() };
        return match parse_reference(&uri, &signature) {
            Err(e) if structural(e.code()) => Err(Rejection::BadRequest(e.code())),
            _ if name_error.is_some() => {
                Err(Rejection::BadRequest(name_error.map_or(ErrorCode::CanonName, |e| e.code())))
            }
            _ if pin.is_some() || !name_ok => Err(Rejection::NotCovered),
            _ => Err(Rejection::Error(ErrorCode::NameUnbound)),
        };
    };
    let reference = parse_reference(&uri, &bound.signature).map_err(|e| {
        if crate::envelope::is_request_code(e.code()) {
            Rejection::BadRequest(e.code())
        } else {
            Rejection::Error(ErrorCode::EvalUnavailable)
        }
    })?;
    let has_slice = reference.args.values().any(|a| matches!(a, Arg::Slice));
    if has_slice && !slice {
        return Err(Rejection::BadRequest(ErrorCode::SliceAtCell));
    }
    if !has_slice && slice {
        // A cell at the slice endpoint: the request names no slice.
        return Err(Rejection::BadRequest(ErrorCode::ParseUri));
    }
    let cell = Cell {
        ns: reference.ns.clone(),
        fn_name: reference.fn_name.clone(),
        fn_hash: bound.fn_hash.clone(),
        args: reference.args.clone(),
    };
    let pin = reference.fn_hash.clone();
    let cell_ref = CellRef { ns: reference.ns, fn_name: reference.fn_name, args: reference.args, asof: reference.asof };
    // format(parse(uri)) = uri (SPEC 5.4): the accepted URI is the canonical one.
    let t = Arc::new(Target {
        uri,
        reference: cell_ref,
        cell,
        id: std::sync::OnceLock::new(),
        key: std::sync::OnceLock::new(),
        bound,
        pin,
        resolved: None,
    });
    bindings.remember(rest, &t);
    Ok(t)
}

/// Percent-decodes a URL path (any `%XX`, either case; the result must be UTF-8).
pub(crate) fn url_decode(s: &str) -> Option<String> {
    let b = s.as_bytes();
    let mut out = Vec::with_capacity(b.len());
    let mut i = 0;
    while i < b.len() {
        if b[i] == b'%' {
            let h = std::str::from_utf8(b.get(i + 1..i + 3)?).ok()?;
            out.push(u8::from_str_radix(h, 16).ok()?);
            i += 3;
        } else {
            out.push(b[i]);
            i += 1;
        }
    }
    String::from_utf8(out).ok()
}

/// [`Bindings`] as the formula crate's call environment: aliases from the mesh's map, names as
/// of the call's asof (or now), descriptors by pin.
struct Env<'a> {
    bindings: &'a dyn Bindings,
    aliases: &'a BTreeMap<String, String>,
    now: &'a str,
}

impl datumesh_formula::call::CallEnv for Env<'_> {
    fn alias(&self, alias: &str) -> Result<Option<String>, String> {
        Ok(self.aliases.get(alias).cloned().or_else(|| self.bindings.alias(alias)))
    }
    fn bound(&self, name: &str, asof: Option<&str>) -> Result<Option<FunctionDescriptor>, String> {
        match self.bindings.binding(name, asof.unwrap_or(self.now)) {
            Ok(b) => Ok(b.map(|b| (*b.descriptor).clone())),
            Err(c) => Err(c.as_str().to_string()),
        }
    }
    fn pinned(&self, fn_hash: &str) -> Result<Option<FunctionDescriptor>, String> {
        match self.bindings.pinned(fn_hash) {
            Ok(b) => Ok(b.map(|b| (*b.descriptor).clone())),
            Err(c) => Err(c.as_str().to_string()),
        }
    }
    fn bound_class(&self, name: &str, asof: Option<&str>) -> Result<Option<ClassDescriptor>, String> {
        match self.bindings.class(name, asof.unwrap_or(self.now)) {
            Ok(c) => Ok(c.map(|c| (*c.descriptor).clone())),
            Err(c) => Err(c.as_str().to_string()),
        }
    }
    fn aliases_tx(&self) -> Result<Option<String>, String> {
        Ok(self.bindings.aliases_tx())
    }
    fn now(&self) -> Result<Option<String>, String> {
        Ok(Some(self.now.to_string()))
    }
    fn lookup(&self, class: &str, label: &str, asof: &str) -> Result<Option<String>, String> {
        self.bindings.lookup(class, label, asof).map_err(|c| c.as_str().to_string())
    }
}

/// Resolves a call (SPEC 5.8).
pub fn resolve_call(
    rest: &str,
    now: &str,
    aliases: &BTreeMap<String, String>,
    bindings: &dyn Bindings,
    slice: bool,
) -> Result<Arc<Target>, Rejection> {
    let (path, query) = match rest.split_once('?') {
        Some((p, q)) => (p, Some(q)),
        None => (rest, None),
    };
    let mut text = url_decode(path).ok_or(Rejection::BadRequest(ErrorCode::CanonPercent))?;
    if let Some(q) = query {
        text.push('?');
        text.push_str(q);
    }
    let env = Env { bindings, aliases, now };
    let call = datumesh_formula::call::parse_call(text.trim_start(), &env).map_err(|e| match e {
        datumesh_formula::call::CallError::Code(c, _) => match ErrorCode::from_code(c) {
            Some(ErrorCode::NameUnbound) => Rejection::Error(ErrorCode::NameUnbound),
            Some(ErrorCode::FnUnavailable) => Rejection::NotCovered,
            Some(code) if crate::envelope::is_request_code(code) => Rejection::BadRequest(code),
            _ => Rejection::BadRequest(ErrorCode::ParseUri),
        },
        datumesh_formula::call::CallError::Env(m) => {
            Rejection::Error(ErrorCode::from_code(&m).unwrap_or(ErrorCode::InputUnavailable))
        }
        _ => Rejection::Error(ErrorCode::EvalUnavailable),
    })?;
    let uri = call.uri();
    if !slice && call.cell.args.values().any(|a| matches!(a, Arg::Slice)) {
        return Err(Rejection::BadRequest(ErrorCode::SliceAtCell));
    }
    let target = resolve_uri(&uri["cell://".len()..], now, bindings, slice)?;
    match call.resolved {
        // The target may come from the cache: the clone carries what this request resolved, and is
        // not cached.
        Some(r) => {
            let mut t = (*target).clone();
            t.resolved = Some(r);
            Ok(Arc::new(t))
        }
        None => Ok(target),
    }
}

/// The URI of a reference with its pin (core's `format_reference`).
pub fn format_pinned(reference: &Reference) -> String {
    format_reference(reference)
}