Add router helper fn

This commit is contained in:
jaina heartles 2022-11-24 03:55:47 -08:00
parent 503ab62607
commit 039377f168
1 changed files with 8 additions and 8 deletions

View File

@ -176,6 +176,14 @@ pub fn Router(comptime Routes: []const type) type {
}
};
}
fn fieldTypes(comptime Tuple: type) []const type {
var types: [Tuple.len]type = undefined;
for (std.meta.fields(Tuple)) |f, i| types[i] = f.field_type;
return &types;
}
pub fn router(routes: anytype) Router(fieldTypes(@TypeOf(routes))) {
return Router(fieldTypes(@TypeOf(routes))){ .routes = routes };
}
// helper function for doing route analysis
fn pathMatches(route: []const u8, path: []const u8) bool {
@ -219,14 +227,6 @@ pub const Route = struct {
error.RouteMismatch;
}
};
pub fn ComptimeRoute(comptime desc: Route.Desc) type {
return struct {
const route = Route{ .desc = desc };
pub fn handle(_: @This(), req: anytype, res: anytype, ctx: anytype, next: anytype) !void {
return route.handle(req, res, ctx, next);
}
};
}
pub fn Mount(comptime route: []const u8) type {
return struct {