Skip to content

Commit

Permalink
chore: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mookums committed Dec 18, 2024
1 parent c3e26ed commit d03e166
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 28 deletions.
3 changes: 0 additions & 3 deletions examples/basic/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ pub fn main() !void {
\\ </body>
\\ </html>
;

const body = try std.fmt.allocPrint(ctx.allocator, body_fmt, .{ctx.state.*});

// This is the standard response and what you
// will usually be using. This will send to the
// client and then continue to await more requests.
Expand All @@ -63,7 +61,6 @@ pub fn main() !void {
try ctx.allocator.dupe(u8, b)
else
"";

try ctx.respond(.{
.status = .OK,
.mime = http.Mime.HTML,
Expand Down
1 change: 0 additions & 1 deletion examples/fs/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ pub fn main() !void {
\\ </body>
\\ </html>
;

try ctx.respond(.{
.status = .OK,
.mime = http.Mime.HTML,
Expand Down
1 change: 0 additions & 1 deletion examples/minram/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ pub fn main() !void {
\\ </body>
\\ </html>
;

try ctx.respond(.{
.status = .OK,
.mime = http.Mime.HTML,
Expand Down
1 change: 0 additions & 1 deletion examples/tls/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ pub fn main() !void {
\\ </body>
\\ </html>
;

try ctx.respond(.{
.status = .OK,
.mime = http.Mime.HTML,
Expand Down
4 changes: 1 addition & 3 deletions examples/unix/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ pub fn main() !void {
});
defer t.deinit();

var router = Router.init({}, &[_]Route{
Route.init("/").get(root_handler)
}, .{});
var router = Router.init({}, &[_]Route{Route.init("/").get(root_handler)}, .{});

try t.entry(
&router,
Expand Down
3 changes: 1 addition & 2 deletions examples/valgrind/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub fn main() !void {
\\ </body>
\\ </html>
;

try ctx.respond(.{
.status = .OK,
.mime = http.Mime.HTML,
Expand All @@ -44,7 +43,7 @@ pub fn main() !void {
pub fn handler_fn(ctx: *Context) !void {
ctx.runtime.stop();
}
}.handler_fn)
}.handler_fn),
}, .{});

var t = try Tardy.init(.{
Expand Down
7 changes: 5 additions & 2 deletions src/http/router/fs_dir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,16 @@ pub fn FsDir(Server: type, AppState: type) type {

//TODO Can we do this once and for all at initialization?
// Resolving the base directory.
const resolved_dir = try std.fs.path.resolve(ctx.allocator, &[_][]const u8{ dir_path });
const resolved_dir = try std.fs.path.resolve(ctx.allocator, &[_][]const u8{dir_path});
defer ctx.allocator.free(resolved_dir);

// Resolving the requested file.
const search_path = ctx.captures[0].remaining;
const resolved_file_path = blk: {
const file_path = std.fs.path.resolve(ctx.allocator, &[_][]const u8{ dir_path, search_path }) catch {
const file_path = std.fs.path.resolve(
ctx.allocator,
&[_][]const u8{ dir_path, search_path },
) catch {
try ctx.respond(.{
.status = .@"Not Found",
.mime = Mime.HTML,
Expand Down
17 changes: 10 additions & 7 deletions src/http/router/route.zig
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ pub fn Route(comptime Server: type, comptime AppState: type) type {
// it might be more beneficial to using caching.
if (comptime bytes.len > 1024) {
@setEvalBranchQuota(1_000_000);
const etag = comptime std.fmt.comptimePrint("\"{d}\"", .{std.hash.Wyhash.hash(0, bytes)});
const etag = comptime std.fmt.comptimePrint(
"\"{d}\"",
.{std.hash.Wyhash.hash(0, bytes)},
);
ctx.response.headers.putAssumeCapacity("ETag", etag[0..]);

if (ctx.request.headers.get("If-None-Match")) |match| {
Expand Down Expand Up @@ -208,14 +211,14 @@ pub fn Route(comptime Server: type, comptime AppState: type) type {
);

return self
// Set the new path.
// Set the new path.
.set_path(url_with_match_all)
// Set GET handler.
// Set GET handler.
.get(struct {
fn handler_fn(ctx: *Context) !void {
try FsDir.handler_fn(ctx, dir_path);
}
}.handler_fn);
fn handler_fn(ctx: *Context) !void {
try FsDir.handler_fn(ctx, dir_path);
}
}.handler_fn);
}
};
}
9 changes: 6 additions & 3 deletions src/http/router/routing_trie.zig
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub fn RoutingTrie(comptime Server: type, comptime AppState: type) type {
return Node{
.token = self.token,
.route = self.route,
.children = self.children.with_kvs(&[_]ChildrenMap.KV{.{ token, node }})
.children = self.children.with_kvs(&[_]ChildrenMap.KV{.{ token, node }}),
};
}
};
Expand Down Expand Up @@ -136,7 +136,6 @@ pub fn RoutingTrie(comptime Server: type, comptime AppState: type) type {
}
std.debug.print("\n", .{});


print_node(node, depth + 1);
}
}
Expand Down Expand Up @@ -481,7 +480,11 @@ test "Routing with Queries" {

{
// Purposefully have too many queries.
const captured = try s.get_route("/item/100/price/283.21?a=1&b=2&c=3&d=4&e=5&f=6&g=7&h=8&i=9&j=10&k=11", captures[0..], &q);
const captured = try s.get_route(
"/item/100/price/283.21?a=1&b=2&c=3&d=4&e=5&f=6&g=7&h=8&i=9&j=10&k=11",
captures[0..],
&q,
);
try testing.expectEqual(null, captured);
}
}
10 changes: 5 additions & 5 deletions src/http/router/token_hash_map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ const std = @import("std");
const Token = @import("routing_trie.zig").Token;

/// Errors of get function.
pub const MapGetErrors = error {
pub const MapGetErrors = error{
NotFound,
};

/// Type of a token hash.
pub const Hash = u64;

/// Type of a hash entry in hashed array.
pub const HashEntry = struct{Hash, usize};
pub const HashEntry = struct { Hash, usize };

/// In-place sort of the given array at compile time.
/// Implementation reference: https://github.com/Koura/algorithms/blob/b1dd07147a34554543994b2c033fae64a2202933/sorting/quicksort.zig
/// https://github.com/Koura/algorithms/blob/b1dd07147a34554543994b2c033fae64a2202933/sorting/quicksort.zig
fn sort(A: []HashEntry, lo: usize, hi: usize) void {
if (lo < hi) {
const p = partition(A, lo, hi);
Expand Down Expand Up @@ -111,7 +111,7 @@ pub fn TokenHashMap(V: type) type {
pub fn get_kvs(self: *const Self) []const KV {
var kvs: [self.keys.len]KV = undefined;
for (&kvs, self.keys, self.values) |*kv, key, value| {
kv.* = .{key, value};
kv.* = .{ key, value };
}
return &kvs;
}
Expand Down Expand Up @@ -160,7 +160,7 @@ pub fn TokenHashMap(V: type) type {

// Search in the sorted hashes array.
const hash_index = std.sort.binarySearch(HashEntry, hash, self.hashes, {}, struct {
fn f (_: void, searched_key: Hash, mid_item: HashEntry) std.math.Order {
fn f(_: void, searched_key: Hash, mid_item: HashEntry) std.math.Order {
if (searched_key < mid_item[0]) return std.math.Order.lt;
if (searched_key > mid_item[0]) return std.math.Order.gt;
if (searched_key == mid_item[0]) return std.math.Order.eq;
Expand Down

0 comments on commit d03e166

Please sign in to comment.