harmos-sdk 0.1.2

Harmos SDK for authoring plugins and operations over the Harmos runtime.
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
/* ***
 *
 * Harmos
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * ***
 */

// Protobuf From And To The Canonical Postcard Value, Driven By The Described Shape.
//
// Every message crosses process boundaries as its Postcard value; the protobuf
// form packaging publishes in `.proto` files is a projection of the same
// described fields. Fields are written in tag order, required scalars equal to
// their default are omitted, numeric repeated fields are packed, and a message
// whose fields are all variants is a oneof whose Postcard form is the variant
// index followed by that variant's fields. Floats are default only at positive
// zero, so negative zero keeps its sign across the wire.

/* === Modules === */

/* === Re-Exports === */

/* === Imports === */

// Standard Library Imports

// Third Party Imports
use prost::encoding::{WireType, encode_key, encode_varint};
// Local Imports
use super::input::{DecodeField, DecodeInput, DecodeScalar, DecodeValue};
use harmos_domain::__private::{PostcardReader as Reader, PostcardValue};
use harmos_domain::MessageError;
use harmos_domain::schema::Cardinality;
use harmos_domain::schema::declaration::{Definition, Field, Kind};

/* === Constants === */

/* === Macros === */

/* === Type Aliases === */

/* === Structs === */

/* === Enums === */

/* === Traits === */

/* === Implementations === */

/* === Public Functions === */

pub fn encode(definition: &Definition, postcard: &[u8], bytes: &mut Vec<u8>) -> Result<(), MessageError> {
    let mut input = Reader::new(postcard);
    message(definition, &mut input, bytes)?;
    input.finish()
}

pub fn decode(definition: &Definition, input: DecodeInput<'_>) -> Result<Vec<u8>, MessageError> {
    // Postcard Rarely Outgrows Its Protobuf Source, So The Bound Stays Near The Payload.
    let mut bytes = Vec::with_capacity(input.encoded_len()?);
    decode_message(definition, input, &mut bytes)?;
    Ok(bytes)
}

/* === Private Functions === */

fn oneof(fields: &[Field]) -> bool {
    !fields.is_empty() && fields.iter().all(|field| field.cardinality == Cardinality::Variant)
}

fn payload(field: &Field) -> Result<&Definition, MessageError> {
    match &field.kind {
        Kind::Message { definition } => Ok(definition),
        _ => Err(MessageError::borrowed("a oneof variant carries a message payload")),
    }
}

/* --- Postcard To Protobuf --- */

fn message(definition: &Definition, input: &mut Reader<'_>, bytes: &mut Vec<u8>) -> Result<(), MessageError> {
    let fields = (definition.fields)();
    if oneof(fields) {
        let index: u32 = input.value()?;
        let field = usize::try_from(index)
            .ok()
            .and_then(|index| fields.get(index))
            .ok_or_else(|| MessageError::borrowed("unknown Postcard enum variant"))?;
        let mut body = Vec::new();
        members(payload(field)?, input, &mut body)?;
        delimited(field.tag, &body, bytes);
        return Ok(());
    }
    members(definition, input, bytes)
}

// Postcard Holds Fields In Declaration Order; Protobuf Writes Them By Tag.
fn members(definition: &Definition, input: &mut Reader<'_>, bytes: &mut Vec<u8>) -> Result<(), MessageError> {
    let fields = (definition.fields)();
    let mut chunks = Vec::with_capacity(fields.len());
    for field in fields {
        let mut chunk = Vec::new();
        member(field, input, &mut chunk)?;
        if !chunk.is_empty() {
            chunks.push((field.tag, chunk));
        }
    }
    chunks.sort_by_key(|(tag, _)| *tag);
    for (_, chunk) in chunks {
        bytes.extend_from_slice(&chunk);
    }
    Ok(())
}

