semiont 0.7.2

Semiont's Rust SDK: the protocol's types, the transport contract and a client of the bus over it, and what a client needs to name its principals and hold its roles, as protocol/specs/ states it.
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
//! The first fault of a value as one of the protocol's shapes. Every SDK
//! holds what it reads to the table of what each component schema requires
//! (`SCHEMAS`, which the build writes from src/required-schemas.json, by the
//! rules of packages/codegen/schemas/required-shapes.mjs) before it decodes it, and
//! names the first fault it finds the same way: a member the value lacks, or
//! one that is not what its schema allows. What serde refuses that the table
//! does not state is a fault of the value itself, and nothing of serde's own
//! account is said: it shows what it was given.

use crate::transport::FrameRefusalReason;
use crate::types::Schema;
use serde_json::{Map, Value};
use std::fmt;

/// A kind of JSON value, as a schema names it. An integer is a number with no
/// fractional part.
pub(crate) enum Kind {
    Object,
    Array,
    String,
    Integer,
    Number,
    Boolean,
    Null,
}

/// A value a shape lists: the value is one of them.
pub(crate) enum Listed {
    Text(&'static str),
    Number(f64),
    Boolean(bool),
}

impl Listed {
    /// Whether `value` is this one: a number is one equal to it in value.
    fn is(&self, value: &Value) -> bool {
        match (self, value) {
            (Listed::Text(one), Value::String(held)) => one == held,
            (Listed::Number(one), Value::Number(held)) => held.as_f64() == Some(*one),
            (Listed::Boolean(one), Value::Bool(held)) => one == held,
            _ => false,
        }
    }
}

/// One step of a pattern: at least `min` of a text's next code points, and
/// at most `max` (none: any number), each in one of the ranges `of`, from and
/// to, both included.
pub(crate) struct Step {
    pub(crate) of: &'static [(u32, u32)],
    pub(crate) min: usize,
    pub(crate) max: Option<usize>,
}

impl Step {
    fn holds(&self, point: char) -> bool {
        self.of
            .iter()
            .any(|(from, to)| (*from..=*to).contains(&u32::from(point)))
    }
}

/// The member whose value names the variant a union is.
pub(crate) struct Discriminator {
    pub(crate) property: &'static str,
    pub(crate) mapping: &'static [(&'static str, usize)],
}

/// What a value must be: packages/codegen/schemas/required-shapes.mjs states
/// what each part means. A part the table does not state is `NONE`'s: no kinds allow
/// any, and an empty list holds nothing.
pub(crate) struct Shape {
    pub(crate) reference: Option<&'static str>,
    pub(crate) kinds: Option<&'static [Kind]>,
    pub(crate) enumeration: Option<&'static [Listed]>,
    pub(crate) pattern: Option<&'static [Step]>,
    pub(crate) min_length: Option<usize>,
    pub(crate) max_length: Option<usize>,
    pub(crate) minimum: Option<f64>,
    pub(crate) maximum: Option<f64>,
    pub(crate) min_items: Option<usize>,
    pub(crate) max_items: Option<usize>,
    pub(crate) required: &'static [&'static str],
    pub(crate) members: &'static [(&'static str, Shape)],
    /// The only members an object may carry; none, where it may carry any.
    pub(crate) closed: Option<&'static [&'static str]>,
    pub(crate) items: Option<&'static Shape>,
    pub(crate) values: Option<&'static Shape>,
    pub(crate) one_of: &'static [Shape],
    pub(crate) discriminator: Option<Discriminator>,
    pub(crate) all_of: &'static [Shape],
}

/// The shape that states nothing.
pub(crate) const NONE: Shape = Shape {
    reference: None,
    kinds: None,
    enumeration: None,
    pattern: None,
    min_length: None,
    max_length: None,
    minimum: None,
    maximum: None,
    min_items: None,
    max_items: None,
    required: &[],
    members: &[],
    closed: None,
    items: None,
    values: None,
    one_of: &[],
    discriminator: None,
    all_of: &[],
};

include!(concat!(env!("OUT_DIR"), "/required.rs"));

