aboutsummaryrefslogtreecommitdiff
path: root/src/http/Route.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/http/Route.zig')
-rw-r--r--src/http/Route.zig45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/http/Route.zig b/src/http/Route.zig
new file mode 100644
index 0000000..bc86544
--- /dev/null
+++ b/src/http/Route.zig
@@ -0,0 +1,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..] };
+}