fn member(field: &Field, input: &mut Reader<'_>, bytes: &mut Vec<u8>) -> Result<(), MessageError> {
    match (field.cardinality, &field.kind) {
        (Cardinality::Required, Kind::Message { definition }) => nested(field.tag, definition, input, bytes),
        (Cardinality::Required, kind) => scalar(field.tag, kind, input, bytes, true),
        (Cardinality::Optional, kind) => {
            if !input.value::<bool>()? {
                return Ok(());
            }
            match kind {
                Kind::Message { definition } => nested(field.tag, definition, input, bytes),
                kind => scalar(field.tag, kind, input, bytes, false),
            }
        }
        (Cardinality::Repeated, kind) => {
            let count: usize = input.value()?;
            match kind {
                Kind::Message { definition } => {
                    for _ in 0..count {
                        nested(field.tag, definition, input, bytes)?;
                    }
                }
                Kind::String | Kind::Bytes => {
                    for _ in 0..count {
                        scalar(field.tag, kind, input, bytes, false)?;
                    }
                }
                kind if count > 0 => {
                    let mut packed = Vec::new();
                    for _ in 0..count {
                        number(kind, input, &mut packed)?;
                    }
                    delimited(field.tag, &packed, bytes);
                }
                _ => {}
            }
            Ok(())
        }
        (Cardinality::Variant, _) => Err(MessageError::borrowed("a variant field belongs to a oneof")),
    }
}

fn nested(tag: u32, definition: &Definition, input: &mut Reader<'_>, bytes: &mut Vec<u8>) -> Result<(), MessageError> {
    let mut body = Vec::new();
    message(definition, input, &mut body)?;
    delimited(tag, &body, bytes);
    Ok(())
}

fn scalar(tag: u32, kind: &Kind, input: &mut Reader<'_>, bytes: &mut Vec<u8>, omit_default: bool) -> Result<(), MessageError> {
    match kind {
        Kind::String => {
            let text: &str = input.value()?;
            if !(omit_default && text.is_empty()) {
                delimited(tag, text.as_bytes(), bytes);
            }
        }
        Kind::Bytes => {
            let length: usize = input.value()?;
            let value = input.take(length)?;
            if !(omit_default && value.is_empty()) {
                delimited(tag, value, bytes);
            }
        }
        kind => {
            let mut value = Vec::new();
            if number(kind, input, &mut value)? || !omit_default {
                encode_key(tag, wire(kind), bytes);
                bytes.extend_from_slice(&value);
            }
        }
    }
    Ok(())
}

// Write One Numeric Value Without Its Key; True When It Differs From Default.
fn number(kind: &Kind, input: &mut Reader<'_>, bytes: &mut Vec<u8>) -> Result<bool, MessageError> {
    Ok(match kind {
        Kind::Bool => {
            let value: bool = input.value()?;
            encode_varint(u64::from(value), bytes);
            value
        }
        Kind::U32 => {
            let value: u32 = input.value()?;
            encode_varint(u64::from(value), bytes);
            value != 0
        }
        Kind::U64 => {
            let value: u64 = input.value()?;
            encode_varint(value, bytes);
            value != 0
        }
        Kind::I32 => {
            let value: i32 = input.value()?;
            encode_varint(i64::from(value) as u64, bytes);
            value != 0
        }
        Kind::I64 => {
            let value: i64 = input.value()?;
            encode_varint(value as u64, bytes);
            value != 0
        }
        Kind::F32 => {
            let value: f32 = input.value()?;
            bytes.extend_from_slice(&value.to_le_bytes());
            value.to_bits() != 0
        }
        Kind::F64 => {
            let value: f64 = input.value()?;
            bytes.extend_from_slice(&value.to_le_bytes());
            value.to_bits() != 0
        }
        Kind::String | Kind::Bytes | Kind::Message { .. } => {
            return Err(MessageError::borrowed("expected a numeric protobuf field"));
        }
    })
}

fn wire(kind: &Kind) -> WireType {
    match kind {
        Kind::F32 => WireType::ThirtyTwoBit,
        Kind::F64 => WireType::SixtyFourBit,
        Kind::String | Kind::Bytes | Kind::Message { .. } => WireType::LengthDelimited,
        _ => WireType::Varint,
    }
}