/// The first thing wrong with a value: the way to the member it is at (the
/// names of the members it is within, and its own, joined by dots; empty for
/// the value itself), and why.
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct Fault {
    pub(crate) member: String,
    pub(crate) reason: FrameRefusalReason,
}

impl Fault {
    /// The fault of a value serde refuses that has no fault the table
    /// states: the value itself is malformed.
    pub(crate) fn undecoded() -> Self {
        Fault {
            member: String::new(),
            reason: FrameRefusalReason::Malformed,
        }
    }

    /// This fault of the value at `member` of another, named by its way from
    /// that other.
    pub(crate) fn within(self, member: &str) -> Fault {
        Fault {
            member: match self.member.is_empty() {
                true => member.to_owned(),
                false => within(member, &self.member),
            },
            reason: self.reason,
        }
    }
}

/// What every SDK says of the fault.
impl fmt::Display for Fault {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.reason {
            FrameRefusalReason::Lacks => write!(f, "it has no `{}`", self.member),
            FrameRefusalReason::Malformed if self.member.is_empty() => {
                f.write_str("it is malformed")
            }
            FrameRefusalReason::Malformed => write!(f, "its `{}` is malformed", self.member),
        }
    }
}

/// What the component schema `name` requires of a value.
pub(crate) fn schema(name: &str) -> &'static Shape {
    SCHEMAS
        .binary_search_by(|(named, _)| (*named).cmp(name))
        .map(|at| &SCHEMAS[at].1)
        .unwrap_or_else(|_| panic!("{name} is no component schema of the spec"))
}

