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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
|
const std = @import("std");
pub const Reader = struct {
const Self = @This();
pub const ReadError = error{EOF};
buf: []u8,
remain: []u8,
allocator: *std.mem.Allocator,
pub fn init(buf: []u8, allocator: *std.mem.Allocator) Reader {
return Reader{
.buf = buf,
.remain = buf,
.allocator = allocator,
};
}
pub fn read(this: *Self, count: usize) ![]u8 {
const read_count = std.math.min(count, this.remain.len);
if (read_count < count) {
return error.EOF;
}
var slice = this.remain[0..read_count];
this.remain = this.remain[read_count..];
return slice;
}
pub fn readAs(this: *Self, comptime T: type) !T {
if (!std.meta.trait.hasUniqueRepresentation(T)) {
@compileError(@typeName(T) ++ " must have unique representation.");
}
return std.mem.bytesAsValue(T, try this.read(@sizeOf(T)));
}
pub fn readByte(this: *Self) !u8 {
return (try this.read(1))[0];
}
pub fn readEnum(this: *Self, comptime Enum: type) !Enum {
const E = error{
/// An integer was read, but it did not match any of the tags in the supplied enum.
InvalidValue,
};
const type_info = @typeInfo(Enum).Enum;
const tag = try this.readInt(type_info.tag_type);
inline for (std.meta.fields(Enum)) |field| {
if (tag == field.value) {
return @field(Enum, field.name);
}
}
return E.InvalidValue;
}
pub fn readArray(this: *Self, comptime T: type) ![]const T {
const length = try this.readInt(u32);
if (length == 0) {
return &([_]T{});
}
switch (T) {
u8 => {
return try this.read(length);
},
u16, u32, i8, i16, i32 => {
return std.mem.readIntSliceNative(T, this.read(length * @sizeOf(T)));
},
[]const u8 => {
var i: u32 = 0;
var array = try this.allocator.alloc([]const u8, length);
while (i < length) : (i += 1) {
array[i] = try this.readArray(u8);
}
return array;
},
else => {
switch (@typeInfo(T)) {
.Struct => |Struct| {
switch (Struct.layout) {
.Packed => {
const sizeof = @sizeOf(T);
var slice = try this.read(sizeof * length);
return std.mem.bytesAsSlice(T, slice);
},
else => {},
}
},
.Enum => |type_info| {
const enum_values = try this.read(length * @sizeOf(type_info.tag_type));
return @ptrCast([*]T, enum_values.ptr)[0..length];
},
else => {},
}
var i: u32 = 0;
var array = try this.allocator.alloc(T, length);
while (i < length) : (i += 1) {
array[i] = try this.readValue(T);
}
return array;
},
}
}
pub fn readByteArray(this: *Self) ![]u8 {
const length = try this.readInt(u32);
if (length == 0) {
return &([_]u8{});
}
return try this.read(@intCast(usize, length));
}
pub fn readInt(this: *Self, comptime T: type) !T {
var slice = try this.read(@sizeOf(T));
return std.mem.readIntSliceNative(T, slice);
}
pub fn readBool(this: *Self) !bool {
return (try this.readByte()) > 0;
}
pub fn readValue(this: *Self, comptime T: type) !T {
switch (T) {
bool => {
return try this.readBool();
},
u8 => {
return try this.readByte();
},
[]const u8 => {
return try this.readArray(u8);
},
[]const []const u8 => {
return try this.readArray([]const u8);
},
[]u8 => {
return try this.readArray([]u8);
},
u16, u32, i8, i16, i32 => {
return std.mem.readIntSliceNative(T, try this.read(@sizeOf(T)));
},
else => {
switch (@typeInfo(T)) {
.Struct => |Struct| {
switch (Struct.layout) {
.Packed => {
const sizeof = @sizeOf(T);
var slice = try this.read(sizeof);
return @ptrCast(*T, slice[0..sizeof]).*;
},
else => {},
}
},
.Enum => |type_info| {
return try this.readEnum(T);
},
else => {},
}
return try T.decode(this);
},
}
@compileError("Invalid type passed to readValue");
}
};
pub fn Writer(comptime WritableStream: type) type {
return struct {
const Self = @This();
writable: WritableStream,
pub fn init(writable: WritableStream) Self {
return Self{ .writable = writable };
}
pub fn write(this: *Self, bytes: anytype) !void {
_ = try this.writable.write(bytes);
}
pub fn writeByte(this: *Self, byte: u8) !void {
_ = try this.writable.write(&[1]u8{byte});
}
pub fn writeInt(this: *Self, int: anytype) !void {
try this.write(std.mem.asBytes(&int));
}
pub fn writeFieldID(this: *Self, comptime id: comptime_int) !void {
try this.writeByte(id);
}
pub fn writeEnum(this: *Self, val: anytype) !void {
try this.writeInt(@enumToInt(val));
}
pub fn writeValue(this: *Self, slice: anytype) !void {
switch (@TypeOf(slice)) {
[]u16,
[]u32,
[]i16,
[]i32,
[]i8,
[]const u16,
[]const u32,
[]const i16,
[]const i32,
[]const i8,
=> {
try this.writeArray(@TypeOf(slice), slice);
},
[]u8, []const u8 => {
try this.writeArray(u8, slice);
},
u8 => {
try this.write(slice);
},
u16, u32, i16, i32, i8 => {
try this.write(std.mem.asBytes(slice));
},
else => {
try slice.encode(this);
},
}
}
pub fn writeArray(this: *Self, comptime T: type, slice: anytype) !void {
try this.writeInt(@truncate(u32, slice.len));
switch (T) {
u8 => {
try this.write(slice);
},
u16, u32, i16, i32, i8 => {
try this.write(std.mem.asBytes(slice));
},
[]u8,
[]u16,
[]u32,
[]i16,
[]i32,
[]i8,
[]const u8,
[]const u16,
[]const u32,
[]const i16,
[]const i32,
[]const i8,
=> {
for (slice) |num_slice| {
try this.writeArray(std.meta.Child(@TypeOf(num_slice)), num_slice);
}
},
else => {
for (slice) |val| {
try val.encode(this);
}
},
}
}
pub fn endMessage(this: *Self) !void {
try this.writeByte(0);
}
};
}
pub const ByteWriter = Writer(*std.io.FixedBufferStream([]u8));
pub const FileWriter = Writer(std.fs.File);
pub const analytics = struct {
pub const OperatingSystem = enum(u8) {
_none,
/// linux
linux,
/// macos
macos,
/// windows
windows,
/// wsl
wsl,
_,
pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void {
return try std.json.stringify(@tagName(self), opts, o);
}
};
pub const Architecture = enum(u8) {
_none,
/// x64
x64,
/// arm
arm,
_,
pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void {
return try std.json.stringify(@tagName(self), opts, o);
}
};
pub const Platform = struct {
/// os
os: OperatingSystem,
/// arch
arch: Architecture,
/// version
version: []const u8,
pub fn decode(reader: anytype) anyerror!Platform {
var this = std.mem.zeroes(Platform);
this.os = try reader.readValue(OperatingSystem);
this.arch = try reader.readValue(Architecture);
this.version = try reader.readArray(u8);
return this;
}
pub fn encode(this: *const @This(), writer: anytype) anyerror!void {
try writer.writeEnum(this.os);
try writer.writeEnum(this.arch);
try writer.writeArray(u8, this.version);
}
};
pub const EventKind = enum(u32) {
_none,
/// bundle_success
bundle_success,
/// bundle_fail
bundle_fail,
/// http_start
http_start,
/// http_build
http_build,
/// bundle_start
bundle_start,
_,
pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void {
return try std.json.stringify(@tagName(self), opts, o);
}
};
pub const Uint64 = packed struct {
/// first
first: u32 = 0,
/// second
second: u32 = 0,
pub fn decode(reader: anytype) anyerror!Uint64 {
var this = std.mem.zeroes(Uint64);
this.first = try reader.readValue(u32);
this.second = try reader.readValue(u32);
return this;
}
pub fn encode(this: *const @This(), writer: anytype) anyerror!void {
try writer.writeInt(this.first);
try writer.writeInt(this.second);
}
};
pub const EventListHeader = struct {
/// machine_id
machine_id: Uint64,
/// session_id
session_id: u32 = 0,
/// platform
platform: Platform,
/// build_id
build_id: u32 = 0,
/// project_id
project_id: Uint64,
/// session_length
session_length: u32 = 0,
/// feature_usage
feature_usage: u32 = 0,
pub fn decode(reader: anytype) anyerror!EventListHeader {
var this = std.mem.zeroes(EventListHeader);
this.machine_id = try reader.readValue(Uint64);
this.session_id = try reader.readValue(u32);
this.platform = try reader.readValue(Platform);
this.build_id = try reader.readValue(u32);
this.project_id = try reader.readValue(Uint64);
this.session_length = try reader.readValue(u32);
this.feature_usage = try reader.readValue(u32);
return this;
}
pub fn encode(this: *const @This(), writer: anytype) anyerror!void {
try writer.writeValue(this.machine_id);
try writer.writeInt(this.session_id);
try writer.writeValue(this.platform);
try writer.writeInt(this.build_id);
try writer.writeValue(this.project_id);
try writer.writeInt(this.session_length);
try writer.writeInt(this.feature_usage);
}
};
pub const EventHeader = struct {
/// timestamp
timestamp: Uint64,
/// kind
kind: EventKind,
pub fn decode(reader: anytype) anyerror!EventHeader {
var this = std.mem.zeroes(EventHeader);
this.timestamp = try reader.readValue(Uint64);
this.kind = try reader.readValue(EventKind);
return this;
}
pub fn encode(this: *const @This(), writer: anytype) anyerror!void {
try writer.writeValue(this.timestamp);
try writer.writeEnum(this.kind);
}
};
pub const EventList = struct {
/// header
header: EventListHeader,
/// event_count
event_count: u32 = 0,
pub fn decode(reader: anytype) anyerror!EventList {
var this = std.mem.zeroes(EventList);
this.header = try reader.readValue(EventListHeader);
this.event_count = try reader.readValue(u32);
return this;
}
pub fn encode(this: *const @This(), writer: anytype) anyerror!void {
try writer.writeValue(this.header);
try writer.writeInt(this.event_count);
}
};
};
const ExamplePackedStruct = packed struct {
len: u32 = 0,
offset: u32 = 0,
pub fn encode(this: *const ExamplePackedStruct, writer: anytype) !void {
try writer.write(std.mem.asBytes(this));
}
pub fn decode(reader: anytype) !ExamplePackedStruct {
return try reader.readAs(ExamplePackedStruct);
}
};
const ExampleStruct = struct {
name: []const u8 = "",
age: u32 = 0,
pub fn encode(this: *const ExampleStruct, writer: anytype) !void {
try writer.writeArray(u8, this.name);
try writer.writeInt(this.age);
}
pub fn decode(reader: anytype) !ExampleStruct {
var this = std.mem.zeroes(ExampleStruct);
this.name = try reader.readArray(u8);
this.age = try reader.readInt(u32);
return this;
}
};
const EnumValue = enum(u8) { hey, hi, heyopoo };
const ExampleMessage = struct {
examples: ?[]ExampleStruct = &([_]ExampleStruct{}),
pack: ?[]ExamplePackedStruct = &([_]ExamplePackedStruct{}),
hey: ?u8 = 0,
hey16: ?u16 = 0,
hey32: ?u16 = 0,
heyi32: ?i32 = 0,
heyi16: ?i16 = 0,
heyi8: ?i8 = 0,
boolean: ?bool = null,
heyooo: ?EnumValue = null,
pub fn encode(this: *const ExampleMessage, writer: anytype) !void {
if (this.examples) |examples| {
try writer.writeFieldID(1);
try writer.writeArray(ExampleStruct, examples);
}
if (this.pack) |pack| {
try writer.writeFieldID(2);
try writer.writeArray(ExamplePackedStruct, pack);
}
if (this.hey) |hey| {
try writer.writeFieldID(3);
try writer.writeInt(hey);
}
if (this.hey16) |hey16| {
try writer.writeFieldID(4);
try writer.writeInt(hey16);
}
if (this.hey32) |hey32| {
try writer.writeFieldID(5);
try writer.writeInt(hey32);
}
if (this.heyi32) |heyi32| {
try writer.writeFieldID(6);
try writer.writeInt(heyi32);
}
if (this.heyi16) |heyi16| {
try writer.writeFieldID(7);
try writer.writeInt(heyi16);
}
if (this.heyi8) |heyi8| {
try writer.writeFieldID(8);
try writer.writeInt(heyi8);
}
if (this.boolean) |boolean| {
try writer.writeFieldID(9);
try writer.writeInt(boolean);
}
if (this.heyooo) |heyoo| {
try writer.writeFieldID(10);
try writer.writeEnum(heyoo);
}
try writer.endMessage();
}
pub fn decode(reader: anytype) !ExampleMessage {
var this = std.mem.zeroes(ExampleMessage);
while (true) {
switch (try reader.readByte()) {
0 => {
return this;
},
1 => {
this.examples = try reader.readArray(std.meta.Child(@TypeOf(this.examples.?)));
},
2 => {
this.pack = try reader.readArray(std.meta.Child(@TypeOf(this.pack.?)));
},
3 => {
this.hey = try reader.readValue(@TypeOf(this.hey.?));
},
4 => {
this.hey16 = try reader.readValue(@TypeOf(this.hey16.?));
},
5 => {
this.hey32 = try reader.readValue(@TypeOf(this.hey32.?));
},
6 => {
this.heyi32 = try reader.readValue(@TypeOf(this.heyi32.?));
},
7 => {
this.heyi16 = try reader.readValue(@TypeOf(this.heyi16.?));
},
8 => {
this.heyi8 = try reader.readValue(@TypeOf(this.heyi8.?));
},
9 => {
this.boolean = try reader.readValue(@TypeOf(this.boolean.?));
},
10 => {
this.heyooo = try reader.readValue(@TypeOf(this.heyooo.?));
},
else => {
return error.InvalidValue;
},
}
}
return this;
}
};
test "ExampleMessage" {
var base = std.mem.zeroes(ExampleMessage);
base.hey = 1;
var buf: [4096]u8 = undefined;
var writable = std.io.fixedBufferStream(&buf);
var writer = ByteWriter.init(writable);
var examples = [_]ExamplePackedStruct{
.{ .len = 2, .offset = 5 },
.{ .len = 0, .offset = 10 },
};
var more_examples = [_]ExampleStruct{
.{ .name = "bacon", .age = 10 },
.{ .name = "slime", .age = 300 },
};
base.examples = &more_examples;
base.pack = &examples;
base.heyooo = EnumValue.hey;
try base.encode(&writer);
var reader = Reader.init(&buf, std.heap.c_allocator);
var compare = try ExampleMessage.decode(&reader);
try std.testing.expectEqual(base.hey orelse 255, 1);
const cmp_pack = compare.pack.?;
for (cmp_pack) |item, id| {
try std.testing.expectEqual(item, examples[id]);
}
const cmp_ex = compare.examples.?;
for (cmp_ex) |item, id| {
try std.testing.expectEqualStrings(item.name, more_examples[id].name);
try std.testing.expectEqual(item.age, more_examples[id].age);
}
try std.testing.expectEqual(cmp_pack[0].len, examples[0].len);
try std.testing.expectEqual(base.heyooo, compare.heyooo);
}
|