aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
blob: e540256e969bb04de10df12286220cb7e2660be1 (plain)
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
const std = @import("std");
const testing = std.testing;
const StartupMessage = @import("startup_message.zig");
const AuthenticationOk = @import("authentication_ok.zig");
const AuthenticationCleartextPassword = @import("authentication_cleartext_password.zig");
const PasswordMessage = @import("password_message.zig");
const ErrorResponse = @import("error_response.zig");

pub const ProtocolError = error{
    InvalidProtocolVersion,
    InvalidKeyValuePair,
    InvalidMessageLength,
    InvalidAuthType,
    MissingField,
};

pub const ClientError = error{
    UnsupportedAuthType,
};

pub const AuthType = enum(u32) {
    AuthTypeOk = 0,
    AuthTypeCleartextPassword = 3,
};

// Fallible version of enumFromInt
pub fn enum_from_int(comptime e: type, i: anytype) ?e {
    const enum_ti = @typeInfo(e);
    if (enum_ti != .Enum) @compileError("e should be an enum but instead it's a " ++ @typeName(e));
    const ei = enum_ti.Enum;
    if (@TypeOf(i) != ei.tag_type) @compileError("i should be of type " ++ @typeName(e) ++ " but instead it's " ++ @typeName(@TypeOf(i)));
    inline for (ei.fields) |field| {
        if (field.value == i) {
            return @enumFromInt(i);
        }
    } else {
        return null;
    }
}

test {
    _ = StartupMessage;
    _ = AuthenticationOk;
    _ = AuthenticationCleartextPassword;
    _ = PasswordMessage;
    _ = ErrorResponse;
}