Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const http = struct {
- pub const Version = undefined;
- const Headers = undefined;
- pub const request_module = struct {
- pub fn Request(comptime state_type: type, comptime path_arguments_type: type) type {
- _ = state_type;
- _ = path_arguments_type;
- return struct {
- const Self = undefined;
- };
- }
- };
- pub const StatusCode = undefined;
- pub const Request = request_module.Request;
- pub const Response = struct {
- pub fn init(version: Version, status_code: StatusCode, headers: Headers, body: ?[]const u8) Response {
- _ = version;
- _ = status_code;
- _ = headers;
- _ = body;
- @trap();
- }
- };
- pub const Method = enum {
- get,
- };
- const handler_module = struct {
- pub const PathArgumentsLen = 200;
- pub const PathArguments = stack_hashmap.StackStringHashMap(void, PathArgumentsLen);
- pub fn HandlerFn(comptime state_type: type) type {
- return *const fn (req: *Request(state_type, void), args: []const []const u8) Response;
- }
- pub fn MethodHandlers(comptime state_type: type) type {
- return stack_hashmap.StackHashMap(Method, HandlerFn(state_type), std.meta.fields(Method).len);
- }
- };
- pub fn NodeAllocator(comptime state_type: type, comptime node_count: usize) type {
- return struct {
- data: [node_count]Node(state_type, node_count) = undefined,
- const Self = @This();
- pub fn init() Self {
- return .{};
- }
- };
- }
- const stack_hashmap = struct {
- pub fn StackStringHashMap(comptime V: type, comptime size: usize) type {
- return StackHashMap([]const u8, V, size);
- }
- pub fn StackHashMap(comptime K: type, comptime V: type, comptime size: usize) type {
- return struct {
- keys: [size]K = undefined,
- const Self = @This();
- pub fn init() Self {
- return .{};
- }
- /// This method fails when there is no memory available anymore
- pub fn put(self: *Self, key: []const u8, value: V) !void {
- _ = self;
- _ = key;
- _ = value;
- @trap();
- }
- };
- }
- };
- const route_module = struct {
- pub const Route = []RouteSegment;
- pub const RouteSegment = struct {};
- pub fn parse_route(route: []const u8, out: Route) Route {
- var index: usize = 0;
- var split = std.mem.splitScalar(u8, route[1..], '/');
- while (split.next()) |segment| : (index += 1) {
- const is_arg = if (segment[0] == '$') true else blk: {
- break :blk false;
- };
- _ = is_arg;
- }
- return out[0..index];
- }
- };
- pub fn Node(comptime state_type: type, comptime node_children_count: usize) type {
- return struct {
- name: []const u8,
- is_arg: bool,
- handlers: handler_module.MethodHandlers(state_type),
- children: ?Children,
- const Self = @This();
- const Children = stack_hashmap.StackStringHashMap(*Self, node_children_count);
- pub fn put(
- comptime self: *Self,
- comptime node_allocator: *NodeAllocator(state_type, 400 * 400),
- comptime route: route_module.Route,
- comptime method: Method,
- comptime handler: *const fn (req: *anyopaque) Response,
- comptime arguments: *handler_module.PathArguments,
- ) !void {
- //If this is the last segment of the route
- _ = route;
- _ = self;
- _ = method;
- _ = handler;
- _ = arguments;
- _ = node_allocator;
- }
- };
- }
- pub fn Router(comptime state_type: type, comptime node_count: usize) type {
- return struct {
- root_node: Node(state_type, node_count),
- state: state_type,
- node_allocator: NodeAllocator(state_type, node_count),
- const Self = @This();
- pub fn init(comptime state: state_type) Self {
- return .{
- .root_node = .{
- .is_arg = false,
- .name = "/",
- .handlers = handler_module.MethodHandlers(state_type).init(),
- .children = null,
- },
- .state = state,
- .node_allocator = NodeAllocator(state_type, node_count).init(),
- };
- }
- pub fn add_route(comptime self: *Self, comptime route: []const u8, comptime method: Method, comptime handler: *const fn (req: *anyopaque) Response) !void {
- comptime var route_out: [300]route_module.RouteSegment = undefined;
- const route_out_slice = route_module.parse_route(route, &route_out);
- comptime var arguments = handler_module.PathArguments.init();
- comptime try self.root_node.put(&self.node_allocator, route_out_slice, method, handler, &arguments);
- }
- };
- }
- };
- const std = @import("std");
- pub fn main() !void {
- var buf: [4_294_967]u8 = undefined;
- var fba = std.heap.FixedBufferAllocator.init(&buf);
- const allocator = fba.allocator();
- _ = allocator;
- comptime var router = http.Router(void, 400 * 400).init({});
- comptime try router.add_route("/posts/$user", http.Method.get, @ptrCast(@alignCast(&struct {
- fn a(req: *http.Request(void, void)) http.Response {
- _ = req;
- @trap();
- }
- }.a)));
- const router_const = router;
- _ = router_const;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement