datumesh-http 0.4.6

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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! The function page (Build 2 Part 4, P2; CONTRACT-build2-part4-publish): `GET /fn/<function name>`,
//! for people (HTML, server-rendered, no script) and for agents (the same data as JSON, by
//! `Accept: application/json` or `?format=json`).
//!
//! The data is built once (as a JSON value, its members named as the contract names them) and the
//! HTML is rendered from it, so both forms carry the same facts. Publisher text is escaped; in the
//! description and the argument, example and case texts every space is a no-break space, so no
//! attribute-looking word ever follows a space in the markup.
//!
//! A relayed function (Build 2 Part 5, F5) is one whose stored receipts carry a `source`: the
//! function's first recorded cells are read (their receipts) and the first `source` found is its
//! source, cached for [`RELAY_TTL`].

use std::time::{Duration, Instant};

use datumesh_core::json::{self, Value};
use datumesh_core::{Arg, Cell, FunctionDescriptor, Receipt, Slice};
use datumesh_store::Store;

use crate::envelope::{self, Cache, Reply};
use crate::mesh::Inner;
use crate::testrun::{self, Case, Expect, Overlay, Ran, Run, TestSpec};

/// How many of a function's recorded cells are read for a `source`.
const RELAY_SCAN: usize = 8;
/// How long a function's source is remembered.
const RELAY_TTL: Duration = Duration::from_secs(300);

/// Where a relayed function's value is taken from (spec-0.6.0, 16.8): the source's name and its
/// takeover page, when the receipt gives one.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Relay {
    pub(crate) name: String,
    pub(crate) takeover: Option<String>,
}

/// The page of `name` on this mesh (`https://<host>/fn/<name>`).
pub(crate) fn page_url(inner: &Inner, name: &str) -> String {
    format!("{}/fn/{name}", inner.config.origin())
}

/// The relay of a function (the first `source` among its first recorded cells), cached.
pub(crate) fn relay_of(inner: &Inner, name: &str, fn_hash: &str, d: &FunctionDescriptor, now: &str) -> Option<Relay> {
    // Keyed by name and hash: two functions of one descriptor are two functions with their own cells.
    let key = format!("{name}\n{fn_hash}");
    if let Some((at, r)) = inner.relays.lock().unwrap_or_else(|p| p.into_inner()).get(&key) {
        if at.elapsed() < RELAY_TTL {
            return r.clone();
        }
    }
    let found = read_relay(inner, name, fn_hash, d, now);
    let mut cache = inner.relays.lock().unwrap_or_else(|p| p.into_inner());
    if cache.len() >= 10_000 {
        cache.clear();
    }
    cache.insert(key, (Instant::now(), found.clone()));
    found
}

/// A relay is found by reading the receipts of a function's first recorded cells. This is a heuristic
/// for now (all cells of a relay carry the source; a mixed function is not a relay the rule names). A
/// store index of the sources per function (filled by `publish-stored`) would answer without the
/// scan: that is the step after this one.
fn read_relay(inner: &Inner, name: &str, fn_hash: &str, d: &FunctionDescriptor, now: &str) -> Option<Relay> {
    let (ns, f) = name.rsplit_once('/')?;
    let args = d.signature.args.keys().map(|k| (k.clone(), Arg::Slice)).collect();
    let cell = Cell { ns: ns.into(), fn_name: f.into(), fn_hash: fn_hash.into(), args };
    let slice = Slice::from_value(&cell.to_value()).ok()?;
    let values = inner.store.slice_values(&slice, now).ok()?;
    let hashes: Vec<String> = values.iter().take(RELAY_SCAN).map(|v| v.version.receipt_hash.clone()).collect();
    let refs: Vec<&str> = hashes.iter().map(String::as_str).collect();
    let receipts = inner.store.receipts_many(&refs).ok()?;
    for bytes in receipts.into_iter().flatten() {
        let Ok(r) = Receipt::from_json(&bytes) else { continue };
        if let Some(s) = r.source() {
            return Some(Relay { name: s.name().to_string(), takeover: s.takeover().map(str::to_string) });
        }
    }
    None
}

fn s(v: &str) -> Value {
    Value::String(v.to_string())
}

fn num(n: u64) -> Value {
    json::parse(n.to_string().as_bytes()).unwrap_or(Value::Null)
}

/// The reads of a JSON value this page makes (the core's value has no typed accessors for these).
trait Read4 {
    fn as_bool(&self) -> Option<bool>;
    fn as_u64(&self) -> Option<u64>;
    fn is_null(&self) -> bool;
}

impl Read4 for Value {
    fn as_bool(&self) -> Option<bool> {
        match self {
            Value::Bool(b) => Some(*b),
            _ => None,
        }
    }
    fn as_u64(&self) -> Option<u64> {
        self.as_number_text()?.parse().ok()
    }
    fn is_null(&self) -> bool {
        matches!(self, Value::Null)
    }
}

fn obj(members: Vec<(&str, Value)>) -> Value {
    Value::Object(members.into_iter().map(|(k, v)| (k.to_string(), v)).collect())
}

/// The text of an expectation: the value, or the fault.
fn expect_text(e: &Expect) -> String {
    match e {
        Expect::Value(v) => v.lexical().to_string(),
        Expect::Fault(code) => format!("fault {code}"),
    }
}

/// The page's data of `name`, or `None` when no function of that name is bound now.
pub(crate) fn data(inner: &Inner, name: &str) -> Option<Value> {
    let store: &dyn Store = &*inner.store;
    let now = inner.now();
    let binding = store.resolve_binding(name, &now).ok()??;
    let fn_hash = binding.fn_hash.clone();
    let d = FunctionDescriptor::from_json(&store.descriptor(&fn_hash)?).ok()?;
    let entry = crate::find::entry_by_name(store, name);
    let origin = inner.config.origin();
    let link = |n: &str| obj(vec![("name", s(n)), ("page", s(&page_url(inner, n)))]);

    // The catalogue: description, arguments, examples.
    let description = entry.as_ref().map(|e| e.description.clone()).filter(|t| !t.is_empty());
    let label = entry.as_ref().map(|e| e.label.clone()).filter(|t| !t.is_empty()).unwrap_or_else(|| name.to_string());
    let args: Vec<(String, Value)> =
        entry.as_ref().map(|e| e.args.iter().map(|(k, v)| (k.clone(), s(v))).collect()).unwrap_or_default();
    let examples_json: Vec<Value> = entry
        .as_ref()
        .and_then(|e| json::parse(e.examples.as_bytes()).ok())
        .and_then(|v| if let Value::Array(a) = v { Some(a) } else { None })
        .unwrap_or_default();
    let examples: Vec<Value> = examples_json
        .iter()
        .filter_map(|x| {
            let call = x.get("call").and_then(Value::as_str)?.to_string();
            let cell = format!("{origin}/cell/{}", crate::client::pct(&call));
            Some(obj(vec![
                ("call", s(&call)),
                ("expected", x.get("expected").cloned().unwrap_or(Value::Null)),
                ("cell", s(&cell)),
            ]))
        })
        .collect();

    // The signature: the descriptor's, with the order, the hash and the binding history.
    let order: Vec<Value> = match &d.order {
        Some(o) => o.iter().map(|x| s(x)).collect(),
        None => d.signature.args.keys().map(|x| s(x)).collect(),
    };
    let sig = d.signature.to_value();
    let mut sig_members: Vec<(String, Value)> = match sig {
        Value::Object(m) => m,
        _ => Vec::new(),
    };
    sig_members.retain(|(k, _)| k != "order" && k != "fn_hash" && k != "bindings");
    sig_members.push(("order".into(), Value::Array(order)));
    sig_members.push(("fn_hash".into(), s(&fn_hash)));
    sig_members
        .push(("bindings".into(), Value::Array(vec![obj(vec![("fn_hash", s(&fn_hash)), ("since", s(&binding.tx))])])));
    let signature = Value::Object(sig_members);

    // The body: a computed function's formula.
    let body = if d.lang == "stored" { Value::Null } else { s(&d.body) };

    // The tests: counts, the derived cases, each test's call and expectation, the last run.
    let (count, branches, covered, counterexamples) =
        testrun::counts(store, &inner.config.host, name, &fn_hash, &now).unwrap_or((0, 0, 0, 0));
    let open: Vec<Value> = testrun::counterexamples(store, &inner.config.host, name, &fn_hash, &now)
        .unwrap_or_default()
        .into_iter()
        .map(|c| obj(vec![("signer", s(&c.signer)), ("case", s(&c.case))]))
        .collect();
    let held = testrun::held_tests_of(store, &fn_hash, name, &now).unwrap_or_default();
    let overlay = Overlay::default();
    let ran: Vec<Ran> = held.iter().map(|(_, spec)| testrun::run_test(store, &overlay, &now, spec)).collect();
    let refs: Vec<&Ran> = ran.iter().collect();
    let derived: Vec<Value> = {
        let mut run = Run::new(store, &overlay, &now);
        match run.built(name, &fn_hash) {
            Ok(b) => match b.compiled.as_ref() {
                Some(c) => testrun::cases_of(c, &b.function.signature, &refs)
                    .into_iter()
                    .map(|(Case(case), ok)| obj(vec![("case", s(&case)), ("covered", Value::Bool(ok))]))
                    .collect(),
                None => Vec::new(),
            },
            Err(_) => Vec::new(),
        }
    };
    // Each case with its last result (`pass`, `fail`, `error`; null before its first run).
    let cases: Vec<Value> = held
        .iter()
        .map(|(row, spec): &(_, TestSpec)| {
            let result = row.last_result.as_deref().map_or(Value::Null, s);
            obj(vec![("call", s(&spec.call)), ("expect", s(&expect_text(&spec.expect))), ("result", result)])
        })
        .collect();
    let rows: Vec<&datumesh_store::FunctionTest> = held.iter().map(|(r, _)| r).collect();
    let results: Vec<Option<&str>> = rows.iter().map(|r| r.last_result.as_deref()).collect();
    // The worst result first, whatever the store's order: error, then fail (QUESTIONS-build2 D22).
    let worst = ["error", "fail"].into_iter().find(|w| results.iter().flatten().any(|r| r == w));
    let result = if let Some(bad) = worst.or_else(|| results.iter().flatten().copied().find(|r| *r != "pass")) {
        bad.to_string()
    } else if !results.is_empty() && results.iter().all(|r| r.is_some()) {
        "pass".to_string()
    } else {
        "not run".to_string()
    };
    let at = rows.iter().filter_map(|r| r.last_run_at.clone()).max().unwrap_or_default();
    let tests = obj(vec![
        ("count", num(count)),
        ("branches", num(branches)),
        ("covered", num(covered)),
        ("counterexamples", num(counterexamples)),
        ("open_counterexamples", Value::Array(open)),
        ("derived", Value::Array(derived)),
        ("cases", Value::Array(cases)),
        ("last_run", obj(vec![("result", s(&result)), ("at", s(&at))])),
    ]);

    // The lineage: inputs, dependants on this mesh, the relay's source.
    let inputs: Vec<Value> = d.inputs.iter().map(|n| link(n)).collect();
    let mut dependants: Vec<String> = Vec::new();
    for h in store.function_readers(name).unwrap_or_default() {
        for n in store.bound_names(&h).unwrap_or_default() {
            let bound_now = store.resolve_binding(&n, &now).ok().flatten().is_some_and(|b| b.fn_hash == h);
            if bound_now && !dependants.contains(&n) {
                dependants.push(n);
            }
        }
    }
    dependants.sort();
    let mut lineage = vec![
        ("inputs", Value::Array(inputs)),
        ("dependants", Value::Array(dependants.iter().map(|n| link(n)).collect())),
    ];
    let relay = relay_of(inner, name, &fn_hash, &d, &now);
    if let Some(r) = &relay {
        let mut src = vec![("name", s(&r.name)), ("relayed_by", s(&inner.config.host))];
        if let Some(t) = &r.takeover {
            src.push(("takeover", s(t)));
        }
        lineage.push(("source", obj(src)));
    }

    // The licence: the publisher's declarations of this function (SPEC 9.2, 8.5).
    let host = name.split('/').next().unwrap_or("");
    let declared: Vec<Value> = store
        .namespace(host)
        .ok()
        .flatten()
        .and_then(|n| json::parse(&n.keydoc).ok())
        .and_then(|doc| doc.get("declarations").and_then(|d| d.get(name)).cloned())
        .and_then(|v| if let Value::Array(a) = v { Some(a) } else { None })
        .unwrap_or_default();

    Some(obj(vec![
        ("name", s(name)),
        ("page", s(&page_url(inner, name))),
        ("fn_hash", s(&fn_hash)),
        ("label", s(&label)),
        ("description", description.as_deref().map_or(Value::Null, s)),
        ("args", Value::Object(args)),
        ("signature", signature),
        ("body", body),
        ("tests", tests),
        ("lineage", obj(lineage)),
        ("examples", Value::Array(examples)),
        ("licence", obj(vec![("declarations", Value::Array(declared))])),
    ]))
}

