libtracer 0.19.0

Rust binding for libtracer — a lightweight decentralized graph-based networking protocol.
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright 2026 avatarsd LLC

/*!
 * @brief Structured-TLV typed accessors + builders (Unit 6).
 *
 * Typed read/build for the core structured containers: POINT (`0x07`), SETTINGS
 * (`0x0B`), ACL (`0x0A`, NFSv4-style ACEs), SUBSCRIBER (`0x04`), SPEC (`0x0E`),
 * plus a generic NAME-tagged field reader that also covers the ROUTER (`0x0D`)
 * envelope. Byte layouts per reference/05. Vector-pinned: `settings-reliability`,
 * `subscriber-path`, `router-wrapped`, `acl-aces`.
 */

use alloc::string::{String, ToString};
use alloc::vec::Vec;

use crate::path::tlv_to_path;
use crate::tlv_builders::{
    name, subscriber, text_name, value, value_u16, value_u32, value_u64, value_u8, BuildError,
};
use crate::{type_code, Opt, Tlv};

fn named_value(key: &str, val: Tlv) -> Result<[Tlv; 2], BuildError> {
    Ok([name(key)?, val])
}

/* -------------------------------------------------------------- NAME-tagged --- */

/**
 * @brief A NAME-tagged field: a NAME key paired with the TLV that follows it. The
 * generic shape of SETTINGS, SPEC, an ACE's fields, and the ROUTER envelope.
 */
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NamedField {
    /** @brief The field key (the NAME child's UTF-8 payload). */
    pub key: String,
    /** @brief The value TLV that follows the NAME key. */
    pub value: Tlv,
}

/**
 * @brief Read the NAME-tagged fields of any structured TLV as positional `(NAME key,
 * value)` PAIRS — the C++ `wire::config_reader_t` mechanics (#927), mirrored here so
 * both cores parse one grammar (#995).
 *
 * The walk is **pair-consuming**: it steps two children at a time, so a value child is
 * never re-read as the next position's key. A child that is not a `NAME` where a key
 * belongs desynchronizes the pair stream and the walk STOPS there rather than guessing
 * a resync — the every-offset scan this replaced made the grammar ambiguous (a pair
 * whose value textually equalled a known key bound the following child as that key's
 * value). A trailing unpaired `NAME` is ignored. Duplicate keys are preserved in wire
 * order: which occurrence wins — or whether a repeat is rejected outright — is each
 * consumer's disposition, not the walk's (#995: plain NAME-field readers are last-wins;
 * security readers reject).
 *
 * Covers SETTINGS, SPEC, and the `router-wrapped` envelope. The ROUTER (`0x0D`) code
 * is a reserved codepoint with no implemented mechanism in any core; when one lands,
 * its reader joins the REJECT family (the #995 ruling), but until then this walk only
 * enumerates its pairs.
 *
 * # Errors
 * [`BuildError::InvalidUtf8`] on a non-UTF-8 NAME key.
 */
pub fn named_fields(tlv: &Tlv) -> Result<Vec<NamedField>, BuildError> {
    let mut out = Vec::new();
    let ch = &tlv.children;
    let mut i = 0;
    while i + 1 < ch.len() {
        if ch[i].type_code != type_code::NAME {
            break;
        }
        out.push(NamedField {
            key: ch[i].payload_str()?.to_string(),
            value: ch[i + 1].clone(),
        });
        i += 2;
    }
    Ok(out)
}

/**
 * @brief The value TLV of the LAST NAME-tagged field with the given key, or `None` —
 * the plain NAME-field family disposition (#995): pair-consuming + last-occurrence-wins,
 * matching the C++ termini of this grammar (`config_reader_t`, `create_child`,
 * `parse_subscriber_tlv`).
 *
 * Type-agnostic: "last" here means the last PAIR carrying the key. A typed reader
 * ([`settings_str`], [`spec_type_name`]) additionally requires its value TYPE, so a
 * wrong-typed later occurrence never clobbers a well-formed earlier one there.
 *
 * # Errors
 * [`BuildError::InvalidUtf8`] on a non-UTF-8 NAME key while scanning.
 */