fn resolved(shape: &'static Shape) -> &'static Shape {
    match shape.reference {
        Some(name) => resolved(schema(name)),
        None => shape,
    }
}

/// Whether `kinds` allow `value`: none allow any.
fn allows(kinds: Option<&[Kind]>, value: &Value) -> bool {
    kinds.is_none_or(|kinds| {
        kinds.iter().any(|kind| match (kind, value) {
            (Kind::Object, Value::Object(_))
            | (Kind::Array, Value::Array(_))
            | (Kind::String, Value::String(_))
            | (Kind::Number, Value::Number(_))
            | (Kind::Boolean, Value::Bool(_))
            | (Kind::Null, Value::Null) => true,
            (Kind::Integer, Value::Number(number)) => {
                number.is_i64()
                    || number.is_u64()
                    || number.as_f64().is_some_and(|n| n.fract() == 0.0)
            }
            _ => false,
        })
    })
}

fn within(way: &str, name: &str) -> String {
    if way.is_empty() {
        name.to_owned()
    } else {
        format!("{way}.{name}")
    }
}

fn malformed(way: &str) -> Fault {
    Fault {
        member: way.to_owned(),
        reason: FrameRefusalReason::Malformed,
    }
}

/// Whether `count` is at least `least` and at most `most`, where each is
/// stated.
fn counted(least: Option<usize>, count: usize, most: Option<usize>) -> bool {
    least.is_none_or(|least| count >= least) && most.is_none_or(|most| count <= most)
}

/// Whether `points` are of `steps`: the first step takes as many of them as
/// it may, and gives them back one by one until the steps after it take the
/// rest.
fn matches(steps: &[Step], points: &[char]) -> bool {
    let Some((first, rest)) = steps.split_first() else {
        return points.is_empty();
    };
    let most = first.max.map_or(points.len(), |max| max.min(points.len()));
    let taken = points[..most]
        .iter()
        .take_while(|point| first.holds(**point))
        .count();
    (first.min..=taken)
        .rev()
        .any(|count| matches(rest, &points[count..]))
}

/// Whether `value` is one of `shape`'s values, a text is of its pattern and
/// has as many code points as it allows, a number is within its bounds, and
/// an array has as many items as it allows.
fn is_as_stated(shape: &Shape, value: &Value) -> bool {
    if let Some(listed) = shape.enumeration
        && !listed.iter().any(|one| one.is(value))
    {
        return false;
    }
    match value {
        Value::String(text) => {
            let points: Vec<char> = text.chars().collect();
            counted(shape.min_length, points.len(), shape.max_length)
                && shape.pattern.is_none_or(|steps| matches(steps, &points))
        }
        Value::Number(number) => number.as_f64().is_some_and(|number| {
            shape.minimum.is_none_or(|least| number >= least)
                && shape.maximum.is_none_or(|most| number <= most)
        }),
        Value::Array(items) => counted(shape.min_items, items.len(), shape.max_items),
        _ => true,
    }
}

/// The first fault `value` has as a value of `shape`, from `way`.
fn fault_at(shape: &'static Shape, value: &Value, way: &str) -> Option<Fault> {
    let own = resolved(shape);
    if !allows(own.kinds, value) || !is_as_stated(own, value) {
        return Some(malformed(way));
    }
    if let Value::Object(object) = value
        && let Some(fault) = fault_of_members(own, object, way)
    {
        return Some(fault);
    }
    if let (Value::Array(items), Some(item)) = (value, own.items)
        && let Some(fault) = items.iter().find_map(|held| fault_at(item, held, way))
    {
        return Some(fault);
    }
    if !own.one_of.is_empty()
        && let Some(fault) = fault_of_one_of(own, value, way)
    {
        return Some(fault);
    }
    own.all_of
        .iter()
        .find_map(|part| fault_at(part, value, way))
}

/// The first fault of an object in the members `shape` requires, in what its
/// members are, in the members it may not carry, and in its values.
fn fault_of_members(
    shape: &'static Shape,
    object: &Map<String, Value>,
    way: &str,
) -> Option<Fault> {
    for name in shape.required {
        let member = shape.members.iter().find(|(named, _)| named == name);
        let lacking = match object.get(*name) {
            None => true,
            Some(Value::Null) => {
                member.is_some_and(|(_, member)| !allows(resolved(member).kinds, &Value::Null))
            }
            Some(_) => false,
        };
        if lacking {
            return Some(Fault {
                member: within(way, name),
                reason: FrameRefusalReason::Lacks,
            });
        }
    }
    for (name, member) in shape.members {
        if let Some(held) = object.get(*name).filter(|held| !held.is_null())
            && let Some(fault) = fault_at(member, held, &within(way, name))
        {
            return Some(fault);
        }
    }
    if let Some(closed) = shape.closed
        && let Some(unnamed) = object
            .keys()
            .filter(|name| !closed.contains(&name.as_str()))
            .min()
    {
        return Some(malformed(&within(way, unnamed)));
    }
    let values = shape.values?;
    let mut keys: Vec<&String> = object.keys().collect();
    keys.sort();
    keys.into_iter()
        .find_map(|key| fault_at(values, &object[key], way))
}

/// The fault `value` has as one of the variants of `shape`: the variant its
/// discriminator names, or else each variant whose kinds allow it. None when
/// one of them has none; the first one's when each has one; and a value no
/// variant allows is malformed.
fn fault_of_one_of(shape: &'static Shape, value: &Value, way: &str) -> Option<Fault> {
    let named = shape.discriminator.as_ref().and_then(|discriminator| {
        let stated = value.get(discriminator.property)?.as_str()?;
        discriminator
            .mapping
            .iter()
            .find(|(tag, _)| *tag == stated)
            .map(|(_, at)| &shape.one_of[*at])
    });
    let candidates: Vec<&'static Shape> = match named {
        Some(variant) => vec![variant],
        None => shape
            .one_of
            .iter()
            .filter(|variant| allows(resolved(variant).kinds, value))
            .collect(),
    };
    let mut first = None;
    for candidate in &candidates {
        let fault = fault_at(candidate, value, way)?;
        first.get_or_insert(fault);
    }
    first.or_else(|| Some(malformed(way)))
}

/// The first fault `value` has as a value of `shape`, by the rules every SDK
/// reads the table by; nothing when it has none.
pub(crate) fn fault_of(shape: &'static Shape, value: &Value) -> Option<Fault> {
    fault_at(shape, value, "")
}

