all repos — pba-zig @ 7380b30e39aad95714215d2a1bc9b6583168f442

Protobufs But Awesomer as rewritten in Zig

src/ingest.zig (view raw)

 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
const lex = @import("lex.zig");

pub const Scalar = enum {
    U8,
    U16,
    U32,
    U64,

    pub fn width(self: Scalar) usize {
        return switch (self) {
            Scalar.U8 => 1,
            Scalar.U16 => 2,
            Scalar.U32 => 4,
            Scalar.U64 => 8,
        };
    }

    pub fn pba_name(self: Scalar) []const u8 {
        return switch (self) {
            Scalar.U8 => "u8",
            Scalar.U16 => "u16",
            Scalar.U32 => "u32",
            Scalar.U64 => "u64",
        };
    }

    pub fn c_type(self: Scalar) []const u8 {
        return switch (self) {
            Scalar.U8 => "uint8_t",
            Scalar.U16 => "uint16_t",
            Scalar.U32 => "uint32_t",
            Scalar.U64 => "uint64_t",
        };
    }
};

pub const ArrayLength = union(enum) {
    fixed: usize,
    ref: []const u8,
    open: void,
};

pub const FieldType = union(enum) {
    single: Scalar,
    array: struct {
        scalar_type: Scalar,
        length: ArrayLength,
    },
};

pub const Field = struct {
    name: []const u8,
    field_type: FieldType,
    position: lex.Position,
};

pub const Builtin = enum {
    PRECEDING,
};

pub const Value = union(enum) {
    literal: usize,
    field_ref: []const u8,
    builtin: Builtin,
};

pub const AeadAlgorithm = union(enum) {
    chacha20_poly1305: struct {
        nonce: Value,
        key: Value,
        aad: Value,
    },

    pub fn tag_len(self: AeadAlgorithm) usize {
        return switch (self) {
            .chacha20_poly1305 => 16,
        };
    }
};

pub const DigestAlgorithm = enum {
    SHA256,
    SHA512,

    pub fn digest_len(self: DigestAlgorithm) usize {
        return switch (self) {
            DigestAlgorithm.SHA256 => 32,
            DigestAlgorithm.SHA512 => 64,
        };
    }
};

pub const Block = struct {
    contents: union(enum) {
        clear: []Field,
        aead: []struct {
            algorithm: AeadAlgorithm,
        },
    },
    pos: lex.Position,
};

pub const Message = struct {
    name: []const u8,
    params: []Field,
    blocks: []Block,
    pos: lex.Position,
    digest: ?DigestAlgorithm,
    fragment_key: ?Value,
};

pub const ProtocolInstance = struct {
    version: usize,
    header: []Field,
    messages: []Message,
    pos: lex.Position,
};

pub const Protocol = struct {
    name: []const u8,
    mtu: usize,
    instances: []ProtocolInstance,
};

const auto_header_len = 6;