/// `GET /fn/<name>`: the JSON form when asked for it, else the HTML page; `not_covered` when no
/// function of that name is bound on this mesh.
pub(crate) fn respond(inner: &Inner, name: &str, json_form: bool) -> Reply {
    let Some(d) = data(inner, name) else {
        if json_form {
            return envelope::name_unbound_reply(inner.config.ttl.latest_max_age.as_secs());
        }
        let body = format!(
            "<!doctype html><html lang=\"en\"><head><meta charset=\"utf-8\"><title>not covered</title></head>\
             <body><h1>not_covered</h1><p>E_NAME_UNBOUND: no function named {} is bound on this mesh.</p></body></html>",
            esc(name)
        );
        return Reply {
            status: 404,
            body: body.into_bytes().into(),
            content_type: "text/html; charset=utf-8",
            cache: Cache::Public(60),
            canonical: None,
            retry_after: None,
        };
    };
    if json_form {
        return Reply::json(200, d.to_jcs(), Cache::Public(inner.config.ttl.latest_max_age.as_secs()));
    }
    let body = render(&d);
    Reply {
        status: 200,
        body: body.into_bytes().into(),
        content_type: "text/html; charset=utf-8",
        cache: Cache::Public(inner.config.ttl.latest_max_age.as_secs()),
        canonical: None,
        retry_after: None,
    }
}

/// Escapes text for an HTML text node or a double-quoted attribute.
pub(crate) fn esc(t: &str) -> String {
    let mut out = String::with_capacity(t.len());
    for c in t.chars() {
        match c {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            '"' => out.push_str("&quot;"),
            '\'' => out.push_str("&#39;"),
            c => out.push(c),
        }
    }
    out
}

/// Publisher text as markup: escaped, every space a no-break space (see the module documentation).
fn prose(t: &str) -> String {
    esc(t).replace(' ', "\u{a0}")
}

fn get<'a>(v: &'a Value, k: &str) -> &'a Value {
    v.get(k).unwrap_or(&Value::Null)
}

fn text_of(v: &Value) -> String {
    match v {
        Value::String(x) => x.clone(),
        Value::Null => String::new(),
        other => String::from_utf8_lossy(&other.to_jcs()).into_owned(),
    }
}

/// The HTML page of the data `d` (no script; every publisher string escaped).
pub(crate) fn render(d: &Value) -> String {
    let name = get(d, "name").as_str().unwrap_or("");
    let label = get(d, "label").as_str().unwrap_or(name);
    let mut h = String::new();
    h.push_str("<!doctype html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n");
    h.push_str(&format!("<title>{} | {}</title>\n", esc(label), esc(name)));
    h.push_str("<style>body{font:16px/1.5 system-ui,sans-serif;margin:0;padding:16px;color:#1d1d1f;background:#fff}main{max-width:52rem;margin:0 auto}code,pre{font-family:ui-monospace,monospace;font-size:14px;overflow-x:auto}section{border-top:1px solid #d2d2d7;padding:12px 0}h1{font-size:1.5rem;margin:0.5rem 0}h2{font-size:1.1rem;margin:0 0 .5rem}</style>\n</head>\n<body>\n<main>\n");
    h.push_str(&format!("<header><h1>{}</h1><p><code>{}</code></p></header>\n", esc(label), esc(name)));

    // description
    h.push_str("<section id=\"description\">\n<h2>Description</h2>\n");
    match get(d, "description").as_str() {
        Some(t) => h.push_str(&format!("<p>{}</p>\n", prose(t))),
        None => h.push_str("<p>No description is published.</p>\n"),
    }
    if let Value::Object(args) = get(d, "args") {
        if !args.is_empty() {
            h.push_str("<dl>\n");
            for (k, v) in args {
                h.push_str(&format!("<dt><code>{}</code></dt><dd>{}</dd>\n", esc(k), prose(&text_of(v))));
            }
            h.push_str("</dl>\n");
        }
    }
    h.push_str("</section>\n");

    // signature
    let sig = get(d, "signature");
    h.push_str("<section id=\"signature\">\n<h2>Signature</h2>\n<ul>\n");
    if let Value::Object(args) = get(sig, "args") {
        let order: Vec<String> = match get(sig, "order") {
            Value::Array(a) => a.iter().map(text_of).collect(),
            _ => args.iter().map(|(k, _)| k.clone()).collect(),
        };
        for k in order {
            if let Some((_, decl)) = args.iter().find(|(n, _)| *n == k) {
                let ty = get(decl, "type").as_str().unwrap_or("");
                let unit = get(decl, "unit").as_str().map(|u| format!(", unit {u}")).unwrap_or_default();
                let valid = if get(decl, "valid").as_bool() == Some(true) { ", valid time" } else { "" };
                h.push_str(&format!("<li><code>{}</code>: {}{}{}</li>\n", esc(&k), esc(ty), esc(&unit), valid));
            }
        }
    }
    let ret = get(sig, "returns");
    let ret_text = match ret {
        Value::String(t) => t.clone(),
        Value::Object(_) => {
            let ty = get(ret, "type").as_str().unwrap_or("");
            match get(ret, "unit").as_str() {
                Some(u) => format!("{ty}, unit {u}"),
                None => ty.to_string(),
            }
        }
        _ => String::new(),
    };
    h.push_str(&format!("<li>returns: {}</li>\n", esc(&ret_text)));
    h.push_str(&format!("<li>fn_hash: <code>{}</code></li>\n", esc(get(sig, "fn_hash").as_str().unwrap_or(""))));
    h.push_str("</ul>\n<h3>Bindings</h3>\n<ul>\n");
    if let Value::Array(bs) = get(sig, "bindings") {
        for b in bs {
            h.push_str(&format!(
                "<li><code>{}</code> since {}</li>\n",
                esc(get(b, "fn_hash").as_str().unwrap_or("")),
                esc(get(b, "since").as_str().unwrap_or(""))
            ));
        }
    }
    h.push_str("</ul>\n</section>\n");

    // body
    h.push_str("<section id=\"body\">\n<h2>Body</h2>\n");
    match get(d, "body").as_str() {
        Some(b) => h.push_str(&format!("<pre><code>{}</code></pre>\n", esc(b))),
        None => h.push_str("<p>A stored function: its values are published, it has no formula.</p>\n"),
    }
    h.push_str("</section>\n");

    // tests
    let t = get(d, "tests");
    let n = |k: &str| get(t, k).as_u64().unwrap_or(0);
    h.push_str("<section id=\"tests\">\n<h2>Tests</h2>\n");
    h.push_str(&format!(
        "<p>{} tests, {} of {} branches covered, {} counterexamples.</p>\n",
        n("count"),
        n("covered"),
        n("branches"),
        n("counterexamples")
    ));
    let last = get(t, "last_run");
    h.push_str(&format!(
        "<p>Last run: {} {}</p>\n",
        esc(get(last, "result").as_str().unwrap_or("")),
        esc(get(last, "at").as_str().unwrap_or(""))
    ));
    h.push_str("<h3>Derived cases</h3>\n<ul>\n");
    if let Value::Array(ds) = get(t, "derived") {
        for c in ds {
            let covered = if get(c, "covered").as_bool() == Some(true) { "covered" } else { "not covered" };
            h.push_str(&format!("<li>{}: {}</li>\n", prose(get(c, "case").as_str().unwrap_or("")), covered));
        }
    }
    h.push_str("</ul>\n<h3>Counterexamples</h3>\n<ul>\n");
    if let Value::Array(open) = get(t, "open_counterexamples") {
        for c in open {
            h.push_str(&format!(
                "<li>open counterexample from {}: case <code>{}</code></li>\n",
                esc(get(c, "signer").as_str().unwrap_or("")),
                esc(get(c, "case").as_str().unwrap_or(""))
            ));
        }
    }
    h.push_str("</ul>\n<h3>Each test</h3>\n<ul>\n");
    if let Value::Array(cs) = get(t, "cases") {
        for c in cs {
            h.push_str(&format!(
                "<li><code>{}</code> expects {}: {}</li>\n",
                prose(get(c, "call").as_str().unwrap_or("")),
                prose(get(c, "expect").as_str().unwrap_or("")),
                esc(get(c, "result").as_str().unwrap_or("not run"))
            ));
        }
    }
    h.push_str("</ul>\n</section>\n");

    // lineage
    let lin = get(d, "lineage");
    h.push_str("<section id=\"lineage\">\n<h2>Lineage</h2>\n");
    for (title, key) in [("Inputs", "inputs"), ("Dependants on this mesh", "dependants")] {
        h.push_str(&format!("<h3>{title}</h3>\n<ul>\n"));
        if let Value::Array(items) = get(lin, key) {
            for it in items {
                let n = get(it, "name").as_str().unwrap_or("");
                h.push_str(&format!(
                    "<li><a href=\"{}\"><code>{}</code></a></li>\n",
                    esc(get(it, "page").as_str().unwrap_or("")),
                    esc(n)
                ));
            }
        }
        h.push_str("</ul>\n");
    }
    let src = get(lin, "source");
    if !src.is_null() {
        let relayed_by = get(src, "relayed_by").as_str().unwrap_or("");
        h.push_str(&format!(
            "<p>Source: {} (relayed by {}).</p>\n",
            esc(get(src, "name").as_str().unwrap_or("")),
            esc(relayed_by)
        ));
        if let Some(take) = get(src, "takeover").as_str() {
            h.push_str(&format!(
                "<p>The source can take over publishing these values: <a href=\"{}\">{}</a></p>\n",
                esc(take),
                esc(take)
            ));
        }
    }
    h.push_str("</section>\n");

    // examples
    h.push_str("<section id=\"examples\">\n<h2>Examples</h2>\n");
    match get(d, "examples") {
        Value::Array(ex) if !ex.is_empty() => {
            h.push_str("<ul>\n");
            for e in ex {
                let call = get(e, "call").as_str().unwrap_or("");
                let expected = match get(e, "expected") {
                    Value::Object(_) => get(get(e, "expected"), "value")
                        .as_str()
                        .map(str::to_string)
                        .unwrap_or_else(|| text_of(get(e, "expected"))),
                    other => text_of(other),
                };
                h.push_str(&format!(
                    "<li><a href=\"{}\">{}</a> gives {}</li>\n",
                    esc(get(e, "cell").as_str().unwrap_or("")),
                    prose(call),
                    prose(&expected)
                ));
            }
            h.push_str("</ul>\n");
        }
        _ => h.push_str("<p>No examples are published.</p>\n"),
    }
    h.push_str("</section>\n");

    // licence
    h.push_str("<section id=\"licence\">\n<h2>Licence and declarations</h2>\n");
    match get(get(d, "licence"), "declarations") {
        Value::Array(ds) if !ds.is_empty() => {
            h.push_str("<ul>\n");
            for dec in ds {
                h.push_str(&format!(
                    "<li>{} from {}{}</li>\n",
                    esc(get(dec, "licence").as_str().unwrap_or("")),
                    esc(get(dec, "valid_from").as_str().unwrap_or("")),
                    get(dec, "valid_to").as_str().map(|t| format!(" until {}", esc(t))).unwrap_or_default()
                ));
            }
            h.push_str("</ul>\n");
        }
        _ => h.push_str("<p>No declarations.</p>\n"),
    }
    h.push_str("</section>\n");
    h.push_str("</main>\n</body>\n</html>\n");
    h
}