-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathany.zig
More file actions
380 lines (330 loc) · 12.2 KB
/
Copy pathany.zig
File metadata and controls
380 lines (330 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
const std = @import("std");
const hdrs = @import("headers.zig");
const NonOptional = @import("utils.zig").NonOptional;
const packNull = @import("null.zig").packNull;
const unpackNull = @import("null.zig").unpackNull;
const isNullError = @import("null.zig").isNullError;
const getBoolSize = @import("bool.zig").getBoolSize;
const packBool = @import("bool.zig").packBool;
const unpackBool = @import("bool.zig").unpackBool;
const getIntSize = @import("int.zig").getIntSize;
const packInt = @import("int.zig").packInt;
const unpackInt = @import("int.zig").unpackInt;
const getFloatSize = @import("float.zig").getFloatSize;
const packFloat = @import("float.zig").packFloat;
const unpackFloat = @import("float.zig").unpackFloat;
const sizeOfPackedString = @import("string.zig").sizeOfPackedString;
const packString = @import("string.zig").packString;
const unpackString = @import("string.zig").unpackString;
const String = @import("string.zig").String;
const sizeOfPackedArray = @import("array.zig").sizeOfPackedArray;
const packArray = @import("array.zig").packArray;
const unpackArray = @import("array.zig").unpackArray;
const packStruct = @import("struct.zig").packStruct;
const unpackStruct = @import("struct.zig").unpackStruct;
const packUnion = @import("union.zig").packUnion;
const unpackUnion = @import("union.zig").unpackUnion;
const getEnumSize = @import("enum.zig").getEnumSize;
const packEnum = @import("enum.zig").packEnum;
const unpackEnum = @import("enum.zig").unpackEnum;
inline fn isString(comptime T: type) bool {
switch (@typeInfo(T)) {
.pointer => |ptr_info| {
if (ptr_info.size == .slice) {
if (ptr_info.child == u8) {
return true;
}
}
},
.optional => |opt_info| {
return isString(opt_info.child);
},
else => {},
}
return false;
}
pub fn sizeOfPackedAny(comptime T: type, value: T) usize {
switch (@typeInfo(NonOptional(T))) {
.bool => return getBoolSize(),
.int => return getIntSize(T, value),
.float => return getFloatSize(T, value),
.@"enum" => return getEnumSize(T, value),
.pointer => |ptr_info| {
if (ptr_info.size == .Slice) {
if (isString(T)) {
return sizeOfPackedString(value.len);
} else {
return sizeOfPackedArray(value.len);
}
}
},
else => {},
}
@compileError("Unsupported type '" ++ @typeName(T) ++ "'");
}
pub fn packAny(writer: anytype, value: anytype) !void {
const T = @TypeOf(value);
switch (@typeInfo(T)) {
.void => return packNull(writer),
.bool => return packBool(writer, value),
.int => return packInt(writer, T, value),
.float => return packFloat(writer, T, value),
.comptime_int => return packInt(writer, i64, @intCast(value)),
.comptime_float => return packFloat(writer, f64, @floatCast(value)),
.array => |arr_info| {
switch (arr_info.child) {
u8 => {
return packString(writer, &value);
},
else => {
return packArray(writer, []const arr_info.child, &value);
},
}
},
.pointer => |ptr_info| {
if (ptr_info.size == .slice) {
switch (ptr_info.child) {
u8 => {
return packString(writer, value);
},
else => {
return packArray(writer, T, value);
},
}
} else if (ptr_info.size == .one) {
return packAny(writer, value.*);
}
},
.@"struct" => return packStruct(writer, T, value),
.@"union" => return packUnion(writer, T, value),
.@"enum" => return packEnum(writer, T, value),
.optional => {
if (value) |val| {
return packAny(writer, val);
} else {
return packNull(writer);
}
},
else => {},
}
@compileError("Unsupported type '" ++ @typeName(T) ++ "'");
}
pub fn unpackAny(reader: anytype, allocator: std.mem.Allocator, comptime T: type) !T {
switch (@typeInfo(T)) {
.void => return unpackNull(reader),
.bool => return unpackBool(reader, T),
.int => return unpackInt(reader, T),
.float => return unpackFloat(reader, T),
.@"struct" => return unpackStruct(reader, allocator, T),
.@"union" => return unpackUnion(reader, allocator, T),
.@"enum" => return unpackEnum(reader, T),
.pointer => |ptr_info| {
if (ptr_info.size == .slice) {
if (isString(T)) {
return unpackString(reader, allocator);
} else {
return unpackArray(reader, allocator, T);
}
}
},
.optional => |opt_info| {
return unpackAny(reader, allocator, opt_info.child) catch |err| {
if (isNullError(err)) {
return null;
}
return err;
};
},
else => {},
}
@compileError("Unsupported type '" ++ @typeName(T) ++ "'");
}
test "packAny/unpackAny: bool" {
var buffer: [16]u8 = undefined;
var stream = std.io.fixedBufferStream(&buffer);
try packAny(stream.writer(), true);
stream.reset();
try std.testing.expectEqual(true, try unpackAny(stream.reader(), std.testing.allocator, bool));
}
test "packAny/unpackAny: optional bool" {
const values = [_]?bool{ true, null };
for (values) |value| {
var buffer: [16]u8 = undefined;
var stream = std.io.fixedBufferStream(&buffer);
try packAny(stream.writer(), value);
stream.reset();
try std.testing.expectEqual(value, try unpackAny(stream.reader(), std.testing.allocator, ?bool));
}
}
test "packAny/unpackAny: int" {
var buffer: [16]u8 = undefined;
var stream = std.io.fixedBufferStream(&buffer);
try packAny(stream.writer(), -42);
stream.reset();
try std.testing.expectEqual(-42, try unpackAny(stream.reader(), std.testing.allocator, i32));
}
test "packAny/unpackAny: optional int" {
const values = [_]?i32{ -42, null };
for (values) |value| {
var buffer: [16]u8 = undefined;
var stream = std.io.fixedBufferStream(&buffer);
try packAny(stream.writer(), value);
stream.reset();
try std.testing.expectEqual(value, try unpackAny(stream.reader(), std.testing.allocator, ?i32));
}
}
test "packAny/unpackAny: float" {
var buffer: [16]u8 = undefined;
var stream = std.io.fixedBufferStream(&buffer);
try packAny(stream.writer(), 3.14);
stream.reset();
try std.testing.expectEqual(3.14, try unpackAny(stream.reader(), std.testing.allocator, f32));
}
test "packAny/unpackAny: optional float" {
const values = [_]?f32{ 3.14, null };
for (values) |value| {
var buffer: [16]u8 = undefined;
var stream = std.io.fixedBufferStream(&buffer);
try packAny(stream.writer(), value);
stream.reset();
try std.testing.expectEqual(value, try unpackAny(stream.reader(), std.testing.allocator, ?f32));
}
}
test "packAny/unpackAny: string" {
var buffer: [32]u8 = undefined;
var stream = std.io.fixedBufferStream(&buffer);
try packAny(stream.writer(), "hello");
stream.reset();
const result = try unpackAny(stream.reader(), std.testing.allocator, []const u8);
defer std.testing.allocator.free(result);
try std.testing.expectEqualStrings("hello", result);
}
test "packAny/unpackAny: optional string" {
const values = [_]?[]const u8{ "hello", null };
for (values) |value| {
var buffer: [32]u8 = undefined;
var stream = std.io.fixedBufferStream(&buffer);
try packAny(stream.writer(), value);
stream.reset();
const result = try unpackAny(stream.reader(), std.testing.allocator, ?[]const u8);
defer if (result) |str| std.testing.allocator.free(str);
if (value) |str| {
try std.testing.expectEqualStrings(str, result.?);
} else {
try std.testing.expectEqual(value, result);
}
}
}
test "packAny/unpackAny: array" {
var buffer: [64]u8 = undefined;
var stream = std.io.fixedBufferStream(&buffer);
const array = [_]i32{ 1, 2, 3, 4, 5 };
try packAny(stream.writer(), &array);
stream.reset();
const result = try unpackAny(stream.reader(), std.testing.allocator, []const i32);
defer std.testing.allocator.free(result);
try std.testing.expectEqualSlices(i32, &array, result);
}
test "packAny/unpackAny: optional array" {
const array = [_]i32{ 1, 2, 3, 4, 5 };
const values = [_]?[]const i32{ &array, null };
for (values) |value| {
var buffer: [64]u8 = undefined;
var stream = std.io.fixedBufferStream(&buffer);
try packAny(stream.writer(), value);
stream.reset();
const result = try unpackAny(stream.reader(), std.testing.allocator, ?[]const i32);
defer if (result) |arr| std.testing.allocator.free(arr);
if (value) |arr| {
try std.testing.expectEqualSlices(i32, arr, result.?);
} else {
try std.testing.expectEqual(value, result);
}
}
}
test "packAny/unpackAny: struct" {
const Point = struct {
x: i32,
y: i32,
};
var buffer: [64]u8 = undefined;
var stream = std.io.fixedBufferStream(&buffer);
const point = Point{ .x = 10, .y = 20 };
try packAny(stream.writer(), point);
stream.reset();
const result = try unpackAny(stream.reader(), std.testing.allocator, Point);
try std.testing.expectEqualDeep(point, result);
}
test "packAny/unpackAny: optional struct" {
const Point = struct {
x: i32,
y: i32,
};
const point = Point{ .x = 10, .y = 20 };
const values = [_]?Point{ point, null };
for (values) |value| {
var buffer: [64]u8 = undefined;
var stream = std.io.fixedBufferStream(&buffer);
try packAny(stream.writer(), value);
stream.reset();
const result = try unpackAny(stream.reader(), std.testing.allocator, ?Point);
try std.testing.expectEqualDeep(value, result);
}
}
test "packAny/unpackAny: union" {
const Value = union(enum) {
int: i32,
float: f32,
};
const values = [_]Value{
Value{ .int = 42 },
Value{ .float = 3.14 },
};
for (values) |value| {
var buffer: [64]u8 = undefined;
var stream = std.io.fixedBufferStream(&buffer);
try packAny(stream.writer(), value);
stream.reset();
const result = try unpackAny(stream.reader(), std.testing.allocator, Value);
try std.testing.expectEqualDeep(value, result);
}
}
test "packAny/unpackAny: optional union" {
const Value = union(enum) {
int: i32,
float: f32,
};
const values = [_]?Value{
Value{ .int = 42 },
Value{ .float = 3.14 },
null,
};
for (values) |value| {
var buffer: [64]u8 = undefined;
var stream = std.io.fixedBufferStream(&buffer);
try packAny(stream.writer(), value);
stream.reset();
const result = try unpackAny(stream.reader(), std.testing.allocator, ?Value);
try std.testing.expectEqualDeep(value, result);
}
}
test "packAny/unpackAny: String struct" {
var buffer: [64]u8 = undefined;
var stream = std.io.fixedBufferStream(&buffer);
const str = String{ .data = "hello" };
try packAny(stream.writer(), str);
stream.reset();
const result = try unpackAny(stream.reader(), std.testing.allocator, String);
defer std.testing.allocator.free(result.data);
try std.testing.expectEqualStrings("hello", result.data);
}
test "packAny/unpackAny: Binary struct" {
var buffer: [64]u8 = undefined;
var stream = std.io.fixedBufferStream(&buffer);
const str = String{ .data = "\x01\x02\x03\x04" };
try packAny(stream.writer(), str);
stream.reset();
const result = try unpackAny(stream.reader(), std.testing.allocator, String);
defer std.testing.allocator.free(result.data);
try std.testing.expectEqualStrings("\x01\x02\x03\x04", result.data);
}