srctree

Gregory Mullen parent 41dbcf31 1a224c2f
move some functions in Frame.zig pt 1

src/frame.zig added: 27, removed: 27, total 0
@@ -128,6 +128,32 @@ pub fn sendRawSlice(vrs: *Frame, slice: []const u8) NetworkError!void {
};
}
 
/// Takes a any object, that can be represented by json, converts it into a
/// json string, and sends to the client.
pub fn sendJSON(vrs: *Frame, json: anytype, comptime code: std.http.Status) !void {
if (code == .no_content) {
@compileError("Sending JSON is not supported with status code no content");
}
 
vrs.status = code;
vrs.content_type = .{
.base = .{ .application = .json },
.parameter = .@"utf-8",
};
 
try vrs.quickStart();
const data = std.json.stringifyAlloc(vrs.alloc, json, .{
.emit_null_optional_fields = false,
}) catch |err| {
log.err("Error trying to print json {}", .{err});
return error.Unknown;
};
vrs.writeAll(data) catch |err| switch (err) {
error.BrokenPipe => |e| return e,
else => unreachable,
};
}
 
pub fn init(a: Allocator, req: *const Request, auth: Auth.Provider) !Frame {
return .{
.alloc = a,
@@ -327,32 +353,6 @@ pub fn sendError(vrs: *Frame, comptime code: std.http.Status) !void {
return Router.defaultResponse(code)(vrs);
}
 
/// Takes a any object, that can be represented by json, converts it into a
/// json string, and sends to the client.
pub fn sendJSON(vrs: *Frame, json: anytype, comptime code: std.http.Status) !void {
if (code == .no_content) {
@compileError("Sending JSON is not supported with status code no content");
}
 
vrs.status = code;
vrs.content_type = .{
.base = .{ .application = .json },
.parameter = .@"utf-8",
};
 
try vrs.quickStart();
const data = std.json.stringifyAlloc(vrs.alloc, json, .{
.emit_null_optional_fields = false,
}) catch |err| {
log.err("Error trying to print json {}", .{err});
return error.Unknown;
};
vrs.writeAll(data) catch |err| switch (err) {
error.BrokenPipe => |e| return e,
else => unreachable,
};
}
 
/// This function may be removed in the future
pub fn quickStart(vrs: *Frame) NetworkError!void {
if (vrs.status == null) vrs.status = .ok;