From 183d60a6e87230cc767c56900b94c9c694596de1 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Tue, 26 Sep 2023 06:51:06 +0100 Subject: Move protocol definitions into a subfolder --- src/main.zig | 39 ++++++++------------------------------- 1 file changed, 8 insertions(+), 31 deletions(-) (limited to 'src/main.zig') diff --git a/src/main.zig b/src/main.zig index 8bc4649..f5941b7 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,18 +1,7 @@ const std = @import("std"); const testing = std.testing; -const StartupMessage = @import("startup_message.zig"); -const AuthenticationRequest = @import("authentication_request.zig"); -const PasswordMessage = @import("password_message.zig"); -const ErrorResponse = @import("error_response.zig"); -const ReadyForQuery = @import("ready_for_query.zig"); -const ParameterStatus = @import("parameter_status.zig"); -const BackendKeyData = @import("backend_key_data.zig"); -const Query = @import("query.zig"); -const DataRow = @import("data_row.zig"); -const RowDescription = @import("row_description.zig"); -const CommandComplete = @import("command_complete.zig"); -const CopyInResponse = @import("copy_in_response.zig"); const Conn = @import("conn.zig"); +const Proto = @import("proto/proto.zig"); pub const ProtocolError = error{ InvalidProtocolVersion, @@ -58,22 +47,21 @@ pub fn read_message(comptime msg_type: type, allocator: std.mem.Allocator, strea if (!@hasDecl(msg_type, "Tag")) @compileError("msg_type must have a Tag declaration!"); if (!@hasDecl(msg_type, "read")) @compileError("msg_type must have a read() function!"); const len = try stream_reader.readIntBig(u32); - const buf = try allocator.alloc(u8, @as(u32, @intCast(len-4))); + const buf = try allocator.alloc(u8, @as(u32, @intCast(len - 4))); defer allocator.free(buf); try stream_reader.readNoEof(buf); return try msg_type.read(allocator, buf); } - pub fn diagnosticReader(comptime n: usize, base_reader: anytype) DiagnosticReader(n, @TypeOf(base_reader)) { - return .{.child_reader = base_reader}; + return .{ .child_reader = base_reader }; } // keeps a buffer of the last n bytes read pub fn DiagnosticReader(comptime n: usize, comptime ReaderType: anytype) type { return struct { child_reader: ReaderType, - ring: [n]u8 = [_]u8{0}**n, + ring: [n]u8 = [_]u8{0} ** n, pos: usize = 0, pub const Error = ReaderType.Error; @@ -97,8 +85,8 @@ pub fn DiagnosticReader(comptime n: usize, comptime ReaderType: anytype) type { pub fn get(self: @This(), allocator: std.mem.Allocator) ![]const u8 { var buf = try allocator.alloc(u8, n); errdefer allocator.free(buf); - @memcpy(buf[0..(n-self.pos)], self.ring[self.pos..n]); - @memcpy(buf[(n-self.pos)..n], self.ring[0..self.pos]); + @memcpy(buf[0..(n - self.pos)], self.ring[self.pos..n]); + @memcpy(buf[(n - self.pos)..n], self.ring[0..self.pos]); return buf; } }; @@ -110,7 +98,7 @@ test "diagnostc reader" { var fbs = std.io.fixedBufferStream(string); var dr = diagnosticReader(15, fbs.reader()); var reader = dr.reader(); - var buf = [_]u8{0}**20; + var buf = [_]u8{0} ** 20; try reader.readNoEof(&buf); const diag = try dr.get(a); defer a.free(diag); @@ -118,17 +106,6 @@ test "diagnostc reader" { } test { - _ = StartupMessage; - _ = AuthenticationRequest; - _ = PasswordMessage; - _ = ErrorResponse; + _ = Proto; _ = Conn; - _ = ReadyForQuery; - _ = ParameterStatus; - _ = BackendKeyData; - _ = Query; - _ = DataRow; - _ = RowDescription; - _ = CommandComplete; - _ = CopyInResponse; } -- cgit v1.2.3-ZIG