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

pub const Segment = union(enum) {
    literal: []const u8,
    identifier: []const u8,

    pub fn format(self: *const @This(), writer: *std.Io.Writer) !void {
        switch (self.*) {
            .literal => |l| try writer.print("{s}", .{l}),
            .identifier => |l| try writer.print("{{{s}}}", .{l}),
        }
    }
};

segments: []const Segment,

pub fn fromAny(comptime route: anytype) @This() {
    var segments: []const Segment = &.{};

    inline for(route) |r| {
        const segment: []const Segment = &.{ switch (@TypeOf(r)) {
            @EnumLiteral() => .{ .identifier = @tagName(r) },
            else => .{ .literal = r },
        } };

        segments = segments ++ segment;
    }

    return .{ .segments = segments };
}

pub fn format(self: *const @This(), writer: *std.Io.Writer) !void {
    for (self.segments) |*seg| {
        try writer.print("/{f}", .{seg});
    }
}


pub fn shift(self: *const @This()) struct { Segment, @This() } {
    if (self.segments.len == 0) {
        @compileError("shift on empty route");
    }

    return .{ self.segments[0], self.segments[1..] };
}