pub fn named_field(tlv: &Tlv, key: &str) -> Result<Option<Tlv>, BuildError> {
    Ok(named_fields(tlv)?
        .into_iter()
        .rev()
        .find(|f| f.key == key)
        .map(|f| f.value))
}

/* ---------------------------------------------------------------- SETTINGS --- */

/**
 * @brief The value a SETTINGS key carries — and, inseparably, the TLV **type** that
 * spells it. The two forms are not interchangeable: a reader looks a key up BY type,
 * so a string written as [`SettingValue::Value`] is invisible where a string is
 * expected, and vice versa. Vector-pinned: `conn/create-via-spec` carries both in one
 * record (`port` opaque, `addr` textual).
 */
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SettingValue<'a> {
    /** @brief An opaque `VALUE` child (`0x01`): integers (little-endian), flags, blobs. */
    Value(&'a [u8]),
    /** @brief A UTF-8 `NAME` child (`0x02`): the form a string-valued key MUST take. */
    Name(&'a str),
}

/** @brief The SETTINGS container around an already-built run of key/value children. */
fn settings_of(children: Vec<Tlv>) -> Tlv {
    Tlv {
        type_code: type_code::SETTINGS,
        opt: Opt::structured(),
        payload: Vec::new(),
        children,
        trailer: None,
    }
}

/**
 * @brief Build a SETTINGS TLV (`type=0x0B`, `PL=1`) from `NAME` key → typed value pairs,
 * choosing the value TLV type per key. Vector-pinned: `conn/create-via-spec`.
 *
 * # Errors
 * A per-key or per-value segment error from [`name`] / [`text_name`].
 */
pub fn settings_typed(pairs: &[(&str, SettingValue<'_>)]) -> Result<Tlv, BuildError> {
    let mut children = Vec::with_capacity(pairs.len() * 2);
    for (key, val) in pairs {
        let node = match val {
            SettingValue::Value(bytes) => value(bytes),
            SettingValue::Name(text) => text_name(text)?,
        };
        let [k, v] = named_value(key, node)?;
        children.push(k);
        children.push(v);
    }
    Ok(settings_of(children))
}

/**
 * @brief Build a SETTINGS TLV (`type=0x0B`, `PL=1`) from `NAME`→opaque-VALUE pairs — the
 * all-[`SettingValue::Value`] case of [`settings_typed`]. Vector-pinned:
 * `settings-reliability` (`SETTINGS{ NAME "reliability", VALUE u8=1 }`).
 *
 * # Errors
 * A per-key segment error from [`name`].
 */
pub fn settings(pairs: &[(&str, &[u8])]) -> Result<Tlv, BuildError> {
    let mut children = Vec::with_capacity(pairs.len() * 2);
    for (key, val) in pairs {
        let [k, v] = named_value(key, value(val))?;
        children.push(k);
        children.push(v);
    }
    Ok(settings_of(children))
}

/**
 * @brief The opaque value bytes of a SETTINGS key, or `None`. On a repeated key the
 * LAST occurrence wins ([`named_field`]'s #995 family disposition).
 *
 * @note Type-agnostic by design — it hands back whatever payload the value child holds.
 *       Use [`settings_str`] for a key a reader looks up as a string, so the value TYPE
 *       is checked and not merely the bytes.
 *
 * # Errors
 * [`BuildError::TypeMismatch`] if the TLV is not a SETTINGS, or a non-UTF-8 key.
 */
pub fn settings_get(tlv: &Tlv, key: &str) -> Result<Option<Vec<u8>>, BuildError> {
    if tlv.type_code != type_code::SETTINGS {
        return Err(BuildError::TypeMismatch);
    }
    Ok(named_field(tlv, key)?.map(|v| v.payload))
}

/**
 * @brief The string value of a SETTINGS key — present only when the value child is a
 * `NAME` (`0x02`), the one form a string-valued key is read back from. A key whose
 * value is an opaque `VALUE` reads as `None` here, matching the terminus's **type**
 * check (`config_reader_t`).
 *
 * Within the matching pairs the LAST **well-formed** (`NAME`-typed) occurrence wins,
 * and a wrong-typed occurrence never clobbers a well-formed one — `SETTINGS{ NAME
 * "kind" VALUE …, NAME "kind" NAME "ws" }` reads `"ws"`, exactly as at the terminus
 * (#995; the shared `settings/duplicate-key-last-wins` vector pins both cores).
 *
 * # Errors
 * [`BuildError::TypeMismatch`] if the TLV is not a SETTINGS; [`BuildError::InvalidUtf8`]
 * on a non-UTF-8 key or value.
 */
pub fn settings_str(tlv: &Tlv, key: &str) -> Result<Option<String>, BuildError> {
    if tlv.type_code != type_code::SETTINGS {
        return Err(BuildError::TypeMismatch);
    }
    for f in named_fields(tlv)?.into_iter().rev() {
        if f.key == key && f.value.type_code == type_code::NAME {
            return Ok(Some(f.value.payload_str()?.to_string()));
        }
    }
    Ok(None)
}

/* -------------------------------------------------------------- SUBSCRIBER --- */

/**
 * @brief The target PATH TLV of a SUBSCRIBER (`0x04`), or `None`.
 *
 * # Errors
 * [`BuildError::TypeMismatch`] if the TLV is not a SUBSCRIBER.
 */
pub fn subscriber_target(tlv: &Tlv) -> Result<Option<&Tlv>, BuildError> {
    if tlv.type_code != type_code::SUBSCRIBER {
        return Err(BuildError::TypeMismatch);
    }
    Ok(tlv.first_child(type_code::PATH))
}

/**
 * @brief The target path of a SUBSCRIBER in string form (`"/sensor/temp"`), or `None`
 * when the subscriber has no target PATH. Such a record is not an unsubscribe: written to
 * `:subscribers[N]` it is refused `tr::schema::type_mismatch` (`subscriber/no-target-refused`);
 * the slot-clear sentinel is the empty STATUS.
 *
 * # Errors
 * [`BuildError::TypeMismatch`] if the TLV is not a SUBSCRIBER, or a PATH-decode
 * error.
 */
pub fn subscriber_target_path(tlv: &Tlv) -> Result<Option<String>, BuildError> {
    match subscriber_target(tlv)? {
        Some(p) => Ok(Some(tlv_to_path(p)?)),
        None => Ok(None),
    }
}

/** @brief The SETTINGS key the packed delivery policy travels under (RFC-0022 §3.A). */
pub const DELIVERY_POLICY_KEY: &str = "delivery_policy";

/**
 * @brief One subscription's DELIVERY policy — the packed 16-bit word RFC-0022 §3.A
 * carries in a SUBSCRIBER's `SETTINGS{ NAME "delivery_policy" VALUE u16 }` child.
 *
 * Delivery policy describes one **producer→subscriber relationship**, not the producer:
 * before RFC-0022 these lived on the vertex, where one setting decided the behaviour of
 * every subscriber at once. The bit layout mirrors the C++ core's `delivery_policy_t`
 * exactly, because the same bytes cross between them:
 *
 * | bits | field |
 * | --- | --- |
 * | 0–1 | `reliability` (0 = best-effort, 1 = reliable) |
 * | 2–4 | `priority` (0–7, 0 = default) |
 * | 5 | `durability_request` — deliver the producer's latched last value on join |
 * | 6–7 | `delivery_class` — 0 conflate (default), 1 immediate, 2 batch, 3 stream |
 * | 8–15 | reserved |
 *
 * Bits 6–7 were reserved until RFC-0025 §4.1 assigned them the delivery class, and the
 * assignment costs no wire byte: `0` is conflate, which is what a sender that predates the
 * class wrote there: old subscribers are conflate-class **by construction**. Reading the
 * class is not honouring it — this binding is a codec.
 *
 * The reserved bits are **round-tripped verbatim and never interpreted**: §3.A says a
 * sender MUST write 0 and a receiver MUST *ignore* them, which is an ignore, not a
 * reject. So [`bits`](Self::bits) keeps whatever arrived, while every accessor masks —
 * a future sender's bits survive a hop through this binding instead of being refused or
 * silently normalised away.
 */
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct DeliveryPolicy {
    /** @brief The packed word, VERBATIM — reserved bits included. */
    pub bits: u16,
}

impl DeliveryPolicy {
    /** @brief Bit 5: ask the producer to deliver its latched last value once, on join. */
    pub const DURABILITY_REQUEST: u16 = 0x0020;

    /** @brief Wrap a packed word as it arrived, keeping every reserved bit. */
    #[must_use]
    pub const fn from_bits(bits: u16) -> Self {
        Self { bits }
    }

    /** @brief Bits 0–1 — the reliability class. */
    #[must_use]
    pub const fn reliability(self) -> u8 {
        (self.bits & 0x0003) as u8
    }

    /** @brief Bits 2–4 — the priority class (0–7). */
    #[must_use]
    pub const fn priority(self) -> u8 {
        ((self.bits >> 2) & 0x0007) as u8
    }

    /** @brief Bit 5 — `durability_request`. */
    #[must_use]
    pub const fn durability_request(self) -> bool {
        (self.bits & Self::DURABILITY_REQUEST) != 0
    }

    /**
     * @brief Bits 6–7 — the delivery class (RFC-0025 §4.1): `0` conflate (the default an
     * absent word decodes to), `1` immediate, `2` batch, `3` stream.
     *
     * Total by construction — every two-bit pattern is an assigned class, so there is no
     * "unknown class" to reject and a word from a future sender still decodes to one of the
     * four.
     */
    #[must_use]
    pub const fn delivery_class(self) -> u8 {
        ((self.bits >> 6) & 0x0003) as u8
    }

    /** @brief Bits 8–15 — the reserved field, as it arrived. Never interpreted; exposed
     * so a caller can assert it survived a round trip. Narrowed from 6–15 by RFC-0025
     * §4.1.2 clause 7, which moved bits 6–7 to [`delivery_class`](Self::delivery_class)
     * without moving a byte. */
    #[must_use]
    pub const fn reserved(self) -> u16 {
        self.bits >> 8
    }

    /** @brief True when no honoured bit is set AND nothing is reserved — the "absent"
     * case, which emits no `SETTINGS` child at all. */
    #[must_use]
    pub const fn is_default(self) -> bool {
        self.bits == 0
    }
}

/**
 * @brief The delivery policy a SUBSCRIBER carries, or [`DeliveryPolicy::default`] when it
 * names none — absent ⇒ all-zero ⇒ today's behaviour (RFC-0022 §3.A).
 *
 * A `SETTINGS` child that names a DIFFERENT key (e.g. `delivery_compact`) is not the
 * policy: the word must be read by name, never by position. Returning the default for
 * such a record is what the `subscriber/policy-absent` vector pins.
 *
 * The walk is the plain NAME-field family's (#995), exactly the C++
 * `parse_subscriber_tlv` → `config_reader_t` rule: the LAST **well-formed** occurrence
 * (a 2-byte `VALUE`) wins, and an ill-formed one — wrong TLV type or wrong width — is
 * SKIPPED rather than rejected, so it neither errors out the read nor clobbers a
 * well-formed earlier word. The shared `subscriber/policy-last-wins` vector pins both
 * cores.
 *
 * # Errors
 * [`BuildError::TypeMismatch`] if the TLV is not a SUBSCRIBER.
 */
pub fn subscriber_policy(tlv: &Tlv) -> Result<DeliveryPolicy, BuildError> {
    if tlv.type_code != type_code::SUBSCRIBER {
        return Err(BuildError::TypeMismatch);
    }
    let mut policy = DeliveryPolicy::default();
    for child in tlv.children_of_type(type_code::SETTINGS) {
        for f in named_fields(child)? {
            if f.key != DELIVERY_POLICY_KEY {
                continue;
            }
            if f.value.type_code != type_code::VALUE || f.value.payload.len() != 2 {
                continue;
            }
            policy = DeliveryPolicy::from_bits(u16::from_le_bytes([
                f.value.payload[0],
                f.value.payload[1],
            ]));
        }
    }
    Ok(policy)
}

/**
 * @brief Build a SUBSCRIBER TLV carrying a target PATH and this subscription's packed
 * delivery policy (RFC-0022 §3.A) — the payload of a subscribe-write.
 *
 * An all-zero policy emits **no** `SETTINGS` child, so the bytes are byte-identical to
 * [`crate::subscriber`]'s and to what a pre-RFC-0022 sender emits (the
 * `subscriber/policy-absent` vector). Reserved bits in `policy` are emitted verbatim.
 *
 * # Errors
 * Any error from building the target path.
 */
pub fn subscriber_with_policy(
    target_path: &[&str],
    policy: DeliveryPolicy,
) -> Result<Tlv, BuildError> {
    let mut tlv = subscriber(target_path)?;
    if !policy.is_default() {
        tlv.children.push(Tlv {
            type_code: type_code::SETTINGS,
            opt: Opt::structured(),
            payload: Vec::new(),
            children: alloc::vec![name(DELIVERY_POLICY_KEY)?, value_u16(policy.bits)],
            trailer: None,
        });
    }
    Ok(tlv)
}

/* -------------------------------------------------------------------- POINT --- */

/**
 * @brief Build a POINT TLV (`type=0x07`, `PL=1`): a leading `NAME`, an optional own
 * `VALUE`, then any extra children (DESCRIPTION / SETTINGS / SUBSCRIBER / nested
 * POINT) in caller order (reference/05 §0x07).
 *
 * # Errors
 * A segment error from building the leading [`name`].
 */
pub fn point(
    vertex_name: &str,
    own_value: Option<&[u8]>,
    extra: &[Tlv],
) -> Result<Tlv, BuildError> {
    let mut children = Vec::new();
    children.push(name(vertex_name)?);
    if let Some(v) = own_value {
        children.push(value(v));
    }
    children.extend_from_slice(extra);
    Ok(Tlv {
        type_code: type_code::POINT,
        opt: Opt::structured(),
        payload: Vec::new(),
        children,
        trailer: None,
    })
}

/**
 * @brief The vertex name of a POINT (its first NAME child), or `None`.
 *
 * # Errors
 * [`BuildError::TypeMismatch`] if the TLV is not a POINT, or a non-UTF-8 name.
 */
pub fn point_name(tlv: &Tlv) -> Result<Option<String>, BuildError> {
    if tlv.type_code != type_code::POINT {
        return Err(BuildError::TypeMismatch);
    }
    match tlv.first_child(type_code::NAME) {
        Some(n) => Ok(Some(n.payload_str()?.to_string())),
        None => Ok(None),
    }
}

/**
 * @brief The own VALUE payload of a POINT (its first VALUE child), or `None`.
 *
 * # Errors
 * [`BuildError::TypeMismatch`] if the TLV is not a POINT.
 */
pub fn point_value(tlv: &Tlv) -> Result<Option<Vec<u8>>, BuildError> {
    if tlv.type_code != type_code::POINT {
        return Err(BuildError::TypeMismatch);
    }
    Ok(tlv.first_child(type_code::VALUE).map(|v| v.payload.clone()))
}

/**
 * @brief The nested child POINTs of a POINT (recursive vertex structure).
 *
 * # Errors
 * [`BuildError::TypeMismatch`] if the TLV is not a POINT.
 */
pub fn point_children(tlv: &Tlv) -> Result<Vec<&Tlv>, BuildError> {
    if tlv.type_code != type_code::POINT {
        return Err(BuildError::TypeMismatch);
    }
    Ok(tlv.children_of_type(type_code::POINT).collect())
}

/* ---------------------------------------------------------------------- ACL --- */

/**
 * @brief One NFSv4-style access-control entry (ADR-0020, reference/05 §0x0A). The wire
 * form is an inner `ACL` TLV of NAME-tagged fields.
 */
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Ace {
    /** @brief `type`: 0 = ALLOW, 1 = DENY. */
    pub ace_type: u8,
    /** @brief `flags` bitfield (INHERIT=0x1, INHERIT_ONLY=0x2, NO_PROPAGATE=0x4, GROUP=0x8). */
    pub flags: u8,
    /** @brief `subject` token bytes (e.g. `"peer-a"`, or the one special subject `"EVERYONE@"`). */
    pub subject: Vec<u8>,
    /** @brief `access_mask` u32 bitfield (READ=0x01, WRITE=0x02, SUBSCRIBE=0x04, …) —
     * canonical wire width per RFC-0026. */
    pub access_mask: u32,
    /** @brief Optional `expires_ns` u64 (absent ⇒ never expires). */
    pub expires_ns: Option<u64>,
}

impl Ace {
    /**
     * @brief Build the inner ACL TLV (one ACE): NAME-tagged `type`, `flags`, `subject`,
     * `access_mask`, and optional `expires_ns`, in that order.
     */
    fn to_tlv(&self) -> Tlv {
        let mut children = Vec::new();
        push_named(&mut children, "type", value_u8(self.ace_type));
        push_named(&mut children, "flags", value_u8(self.flags));
        push_named(&mut children, "subject", value(&self.subject));
        push_named(&mut children, "access_mask", value_u32(self.access_mask));
        if let Some(exp) = self.expires_ns {
            push_named(&mut children, "expires_ns", value_u64(exp));
        }
        Tlv {
            type_code: type_code::ACL,
            opt: Opt::structured(),
            payload: Vec::new(),
            children,
            trailer: None,
        }
    }
}

fn push_named(children: &mut Vec<Tlv>, key: &str, val: Tlv) {
    // Keys here are fixed ASCII literals (never reserved chars), so name() cannot
    // fail; expect() documents the invariant.
    children.push(name(key).expect("ACE field key is a valid segment"));
    children.push(val);
}

/**
 * @brief Build an ACL TLV (`type=0x0A`, `PL=1`): the outer ACE collection whose children
 * are inner ACL ACEs. Vector-pinned: `acl-aces`.
 */
pub fn acl(aces: &[Ace]) -> Tlv {
    Tlv {
        type_code: type_code::ACL,
        opt: Opt::structured(),
        payload: Vec::new(),
        children: aces.iter().map(Ace::to_tlv).collect(),
        trailer: None,
    }
}

/**
 * @brief Parse an ACL TLV into its ACEs (each inner ACL a NAME-tagged ACE record).
 *
 * STRICT — the security-reader family of the #995 ruling, matching the C++
 * `graph::parse_acl` (#906): an ACL is a security document, so a shape the builder
 * never emits is REJECTED rather than read leniently, because leniency here does not
 * lose a field — it inverts or widens a grant. Per ACE, `TypeMismatch` on: an odd
 * child count (an unpaired key or value), a non-`NAME` child in a key slot (a
 * desynchronized pair stream), an UNKNOWN key (a silently dropped attribute would
 * widen access), a REPEATED key (last-wins would let `access_mask` narrow-then-wide
 * widen the grant — the shared `acl/ace-duplicate-key` vector pins the refusal in
 * both cores), a numeric field whose value is not a `VALUE`, an empty `subject`, and
 * a missing required `type` / `subject` / `access_mask`.
 *
 * What is deliberately NOT mirrored here: the terminus's policy-tier checks (DENY
 * acceptance, the flag-bit subset, per-field width caps) — those gate what a graph
 * STORES, not what the codec reads; this crate has no policy to evaluate. The
 * `access_mask` width is the canonical u32 of RFC-0026 (#993), read without
 * narrowing.
 *
 * # Errors
 * [`BuildError::TypeMismatch`] per the reject rules above;
 * [`BuildError::InvalidUtf8`] on a non-UTF-8 key.
 */
pub fn acl_aces(tlv: &Tlv) -> Result<Vec<Ace>, BuildError> {
    if tlv.type_code != type_code::ACL {
        return Err(BuildError::TypeMismatch);
    }
    let mut out = Vec::new();
    for inner in tlv.children_of_type(type_code::ACL) {
        let ch = &inner.children;
        if ch.len() % 2 != 0 {
            return Err(BuildError::TypeMismatch);
        }
        let mut ace_type: Option<u8> = None;
        let mut flags: Option<u8> = None;
        let mut subject: Option<Vec<u8>> = None;
        let mut access_mask: Option<u32> = None;
        let mut expires_ns: Option<u64> = None;
        let mut i = 0;
        while i + 1 < ch.len() {
            if ch[i].type_code != type_code::NAME {
                return Err(BuildError::TypeMismatch);
            }
            let val = &ch[i + 1];
            // Every arm rejects a repeat (dup .is_some()) — the security disposition.
            match ch[i].payload_str()? {
                "type" => {
                    if ace_type.is_some() || val.type_code != type_code::VALUE {
                        return Err(BuildError::TypeMismatch);
                    }
                    ace_type = Some(val.payload_uint() as u8);
                }
                "flags" => {
                    if flags.is_some() || val.type_code != type_code::VALUE {
                        return Err(BuildError::TypeMismatch);
                    }
                    flags = Some(val.payload_uint() as u8);
                }
                "subject" => {
                    // Opaque bytes (VALUE recommended; NAME for the "EVERYONE@"
                    // spelling) — any non-empty payload, like the terminus.
                    if subject.is_some() || val.payload.is_empty() {
                        return Err(BuildError::TypeMismatch);
                    }
                    subject = Some(val.payload.clone());
                }
                "access_mask" => {
                    if access_mask.is_some() || val.type_code != type_code::VALUE {
                        return Err(BuildError::TypeMismatch);
                    }
                    // The full canonical u32 (RFC-0026) — narrowing here would silently
                    // drop the high rights bits a wider mask grants.
                    access_mask = Some(val.payload_uint() as u32);
                }
                "expires_ns" => {
                    if expires_ns.is_some() || val.type_code != type_code::VALUE {
                        return Err(BuildError::TypeMismatch);
                    }
                    expires_ns = Some(val.payload_uint());
                }
                _ => return Err(BuildError::TypeMismatch),
            }
            i += 2;
        }
        let (Some(ace_type), Some(subject), Some(access_mask)) = (ace_type, subject, access_mask)
        else {
            return Err(BuildError::TypeMismatch);
        };
        out.push(Ace {
            ace_type,
            flags: flags.unwrap_or(0),
            subject,
            access_mask,
            expires_ns,
        });
    }
    Ok(out)
}

/* --------------------------------------------------------------------- SPEC --- */

/**
 * @brief Build a SPEC TLV (`type=0x0E`, `PL=1`) for in-band vertex creation
 * (reference/05 §0x0E): NAME-tagged `type` (catalog selector) and `name` (the
 * new child's path component), plus an optional `config` SETTINGS child.
 * Vector-pinned: `spec/create-child`.
 *
 * @note This is the GENERIC `:children[]` creation SPEC, which still needs a catalog
 *       `type`. It is NOT the connection-creation spelling: RFC-0014 S7 retired the
 *       global `/net:children[]` door (and its `spec/conn-client-ws` vector), and the
 *       surviving `write /net/<module>/conn` payload — `conn/create-via-spec` — carries
 *       `{ name, config }` and NO `type`, because the module segment of the path already
 *       fixes the transport and the role. Build that one from [`name`] + [`settings_typed`]
 *       rather than from here, or this builder will emit a `type` pair the endpoint does
 *       not want.
 *
 * Both field VALUES are `NAME` (`0x02`) nodes, not `VALUE` (`0x01`): the terminus
 * matches each `(NAME key, value)` pair on the value's TYPE and skips any other,
 * so a `VALUE`-typed `type`/`name` leaves the selector empty and the create is
 * refused outright with `INVALID_PATH`. The two spellings are not interchangeable
 * — and a core that emits the wrong one round-trips its own bytes green, which is
 * why the vectors above exist.
 *
 * The two fields are checked differently because the terminus checks them
 * differently: `name` becomes a path component, so it must satisfy the addressing
 * grammar ([`name`]), while `type` is a catalog selector that is never addressed
 * and only has to be non-empty ([`text_name`]).
 *
 * @note [`name`] and the C++ `valid_segment` it mirrors agree on the full
 *       seven-character reserved set `/ : . [ ] * ?` (#996 closed the drift where
 *       C++ admitted `[` / `]`), pinned cross-tier by the
 *       `path/path-reserved-brackets` conformance vector. So a `child_name` this
 *       builder refuses is one every conforming terminus refuses too.
 *
 * # Errors
 * A segment error from a NAME key, from a `type` outside the 64-byte budget, or
 * from a `child_name` that is not a legal address segment.
 */
pub fn spec(type_sel: &str, child_name: &str, config: Option<Tlv>) -> Result<Tlv, BuildError> {
    let mut children = Vec::new();
    let [k1, v1] = named_value("type", text_name(type_sel)?)?;
    children.push(k1);
    children.push(v1);
    let [k2, v2] = named_value("name", name(child_name)?)?;
    children.push(k2);
    children.push(v2);
    if let Some(cfg) = config {
        children.push(name("config")?);
        children.push(cfg);
    }
    Ok(Tlv {
        type_code: type_code::SPEC,
        opt: Opt::structured(),
        payload: Vec::new(),
        children,
        trailer: None,
    })
}

/**
 * @brief The `type` (catalog selector) and `name` (child component) of a SPEC, as a
 * pair of UTF-8 strings; either is `None` when its field is absent — or present
 * with a value child that is not a `NAME`, which the terminus likewise treats as
 * absent. Reading the type as well as the bytes is what stops this reader from
 * accepting the `VALUE`-typed spelling the terminus refuses.
 *
 * The walk is [`named_fields`]' pair-consuming one (#995), the same rule
 * `create_child` runs: a SPEC whose children begin with a stray non-`NAME` (the
 * shared `spec/desync-stray-value` vector) reads BOTH fields absent here, exactly as
 * the terminus — which refuses the create with `INVALID_PATH` — never sees them. On
 * a repeated key the last `NAME`-typed occurrence wins, and a wrong-typed occurrence
 * never clobbers a well-formed one.
 *
 * # Errors
 * [`BuildError::TypeMismatch`] if the TLV is not a SPEC, or a non-UTF-8 field.
 */
pub fn spec_type_name(tlv: &Tlv) -> Result<(Option<String>, Option<String>), BuildError> {
    if tlv.type_code != type_code::SPEC {
        return Err(BuildError::TypeMismatch);
    }
    let named_str = |key: &str| -> Result<Option<String>, BuildError> {
        for f in named_fields(tlv)?.into_iter().rev() {
            if f.key == key && f.value.type_code == type_code::NAME {
                return Ok(Some(f.value.payload_str()?.to_string()));
            }
        }
        Ok(None)
    };
    Ok((named_str("type")?, named_str("name")?))
}