summaryrefslogtreecommitdiff
path: root/backend/src/Models/User.zig
blob: 1b7943af194c6e6d4ab665c74b6b0d361908d6c9 (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 pg = @import("pg");

const RoleError = error {
    NotSupported
};

const RolesMap = std.static_string_map.StaticStringMap(Role).initComptime(.{
    .{ "user", .user },
    .{ "editor", .editor },
});

pub const Role = enum {
    user,
    editor,

    pub fn ToString(self: Role) []const u8 {
        return switch (self) {
            .user => "user",
            .editor => "editor"
        };
    }

    pub fn FromString(string: []const u8) !Role {
        return RolesMap.get(string) orelse RoleError.NotSupported;
    }
};

pub const User = struct {
    // UUID
    Id: [36]u8,
    Username: []const u8,
    PasswordHash: []const u8,
    Role: []const u8

    // pub fn Map(row: *const pg.Row) !User {
    // }
};

test "Roles transformation" {
    const roleString = "user";
    const roleVar: Role = .editor;

    try std.testing.expectEqualStrings("editor", Role.ToString(roleVar));
    const actual = try Role.FromString(roleString);
    try std.testing.expectEqual(Role.user, actual);
}