mirror of
https://git.robbyzambito.me/zaprus/
synced 2026-02-04 11:44:49 +00:00
Simplify tagged union
This commit is contained in:
@@ -1,11 +1,3 @@
|
|||||||
/// Type tag for Message union.
|
|
||||||
/// This is the first value in the actual packet sent over the network.
|
|
||||||
pub const PacketType = enum(u16) {
|
|
||||||
relay = 0x003C,
|
|
||||||
connection = 0x00E9,
|
|
||||||
_,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const MessageTypeError = error{
|
pub const MessageTypeError = error{
|
||||||
NotImplementedSaprusType,
|
NotImplementedSaprusType,
|
||||||
UnknownSaprusType,
|
UnknownSaprusType,
|
||||||
@@ -16,16 +8,18 @@ pub const MessageParseError = MessageTypeError || error{
|
|||||||
|
|
||||||
const message = @This();
|
const message = @This();
|
||||||
|
|
||||||
pub const Message = union(PacketType) {
|
pub const Message = union(enum(u16)) {
|
||||||
relay: Message.Relay,
|
relay: Message.Relay = 0x003C,
|
||||||
connection: Message.Connection,
|
connection: Message.Connection = 0x00E9,
|
||||||
|
_,
|
||||||
|
|
||||||
pub const Relay = message.Relay;
|
pub const Relay = message.Relay;
|
||||||
pub const Connection = message.Connection;
|
pub const Connection = message.Connection;
|
||||||
|
|
||||||
pub fn toBytes(self: message.Message, buf: []u8) []u8 {
|
pub fn toBytes(self: message.Message, buf: []u8) []u8 {
|
||||||
return switch (self) {
|
return switch (self) {
|
||||||
inline else => |m| m.toBytes(buf),
|
inline .relay, .connection => |m| m.toBytes(buf),
|
||||||
|
else => unreachable,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,7 +30,7 @@ pub const relay_dest_len = 4;
|
|||||||
|
|
||||||
pub fn parse(bytes: []const u8) MessageParseError!Message {
|
pub fn parse(bytes: []const u8) MessageParseError!Message {
|
||||||
var in: Reader = .fixed(bytes);
|
var in: Reader = .fixed(bytes);
|
||||||
const @"type" = in.takeEnum(PacketType, .big) catch |err| switch (err) {
|
const @"type" = in.takeEnum(std.meta.Tag(Message), .big) catch |err| switch (err) {
|
||||||
error.InvalidEnumTag => return error.UnknownSaprusType,
|
error.InvalidEnumTag => return error.UnknownSaprusType,
|
||||||
else => return error.InvalidMessage,
|
else => return error.InvalidMessage,
|
||||||
};
|
};
|
||||||
@@ -124,7 +118,7 @@ const Relay = struct {
|
|||||||
/// Asserts that buf is large enough to fit the relay message.
|
/// Asserts that buf is large enough to fit the relay message.
|
||||||
pub fn toBytes(self: Relay, buf: []u8) []u8 {
|
pub fn toBytes(self: Relay, buf: []u8) []u8 {
|
||||||
var out: Writer = .fixed(buf);
|
var out: Writer = .fixed(buf);
|
||||||
out.writeInt(u16, @intFromEnum(PacketType.relay), .big) catch unreachable;
|
out.writeInt(u16, @intFromEnum(Message.relay), .big) catch unreachable;
|
||||||
out.writeInt(u16, @intCast(self.payload.len + 4), .big) catch unreachable; // Length field, but unread. Will switch to checksum
|
out.writeInt(u16, @intCast(self.payload.len + 4), .big) catch unreachable; // Length field, but unread. Will switch to checksum
|
||||||
out.writeAll(&self.dest.bytes) catch unreachable;
|
out.writeAll(&self.dest.bytes) catch unreachable;
|
||||||
out.writeAll(self.payload) catch unreachable;
|
out.writeAll(self.payload) catch unreachable;
|
||||||
@@ -178,7 +172,7 @@ const Connection = struct {
|
|||||||
/// Asserts that buf is large enough to fit the connection message.
|
/// Asserts that buf is large enough to fit the connection message.
|
||||||
pub fn toBytes(self: Connection, buf: []u8) []u8 {
|
pub fn toBytes(self: Connection, buf: []u8) []u8 {
|
||||||
var out: Writer = .fixed(buf);
|
var out: Writer = .fixed(buf);
|
||||||
out.writeInt(u16, @intFromEnum(PacketType.connection), .big) catch unreachable;
|
out.writeInt(u16, @intFromEnum(Message.connection), .big) catch unreachable;
|
||||||
out.writeInt(u16, @intCast(self.payload.len + 14), .big) catch unreachable; // Saprus length field, unread.
|
out.writeInt(u16, @intCast(self.payload.len + 14), .big) catch unreachable; // Saprus length field, unread.
|
||||||
out.writeInt(u16, self.src, .big) catch unreachable;
|
out.writeInt(u16, self.src, .big) catch unreachable;
|
||||||
out.writeInt(u16, self.dest, .big) catch unreachable;
|
out.writeInt(u16, self.dest, .big) catch unreachable;
|
||||||
|
|||||||
Reference in New Issue
Block a user