sim-codec-bridge 0.1.1

BRIDGE packet line codec for SIM.
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
use sim_kernel::{Error, Expr, NumberLiteral, Result, Symbol};
use sim_value::build::entry;

/// One axis in a structured collaboration vote.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BridgeScore {
    /// Axis being scored, such as correctness or safety.
    pub axis: Symbol,
    /// Signed score for this axis.
    pub value: i64,
    /// Reviewer rationale for this axis.
    pub reason: String,
}

impl BridgeScore {
    /// Builds a vote score.
    pub fn new(axis: Symbol, value: i64, reason: impl Into<String>) -> Self {
        Self {
            axis,
            value,
            reason: reason.into(),
        }
    }

    /// Decodes a score expression.
    pub fn from_expr(expr: &Expr) -> Result<Self> {
        let fields = map_fields(expr, "bridge/Score")?;
        reject_unknown(fields, &["axis", "value", "reason"])?;
        Ok(Self::new(
            required_symbol(fields, "axis")?.clone(),
            required_i64(fields, "value")?,
            required_string(fields, "reason")?.to_owned(),
        ))
    }

    /// Encodes this score as an expression map.
    pub fn to_expr(&self) -> Expr {
        Expr::Map(vec![
            entry("axis", Expr::Symbol(self.axis.clone())),
            entry("value", i64_expr(self.value)),
            entry("reason", Expr::String(self.reason.clone())),
        ])
    }
}

/// Review payload targeting one packet path.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BridgeReviewPayload {
    /// Target packet path reviewed by this payload.
    pub target: String,
    /// Review text.
    pub body: String,
}

impl BridgeReviewPayload {
    /// Builds a review payload.
    pub fn new(target: impl Into<String>, body: impl Into<String>) -> Self {
        Self {
            target: target.into(),
            body: body.into(),
        }
    }

    /// Decodes a review payload expression.
    pub fn from_expr(expr: &Expr) -> Result<Self> {
        let fields = map_fields(expr, "bridge/Review")?;
        reject_unknown(fields, &["target", "body"])?;
        Ok(Self::new(
            required_string(fields, "target")?,
            required_string(fields, "body")?,
        ))
    }

    /// Encodes this payload as an expression map.
    pub fn to_expr(&self) -> Expr {
        Expr::Map(vec![
            entry("target", Expr::String(self.target.clone())),
            entry("body", Expr::String(self.body.clone())),
        ])
    }
}

/// Vote payload with one or more structured score axes.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BridgeVotePayload {
    /// Target packet path or contribution id being voted on.
    pub target: String,
    /// Structured score axes.
    pub scores: Vec<BridgeScore>,
}

impl BridgeVotePayload {
    /// Builds a vote payload.
    pub fn new(target: impl Into<String>, scores: Vec<BridgeScore>) -> Self {
        Self {
            target: target.into(),
            scores,
        }
    }

    /// Decodes a vote payload expression.
    pub fn from_expr(expr: &Expr) -> Result<Self> {
        let fields = map_fields(expr, "bridge/Vote")?;
        reject_unknown(fields, &["target", "scores"])?;
        let scores = required_vector(fields, "scores")?
            .iter()
            .map(BridgeScore::from_expr)
            .collect::<Result<Vec<_>>>()?;
        if scores.is_empty() {
            return Err(Error::Eval(
                "BRIDGE vote payload must carry at least one score".to_owned(),
            ));
        }
        Ok(Self::new(required_string(fields, "target")?, scores))
    }

    /// Encodes this payload as an expression map.
    pub fn to_expr(&self) -> Expr {
        Expr::Map(vec![
            entry("target", Expr::String(self.target.clone())),
            entry(
                "scores",
                Expr::Vector(self.scores.iter().map(BridgeScore::to_expr).collect()),
            ),
        ])
    }
}

/// Patch payload targeting an exact parent packet and path.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BridgePatchPayload {
    /// Parent packet content id being patched.
    pub parent_cid: String,
    /// Target packet path.
    pub target: String,
    /// Replacement expression for the target path.
    pub replacement: Expr,
}

impl BridgePatchPayload {
    /// Builds a patch payload.
    pub fn new(
        parent_cid: impl Into<String>,
        target: impl Into<String>,
        replacement: Expr,
    ) -> Self {
        Self {
            parent_cid: parent_cid.into(),
            target: target.into(),
            replacement,
        }
    }

