This repository was archived by the owner on Nov 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.zig
More file actions
262 lines (230 loc) · 8.57 KB
/
Copy pathexamples.zig
File metadata and controls
262 lines (230 loc) · 8.57 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
const std = @import("std");
const lizpack = @import("lizpack");
test "basic example" {
const CustomerComplaint = struct {
user_id: u64,
status: enum(u8) {
received,
reviewed,
awaiting_response,
finished,
},
};
var out: [1000]u8 = undefined;
var writer = std.Io.Writer.fixed(&out);
const expected: CustomerComplaint = .{ .user_id = 2345, .status = .reviewed };
try lizpack.encode(expected, &writer, .{});
var reader = std.Io.Reader.fixed(writer.buffered());
try std.testing.expectEqual(expected, lizpack.decode(@TypeOf(expected), &reader, .{}));
}
test "basic example 2" {
const TemperatureMeasurement = struct {
station_id: u64,
temperature_deg_c: f64,
latitude_deg: f64,
longitude_deg: f64,
altitude_m: f64,
};
var out: [1000]u8 = undefined;
var writer = std.Io.Writer.fixed(&out);
const expected: TemperatureMeasurement = .{
.station_id = 456,
.temperature_deg_c = 34.2,
.latitude_deg = 45.2,
.longitude_deg = 23.234562,
.altitude_m = 10034,
};
try lizpack.encode(expected, &writer, .{});
var reader = std.Io.Reader.fixed(writer.buffered());
try std.testing.expectEqual(expected, lizpack.decode(@TypeOf(expected), &reader, .{}));
}
test {
var out: [1]u8 = undefined;
var writer = std.Io.Writer.fixed(&out);
try lizpack.encode(false, &writer, .{});
try std.testing.expectEqualSlices(u8, &.{0xc2}, writer.buffered());
}
test "basic customize encoding" {
const CustomerComplaint = struct {
uuid: [16]u8,
message: []const u8,
const format: lizpack.FormatOptions(@This()) = .{
.layout = .map,
.fields = .{
.uuid = .bin,
.message = .str,
},
};
};
var out: [1000]u8 = undefined;
var writer = std.Io.Writer.fixed(&out);
const expected: CustomerComplaint = .{
.uuid = .{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
.message = "Your software is horrible!",
};
try lizpack.encode(expected, &writer, .{ .format = CustomerComplaint.format });
var reader = std.Io.Reader.fixed(writer.buffered());
const decoded = try lizpack.decodeAlloc(std.testing.allocator, CustomerComplaint, &reader, .{
.format = CustomerComplaint.format,
});
defer decoded.deinit();
try std.testing.expectEqualDeep(
expected,
decoded.value,
);
}
test "nested format customizations" {
const Location = struct {
city: []const u8,
state: []const u8,
pub const format: lizpack.FormatOptions(@This()) = .{
.layout = .map,
.fields = .{
.city = .str,
.state = .str,
},
};
};
const User = struct {
username: []const u8,
uuid: [16]u8,
location: Location,
pub const format: lizpack.FormatOptions(@This()) = .{
.layout = .map,
.fields = .{
.username = .str,
.uuid = .bin,
.location = Location.format,
},
};
};
const my_user: User = .{
.username = "foo",
.uuid = .{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
.location = .{
.city = "Los Angeles",
.state = "California",
},
};
var out: [1000]u8 = undefined;
var writer = std.Io.Writer.fixed(&out);
const expected = my_user;
try lizpack.encode(expected, &writer, .{ .format = User.format });
var reader = std.Io.Reader.fixed(writer.buffered());
const decoded = try lizpack.decodeAlloc(std.testing.allocator, @TypeOf(expected), &reader, .{ .format = User.format });
defer decoded.deinit();
try std.testing.expectEqualDeep(expected, decoded.value);
}
test "enum format customizations" {
var out: [1000]u8 = undefined;
var writer = std.Io.Writer.fixed(&out);
const MyEnum = enum {
foo,
bar,
pub const format_as_str: lizpack.FormatOptions(@This()) = .str;
pub const format_as_int: lizpack.FormatOptions(@This()) = .int;
};
try lizpack.encode(MyEnum.foo, &writer, .{ .format = MyEnum.format_as_str });
try std.testing.expectEqualSlices(u8, &.{ 0b10100011, 'f', 'o', 'o' }, writer.buffered());
writer = std.Io.Writer.fixed(&out);
try lizpack.encode(MyEnum.foo, &writer, .{ .format = MyEnum.format_as_int });
try std.testing.expectEqualSlices(u8, &.{0}, writer.buffered());
}
test "array and slice format customizations" {
var out: [1000]u8 = undefined;
var writer = std.Io.Writer.fixed(&out);
const my_string: []const u8 = "foo";
const format_as_bin: lizpack.FormatOptions(@TypeOf(my_string)) = .bin;
try lizpack.encode(my_string, &writer, .{ .format = format_as_bin });
try std.testing.expectEqualSlices(u8, &.{ (lizpack.spec.Format{ .bin_8 = {} }).encode(), 3, 'f', 'o', 'o' }, writer.buffered());
writer = std.Io.Writer.fixed(&out);
const format_as_string: lizpack.FormatOptions(@TypeOf(my_string)) = .str;
try lizpack.encode(my_string, &writer, .{ .format = format_as_string });
try std.testing.expectEqualSlices(u8, &.{ 0b10100011, 'f', 'o', 'o' }, writer.buffered());
writer = std.Io.Writer.fixed(&out);
const format_as_array: lizpack.FormatOptions(@TypeOf(my_string)) = .array;
try lizpack.encode(my_string, &writer, .{ .format = format_as_array });
try std.testing.expectEqualSlices(u8, &.{
(lizpack.spec.Format{ .fixarray = .{ .len = 3 } }).encode(),
(lizpack.spec.Format{ .uint_8 = {} }).encode(),
'f',
(lizpack.spec.Format{ .uint_8 = {} }).encode(),
'o',
(lizpack.spec.Format{ .uint_8 = {} }).encode(),
'o',
}, writer.buffered());
}
// test "union format customization" {
// const MyUnion = union(enum) {
// my_u8: u8,
// my_bool: bool,
// pub const format_as_map: lizpack.FormatOptions(@This()) = .{ .layout = .map };
// pub const format_as_active_field: lizpack.FormatOptions(@This()) = .{ .layout = .active_field };
// };
// const bytes_active_field: []const u8 = &.{(lizpack.spec.Format{ .false = {} }).encode()};
// try std.testing.expectEqual(MyUnion{ .my_bool = false }, try lizpack.decode(MyUnion, bytes_active_field, .{ .format = MyUnion.format_as_active_field }));
// const bytes_map: []const u8 = &.{
// (lizpack.spec.Format{ .fixmap = .{ .n_elements = 1 } }).encode(),
// (lizpack.spec.Format{ .fixstr = .{ .len = 5 } }).encode(),
// 'm',
// 'y',
// '_',
// 'u',
// '8',
// 0x03,
// };
// try std.testing.expectEqual(MyUnion{ .my_u8 = 3 }, try lizpack.decode(MyUnion, bytes_map, .{ .format = MyUnion.format_as_map }));
// }
test "maps" {
const RoleItem = struct {
username: []const u8, // key
role: enum { admin, plebeian }, // value
};
const roles: []const RoleItem = &.{
.{ .username = "sarah", .role = .admin },
.{ .username = "bob", .role = .plebeian },
};
const format: lizpack.FormatOptions(@TypeOf(roles)) = .{ .layout = .map_item_first_field_is_key };
const expected_bytes: []const u8 = &.{
(lizpack.spec.Format{ .fixmap = .{ .n_elements = 2 } }).encode(),
(lizpack.spec.Format{ .fixstr = .{ .len = 5 } }).encode(),
's',
'a',
'r',
'a',
'h',
0,
(lizpack.spec.Format{ .fixstr = .{ .len = 3 } }).encode(),
'b',
'o',
'b',
1,
};
var out: [1000]u8 = undefined;
var writer = std.Io.Writer.fixed(&out);
try lizpack.encode(roles, &writer, .{ .format = format });
try std.testing.expectEqualSlices(u8, expected_bytes, writer.buffered());
}
test "manual encoding" {
const expected: []const u8 = &.{
(lizpack.spec.Format{ .fixstr = .{ .len = 3 } }).encode(),
'f',
'o',
'o',
};
const actual: lizpack.manual.Value = .{ .fixstr = "foo" };
var buf: [1000]u8 = undefined;
var writer = std.Io.Writer.fixed(&buf);
try lizpack.manual.encode(actual, &writer);
try std.testing.expectEqualSlices(u8, expected, writer.buffered());
}
test "manual decoding" {
const raw: []const u8 = &.{@bitCast(@as(i8, -15))};
var reader = std.Io.Reader.fixed(raw);
const decoded = try lizpack.manual.decode(std.testing.allocator, &reader);
defer decoded.deinit();
switch (decoded.value) {
.negative_fixint => |payload| try std.testing.expectEqual(-15, payload),
else => return error.Invalid,
}
}