fn delimited(tag: u32, value: &[u8], bytes: &mut Vec<u8>) {
    encode_key(tag, WireType::LengthDelimited, bytes);
    encode_varint(value.len() as u64, bytes);
    bytes.extend_from_slice(value);
}

/* --- Protobuf To Postcard --- */

fn decode_message(definition: &Definition, input: DecodeInput<'_>, bytes: &mut Vec<u8>) -> Result<(), MessageError> {
    let fields = (definition.fields)();
    if !oneof(fields) {
        return decode_members(definition, input, bytes);
    }
    let tags = fields.iter().map(|field| field.tag).collect::<Vec<_>>();
    let (index, selected) = match input.variant(&tags)? {
        Some((tag, selected)) => (tags.iter().position(|known| *known == tag).expect("selected declared variant"), selected),
        None => (definition.default_variant.ok_or_else(|| MessageError::borrowed("missing oneof variant"))?, input.child(&[])?),
    };
    let field = fields.get(index).ok_or_else(|| MessageError::borrowed("default variant lies outside the oneof"))?;
    u32::try_from(index).map_err(|_| MessageError::borrowed("oneof variant index overflows"))?.write(bytes);
    decode_members(payload(field)?, selected, bytes)
}

fn decode_members(definition: &Definition, input: DecodeInput<'_>, bytes: &mut Vec<u8>) -> Result<(), MessageError> {
    let fields = (definition.fields)();
    // Check Every Occurrence Of A Known Field Before Any Is Selected
    input.fields(&mut |occurrence| {
        if let Some(field) = fields.iter().find(|field| field.tag == occurrence.tag) {
            check(field, occurrence)?;
        }
        Ok(())
    })?;
    for field in fields {
        decode_member(field, input, bytes)?;
    }
    Ok(())
}

fn check(field: &Field, occurrence: DecodeField<'_>) -> Result<(), MessageError> {
    match (&field.kind, field.cardinality) {
        (Kind::String, _) => occurrence.text().map(drop),
        (Kind::Bytes | Kind::Message { .. }, _) => occurrence.bytes().map(drop),
        (kind, Cardinality::Repeated) => occurrence.scalars(decode_scalar(kind), &mut |_| Ok(())),
        (kind, _) => occurrence.scalar(decode_scalar(kind)).map(drop),
    }
}

fn decode_member(field: &Field, input: DecodeInput<'_>, bytes: &mut Vec<u8>) -> Result<(), MessageError> {
    let tag = field.tag;
    match (field.cardinality, &field.kind) {
        (Cardinality::Required, Kind::Message { definition }) => decode_message(definition, input.field(tag)?, bytes),
        (Cardinality::Optional, Kind::Message { definition }) => {
            let present = last(input, tag)?.is_some();
            present.write(bytes);
            if present {
                decode_message(definition, input.field(tag)?, bytes)?;
            }
            Ok(())
        }
        (Cardinality::Repeated, Kind::Message { definition }) => {
            count(input, tag)?.write(bytes);
            input.fields(&mut |occurrence| {
                if occurrence.tag == tag {
                    decode_message(definition, input.child(occurrence.bytes()?)?, bytes)?;
                }
                Ok(())
            })
        }
        (Cardinality::Required, kind) => match last(input, tag)? {
            Some(occurrence) => decode_scalar_value(kind, occurrence, bytes),
            None => default(kind, bytes),
        },
        (Cardinality::Optional, kind) => {
            let occurrence = last(input, tag)?;
            occurrence.is_some().write(bytes);
            occurrence.map_or(Ok(()), |occurrence| decode_scalar_value(kind, occurrence, bytes))
        }
        (Cardinality::Repeated, kind @ (Kind::String | Kind::Bytes)) => {
            count(input, tag)?.write(bytes);
            input.fields(&mut |occurrence| {
                if occurrence.tag == tag {
                    decode_scalar_value(kind, occurrence, bytes)?;
                }
                Ok(())
            })
        }
        (Cardinality::Repeated, kind) => {
            let scalar = decode_scalar(kind);
            let mut values = 0usize;
            input.fields(&mut |occurrence| {
                if occurrence.tag == tag {
                    occurrence.scalars(scalar, &mut |_| {
                        values += 1;
                        Ok(())
                    })?;
                }
                Ok(())
            })?;
            values.write(bytes);
            input.fields(&mut |occurrence| {
                if occurrence.tag == tag {
                    occurrence.scalars(scalar, &mut |value| write_number(kind, value, bytes))?;
                }
                Ok(())
            })
        }
        (Cardinality::Variant, _) => Err(MessageError::borrowed("a variant field belongs to a oneof")),
    }
}

