aboutsummaryrefslogtreecommitdiff
path: root/src/http/handler/Interface.zig
blob: 22384248e73554cc2871669775f6700fb97edbe0 (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
const std = @import("std");
const Response = @import("../Response.zig");
const Route = @import("../Route.zig");

route: Route,
base: *const anyopaque,
handler: *const fn (
    *const anyopaque,
    std.mem.Allocator,
    std.mem.Allocator,
    std.Io,
    *std.http.Server.Request,
) Response,

pub fn from(instance: anytype, route: Route) @This() {
    const PtrT = @TypeOf(instance);
    const func = struct {
        pub fn handle(
            base: *const anyopaque,
            gpa: std.mem.Allocator,
            arena: std.mem.Allocator,
            io: std.Io,
            request: *std.http.Server.Request,
        ) Response {
            return @as(PtrT, @ptrCast(@alignCast(base))).handle(gpa, arena, io, request);
        }
    }.handle;

    return .{
        .base = @ptrCast(instance),
        .handler = func,
        .route = route,
    };
}

pub fn handle(
    self: *const @This(),
    gpa: std.mem.Allocator,
    arena: std.mem.Allocator,
    io: std.Io,
    request: *std.http.Server.Request,
) Response {
    return self.handler(self.base, gpa, arena, io, request);
}