    /// Decodes a patch payload expression.
    pub fn from_expr(expr: &Expr) -> Result<Self> {
        let fields = map_fields(expr, "bridge/Patch")?;
        reject_unknown(fields, &["parent-cid", "target", "replacement"])?;
        Ok(Self::new(
            required_string(fields, "parent-cid")?,
            required_string(fields, "target")?,
            required_field(fields, "replacement")?.clone(),
        ))
    }

    /// Encodes this payload as an expression map.
    pub fn to_expr(&self) -> Expr {
        Expr::Map(vec![
            entry("parent-cid", Expr::String(self.parent_cid.clone())),
            entry("target", Expr::String(self.target.clone())),
            entry("replacement", self.replacement.clone()),
        ])
    }
}

/// Evidence payload attached to a collaboration packet.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BridgeEvidencePayload {
    /// Evidence source id or URI.
    pub source: String,
    /// Evidence body.
    pub body: String,
}

impl BridgeEvidencePayload {
    /// Builds an evidence payload.
    pub fn new(source: impl Into<String>, body: impl Into<String>) -> Self {
        Self {
            source: source.into(),
            body: body.into(),
        }
    }

    /// Decodes an evidence payload expression.
    pub fn from_expr(expr: &Expr) -> Result<Self> {
        let fields = map_fields(expr, "bridge/Evidence")?;
        reject_unknown(fields, &["source", "body"])?;
        Ok(Self::new(
            required_string(fields, "source")?,
            required_string(fields, "body")?,
        ))
    }

    /// Encodes this payload as an expression map.
    pub fn to_expr(&self) -> Expr {
        Expr::Map(vec![
            entry("source", Expr::String(self.source.clone())),
            entry("body", Expr::String(self.body.clone())),
        ])
    }
}

/// Receipt payload for an accepted or rejected collaboration step.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BridgeReceiptPayload {
    /// Receipt status.
    pub status: Symbol,
    /// Packet paths or content ids covered by the receipt.
    pub refs: Vec<String>,
}

impl BridgeReceiptPayload {
    /// Builds a receipt payload.
    pub fn new(status: Symbol, refs: Vec<String>) -> Self {
        Self { status, refs }
    }

    /// Decodes a receipt payload expression.
    pub fn from_expr(expr: &Expr) -> Result<Self> {
        let fields = map_fields(expr, "bridge/Receipt")?;
        reject_unknown(fields, &["status", "refs"])?;
        Ok(Self::new(
            required_symbol(fields, "status")?.clone(),
            string_vector(fields, "refs")?,
        ))
    }

    /// Encodes this payload as an expression map.
    pub fn to_expr(&self) -> Expr {
        Expr::Map(vec![
            entry("status", Expr::Symbol(self.status.clone())),
            entry(
                "refs",
                Expr::Vector(self.refs.iter().cloned().map(Expr::String).collect()),
            ),
        ])
    }
}

/// Attestation payload citing evidence for a claim.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BridgeAttestPayload {
    /// Subject packet path or content id.
    pub subject: String,
    /// Claim being attested.
    pub claim: String,
    /// Evidence ids supporting the claim.
    pub evidence: Vec<String>,
}

impl BridgeAttestPayload {
    /// Builds an attestation payload.
    pub fn new(
        subject: impl Into<String>,
        claim: impl Into<String>,
        evidence: Vec<String>,
    ) -> Self {
        Self {
            subject: subject.into(),
            claim: claim.into(),
            evidence,
        }
    }

    /// Decodes an attestation payload expression.
    pub fn from_expr(expr: &Expr) -> Result<Self> {
        let fields = map_fields(expr, "bridge/Attest")?;
        reject_unknown(fields, &["subject", "claim", "evidence"])?;
        Ok(Self::new(
            required_string(fields, "subject")?,
            required_string(fields, "claim")?,
            string_vector(fields, "evidence")?,
        ))
    }

    /// Encodes this payload as an expression map.
    pub fn to_expr(&self) -> Expr {
        Expr::Map(vec![
            entry("subject", Expr::String(self.subject.clone())),
            entry("claim", Expr::String(self.claim.clone())),
            entry(
                "evidence",
                Expr::Vector(self.evidence.iter().cloned().map(Expr::String).collect()),
            ),
        ])
    }
}