/// What every SDK says of the first fault `body` has as the component schema
/// `T` is generated from, as it refuses it: "it has no \`member\`", "its
/// \`member\` is malformed", or "it is malformed"; `None` when it has none.
pub fn fault_said<T: Schema>(body: &Value) -> Option<String> {
    fault_of(schema(T::NAME), body).map(|fault| fault.to_string())
}

#[cfg(test)]
mod tests {
    use super::{Fault, fault_of, fault_said, schema};
    use crate::transport::FrameRefusalReason;
    use crate::types::UserResponse;
    use serde_json::json;

    fn malformed(member: &str) -> Option<Fault> {
        Some(Fault {
            member: member.to_owned(),
            reason: FrameRefusalReason::Malformed,
        })
    }

    #[test]
    fn a_value_is_held_to_its_values_its_form_its_size_and_the_members_a_closed_object_names() {
        let cases = [
            ("BusPingMessage", json!({"event": "ping", "data": ""}), None),
            (
                "BusPingMessage",
                json!({"event": "pong", "data": ""}),
                malformed("event"),
            ),
            (
                "BusPingMessage",
                json!({"event": "ping", "data": "x"}),
                malformed("data"),
            ),
            (
                "BrowseProfilesRequest",
                json!({"dids": ["did:web:example.org"]}),
                None,
            ),
            (
                "BrowseProfilesRequest",
                json!({"dids": ["did:\u{1f600}"]}),
                None,
            ),
            (
                "BrowseProfilesRequest",
                json!({"dids": []}),
                malformed("dids"),
            ),
            (
                "BrowseProfilesRequest",
                json!({"dids": ["web:example.org"]}),
                malformed("dids"),
            ),
            (
                "BrowseProfilesRequest",
                json!({"dids": ["did:web:a b"]}),
                malformed("dids"),
            ),
            (
                "GatherAnnotationOptions",
                json!({"contextWindow": 100.0}),
                None,
            ),
            (
                "GatherAnnotationOptions",
                json!({"contextWindow": 5000}),
                None,
            ),
            (
                "GatherAnnotationOptions",
                json!({"contextWindow": 99}),
                malformed("contextWindow"),
            ),
            (
                "GatherAnnotationOptions",
                json!({"contextWindow": 5001}),
                malformed("contextWindow"),
            ),
            // A closed object: what it describes first, then the first member
            // it does not name, by code point.
            (
                "BrowseProfilesRequest",
                json!({"dids": ["did:web:example.org"], "more": 1}),
                malformed("more"),
            ),
            (
                "BrowseProfilesRequest",
                json!({"dids": [], "more": 1}),
                malformed("dids"),
            ),
            (
                "BrowseProfilesRequest",
                json!({"more": 1}),
                Some(Fault {
                    member: "dids".to_owned(),
                    reason: FrameRefusalReason::Lacks,
                }),
            ),
            (
                "BrowseProfilesRequest",
                json!({"dids": ["did:web:example.org"], "\u{1f600}": 1, "\u{ff61}": 1}),
                malformed("\u{ff61}"),
            ),
        ];
        for (name, value, fault) in cases {
            assert_eq!(fault_of(schema(name), &value), fault, "{name}: {value}");
        }
    }

    #[test]
    fn the_first_fault_of_a_body_is_said_as_every_sdk_says_it() {
        let user = json!({"did": "did:web:example.org:users:a", "domain": "example.org", "email": null, "name": null, "image": null});
        assert_eq!(fault_said::<UserResponse>(&user), None);
        assert_eq!(
            fault_said::<UserResponse>(&json!({"did": "did:web:example.org:users:a"})).as_deref(),
            Some("it has no `email`")
        );
        assert_eq!(
            fault_said::<UserResponse>(&json!({"did": "web", "domain": "example.org", "email": null, "name": null, "image": null})).as_deref(),
            Some("its `did` is malformed")
        );
        assert_eq!(
            fault_said::<UserResponse>(&json!("a user")).as_deref(),
            Some("it is malformed")
        );
    }
}