libtracer 0.7.1

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
// 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, value, value_u16, 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: each `NAME` child is paired
 * with the immediately-following child as its value. A trailing `NAME` with no
 * following value is skipped. Covers `router-wrapped`, SETTINGS, SPEC.
 *
 * # 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 < ch.len() {
        if ch[i].type_code == type_code::NAME && i + 1 < ch.len() {
            out.push(NamedField {
                key: ch[i].payload_str()?.to_string(),
                value: ch[i + 1].clone(),
            });
            i += 2;
            continue;
        }
        i += 1;
    }
    Ok(out)
}

/**
 * @brief The value TLV of the first NAME-tagged field with the given key, or `None`.
 *
 * # 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()
        .find(|f| f.key == key)
        .map(|f| f.value))
}

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

/**
 * @brief Build a SETTINGS TLV (`type=0x0B`, `PL=1`) from `NAME`→opaque-VALUE pairs.
 * 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(Tlv {
        type_code: type_code::SETTINGS,
        opt: Opt::structured(),
        payload: Vec::new(),
        children,
        trailer: None,
    })
}

/**
 * @brief The opaque value bytes of a SETTINGS key, or `None`.
 *
 * # 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))
}

/* -------------------------------------------------------------- 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 (the unsubscribe sentinel).
 *
 * # 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–15 | reserved |
 *
 * 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–15 — the reserved field, as it arrived. Never interpreted; exposed
     * so a caller can assert it survived a round trip. */
    #[must_use]
    pub const fn reserved(self) -> u16 {
        self.bits >> 6
    }

    /** @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.
 *
 * # Errors
 * [`BuildError::TypeMismatch`] if the TLV is not a SUBSCRIBER, or if a `delivery_policy`
 * key is present but its value is not a 2-byte VALUE.
 */
pub fn subscriber_policy(tlv: &Tlv) -> Result<DeliveryPolicy, BuildError> {
    if tlv.type_code != type_code::SUBSCRIBER {
        return Err(BuildError::TypeMismatch);
    }
    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 {
                return Err(BuildError::TypeMismatch);
            }
            return Ok(DeliveryPolicy::from_bits(u16::from_le_bytes([
                f.value.payload[0],
                f.value.payload[1],
            ])));
        }
    }
    Ok(DeliveryPolicy::default())
}

/**
 * @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 special `"EVERYONE@"` / `"OWNER@"`). */
    pub subject: Vec<u8>,
    /** @brief `access_mask` u16 bitfield (READ=0x01, WRITE=0x02, SUBSCRIBE=0x04, …). */
    pub access_mask: u16,
    /** @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_u16(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).
 *
 * # Errors
 * [`BuildError::TypeMismatch`] if the TLV is not an ACL or an ACE lacks the
 * required `type` / `subject` / `access_mask` fields.
 */
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 fields = named_fields(inner)?;
        let get = |k: &str| fields.iter().find(|f| f.key == k).map(|f| &f.value);
        let ace_type = get("type").ok_or(BuildError::TypeMismatch)?.payload_uint() as u8;
        let flags = get("flags").map(|v| v.payload_uint() as u8).unwrap_or(0);
        let subject = get("subject")
            .ok_or(BuildError::TypeMismatch)?
            .payload
            .clone();
        let access_mask = get("access_mask")
            .ok_or(BuildError::TypeMismatch)?
            .payload_uint() as u16;
        let expires_ns = get("expires_ns").map(|v| v.payload_uint());
        out.push(Ace {
            ace_type,
            flags,
            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.
 *
 * # Errors
 * A segment error from a NAME key.
 */
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", value(type_sel.as_bytes()))?;
    children.push(k1);
    children.push(v1);
    let [k2, v2] = named_value("name", value(child_name.as_bytes()))?;
    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.
 *
 * # 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 type_sel = named_field(tlv, "type")?
        .map(|v| v.payload_str().map(|s| s.to_string()))
        .transpose()?;
    let child_name = named_field(tlv, "name")?
        .map(|v| v.payload_str().map(|s| s.to_string()))
        .transpose()?;
    Ok((type_sel, child_name))
}