/// Validates a collaboration part payload for its registered part kind.
pub fn validate_collab_payload(kind: &Symbol, payload: &Expr) -> Result<()> {
    if kind == &part("Review") {
        BridgeReviewPayload::from_expr(payload).map(drop)
    } else if kind == &part("Vote") {
        BridgeVotePayload::from_expr(payload).map(drop)
    } else if kind == &part("Patch") {
        BridgePatchPayload::from_expr(payload).map(drop)
    } else if kind == &part("Evidence") {
        BridgeEvidencePayload::from_expr(payload).map(drop)
    } else if kind == &part("Receipt") {
        BridgeReceiptPayload::from_expr(payload).map(drop)
    } else if kind == &part("Attest") {
        BridgeAttestPayload::from_expr(payload).map(drop)
    } else {
        Ok(())
    }
}

fn map_fields<'a>(expr: &'a Expr, label: &str) -> Result<&'a [(Expr, Expr)]> {
    match expr {
        Expr::Map(fields) => Ok(fields),
        _ => Err(Error::Eval(format!("{label} payload must be a map"))),
    }
}

fn reject_unknown(fields: &[(Expr, Expr)], allowed: &[&str]) -> Result<()> {
    for (key, _) in fields {
        let Some(name) = field_name(key) else {
            return Err(Error::Eval(
                "BRIDGE collaboration field keys must be symbols".to_owned(),
            ));
        };
        if !allowed.contains(&name.as_str()) {
            return Err(Error::Eval(format!(
                "unknown BRIDGE collaboration field {name}"
            )));
        }
    }
    Ok(())
}

fn required_field<'a>(fields: &'a [(Expr, Expr)], name: &str) -> Result<&'a Expr> {
    fields
        .iter()
        .find_map(|(key, value)| (field_name(key).as_deref() == Some(name)).then_some(value))
        .ok_or_else(|| Error::Eval(format!("BRIDGE collaboration record is missing {name}")))
}

fn required_symbol<'a>(fields: &'a [(Expr, Expr)], name: &str) -> Result<&'a Symbol> {
    match required_field(fields, name)? {
        Expr::Symbol(symbol) => Ok(symbol),
        _ => Err(Error::TypeMismatch {
            expected: "symbol",
            found: "non-symbol",
        }),
    }
}

fn required_string<'a>(fields: &'a [(Expr, Expr)], name: &str) -> Result<&'a str> {
    match required_field(fields, name)? {
        Expr::String(value) => Ok(value),
        _ => Err(Error::TypeMismatch {
            expected: "string",
            found: "non-string",
        }),
    }
}

fn required_i64(fields: &[(Expr, Expr)], name: &str) -> Result<i64> {
    match required_field(fields, name)? {
        Expr::Number(number) => number.canonical.parse().map_err(|_| {
            Error::Eval(format!(
                "BRIDGE collaboration field {name} must be an i64 literal"
            ))
        }),
        _ => Err(Error::TypeMismatch {
            expected: "number",
            found: "non-number",
        }),
    }
}

fn required_vector<'a>(fields: &'a [(Expr, Expr)], name: &str) -> Result<&'a [Expr]> {
    match required_field(fields, name)? {
        Expr::Vector(items) | Expr::List(items) => Ok(items),
        _ => Err(Error::Eval(format!(
            "BRIDGE collaboration field {name} must be a vector"
        ))),
    }
}

fn string_vector(fields: &[(Expr, Expr)], name: &str) -> Result<Vec<String>> {
    required_vector(fields, name)?
        .iter()
        .map(|item| match item {
            Expr::String(value) => Ok(value.clone()),
            _ => Err(Error::TypeMismatch {
                expected: "string",
                found: "non-string",
            }),
        })
        .collect()
}

fn i64_expr(value: i64) -> Expr {
    Expr::Number(NumberLiteral {
        domain: Symbol::qualified("numbers", "i64"),
        canonical: value.to_string(),
    })
}

fn field_name(expr: &Expr) -> Option<String> {
    match expr {
        Expr::Symbol(symbol) => Some(symbol.name.to_string()),
        Expr::String(value) => Some(value.clone()),
        _ => None,
    }
}

fn part(name: &str) -> Symbol {
    Symbol::qualified("bridge", name)
}