-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.zig
205 lines (178 loc) · 6.64 KB
/
examples.zig
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
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;
const expected: CustomerComplaint = .{ .user_id = 2345, .status = .reviewed };
const slice: []u8 = try lizpack.encode(expected, &out, .{});
try std.testing.expectEqual(expected, lizpack.decode(@TypeOf(expected), slice, .{}));
}
test "basic example bounded" {
const CustomerComplaint = struct {
user_id: u64,
status: enum(u8) {
received,
reviewed,
awaiting_response,
finished,
},
};
// look mom! no errors!
const expected: CustomerComplaint = .{ .user_id = 2345, .status = .reviewed };
const slice: []const u8 = lizpack.encodeBounded(expected, .{}).slice();
try std.testing.expectEqual(expected, lizpack.decode(@TypeOf(expected), slice, .{}));
}
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;
const expected: TemperatureMeasurement = .{
.station_id = 456,
.temperature_deg_c = 34.2,
.latitude_deg = 45.2,
.longitude_deg = 23.234562,
.altitude_m = 10034,
};
const slice: []u8 = try lizpack.encode(expected, &out, .{});
try std.testing.expectEqual(expected, lizpack.decode(@TypeOf(expected), slice, .{}));
}
test {
var out: [1]u8 = undefined;
const slice: []u8 = try lizpack.encode(false, &out, .{});
try std.testing.expectEqualSlices(u8, &.{0xc2}, slice);
}
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;
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!",
};
const slice: []u8 = try lizpack.encode(expected, &out, .{ .format = CustomerComplaint.format });
const decoded = try lizpack.decodeAlloc(std.testing.allocator, CustomerComplaint, slice, .{
.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;
const expected = my_user;
const slice = try lizpack.encode(expected, &out, .{ .format = User.format });
const decoded = try lizpack.decodeAlloc(std.testing.allocator, @TypeOf(expected), slice, .{ .format = User.format });
defer decoded.deinit();
try std.testing.expectEqualDeep(expected, decoded.value);
}
test "enum format customizations" {
var out: [1000]u8 = undefined;
const MyEnum = enum {
foo,
bar,
pub const format_as_str: lizpack.FormatOptions(@This()) = .str;
pub const format_as_int: lizpack.FormatOptions(@This()) = .int;
};
const slice = try lizpack.encode(MyEnum.foo, &out, .{ .format = MyEnum.format_as_str });
try std.testing.expectEqualSlices(u8, &.{ 0b10100011, 'f', 'o', 'o' }, slice);
const slice2 = try lizpack.encode(MyEnum.foo, &out, .{ .format = MyEnum.format_as_int });
try std.testing.expectEqualSlices(u8, &.{0}, slice2);
}
test "array and slice format customizations" {
var out: [1000]u8 = undefined;
const my_string: []const u8 = "foo";
const format_as_bin: lizpack.FormatOptions(@TypeOf(my_string)) = .bin;
const slice = try lizpack.encode(my_string, &out, .{ .format = format_as_bin });
try std.testing.expectEqualSlices(u8, &.{ (lizpack.Spec.Format{ .bin_8 = {} }).encode(), 3, 'f', 'o', 'o' }, slice);
const format_as_string: lizpack.FormatOptions(@TypeOf(my_string)) = .str;
const slice2 = try lizpack.encode(my_string, &out, .{ .format = format_as_string });
try std.testing.expectEqualSlices(u8, &.{ 0b10100011, 'f', 'o', 'o' }, slice2);
const format_as_array: lizpack.FormatOptions(@TypeOf(my_string)) = .array;
const slice3 = try lizpack.encode(my_string, &out, .{ .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',
}, slice3);
}
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 }));
}