fn last<'a>(input: DecodeInput<'a>, tag: u32) -> Result<Option<DecodeField<'a>>, MessageError> {
    let mut selected = None;
    input.fields(&mut |occurrence| {
        if occurrence.tag == tag {
            selected = Some(occurrence);
        }
        Ok(())
    })?;
    Ok(selected)
}

fn count(input: DecodeInput<'_>, tag: u32) -> Result<usize, MessageError> {
    let mut count = 0usize;
    input.fields(&mut |occurrence| {
        if occurrence.tag == tag {
            count += 1;
        }
        Ok(())
    })?;
    Ok(count)
}

fn decode_scalar(kind: &Kind) -> DecodeScalar {
    match kind {
        Kind::F32 => DecodeScalar::Bits32,
        Kind::F64 => DecodeScalar::Bits64,
        _ => DecodeScalar::Varint,
    }
}

fn decode_scalar_value(kind: &Kind, occurrence: DecodeField<'_>, bytes: &mut Vec<u8>) -> Result<(), MessageError> {
    match kind {
        Kind::String => {
            let text = occurrence.text()?;
            text.len().write(bytes);
            bytes.extend_from_slice(text.as_bytes());
            Ok(())
        }
        Kind::Bytes => {
            let value = occurrence.bytes()?;
            value.len().write(bytes);
            bytes.extend_from_slice(value);
            Ok(())
        }
        kind => write_number(kind, occurrence.scalar(decode_scalar(kind))?, bytes),
    }
}

fn write_number(kind: &Kind, value: DecodeValue<'_>, bytes: &mut Vec<u8>) -> Result<(), MessageError> {
    match (kind, value) {
        (Kind::Bool, DecodeValue::Varint(value)) => (value != 0).write(bytes),
        (Kind::U32, DecodeValue::Varint(value)) => (value as u32).write(bytes),
        (Kind::U64, DecodeValue::Varint(value)) => value.write(bytes),
        (Kind::I32, DecodeValue::Varint(value)) => (value as i32).write(bytes),
        (Kind::I64, DecodeValue::Varint(value)) => (value as i64).write(bytes),
        (Kind::F32, DecodeValue::Bits32(value)) => f32::from_bits(value).write(bytes),
        (Kind::F64, DecodeValue::Bits64(value)) => f64::from_bits(value).write(bytes),
        _ => return Err(MessageError::borrowed("protobuf scalar wire type mismatch")),
    }
    Ok(())
}

fn default(kind: &Kind, bytes: &mut Vec<u8>) -> Result<(), MessageError> {
    match kind {
        Kind::Bool => false.write(bytes),
        Kind::U32 => 0u32.write(bytes),
        Kind::U64 => 0u64.write(bytes),
        Kind::I32 => 0i32.write(bytes),
        Kind::I64 => 0i64.write(bytes),
        Kind::F32 => 0f32.write(bytes),
        Kind::F64 => 0f64.write(bytes),
        Kind::String | Kind::Bytes => 0usize.write(bytes),
        Kind::Message { .. } => {
            return Err(MessageError::borrowed("expected a scalar protobuf field"));
        }
    }
    Ok(())
}

/* === Tests === */