| ... | ... | @@ -438,17 +438,17 @@ pub const SerializeContainerOptions = struct { |
| 438 | 438 | /// * `multilineString` |
| 439 | 439 | /// |
| 440 | 440 | /// For manual serialization of containers, see: |
| 441 | | /// * `startStruct` |
| 442 | | /// * `startTuple` |
| 441 | /// * `beginStruct` |
| 442 | /// * `beginTuple` |
| 443 | 443 | /// |
| 444 | 444 | /// # Example |
| 445 | 445 | /// ```zig |
| 446 | 446 | /// var sz = serializer(writer, .{}); |
| 447 | | /// var vec2 = try sz.startStruct(.{}); |
| 447 | /// var vec2 = try sz.beginStruct(.{}); |
| 448 | 448 | /// try vec2.field("x", 1.5, .{}); |
| 449 | 449 | /// try vec2.fieldPrefix(); |
| 450 | 450 | /// try sz.value(2.5); |
| 451 | | /// try vec2.finish(); |
| 451 | /// try vec2.end(); |
| 452 | 452 | /// ``` |
| 453 | 453 | pub fn Serializer(Writer: type) type { |
| 454 | 454 | return struct { |
| ... | ... | @@ -525,22 +525,22 @@ pub fn Serializer(Writer: type) type { |
| 525 | 525 | } |
| 526 | 526 | }, |
| 527 | 527 | .array => { |
| 528 | | var container = try self.startTuple( |
| 528 | var container = try self.beginTuple( |
| 529 | 529 | .{ .whitespace_style = .{ .fields = val.len } }, |
| 530 | 530 | ); |
| 531 | 531 | for (val) |item_val| { |
| 532 | 532 | try container.fieldArbitraryDepth(item_val, options); |
| 533 | 533 | } |
| 534 | | try container.finish(); |
| 534 | try container.end(); |
| 535 | 535 | }, |
| 536 | 536 | .@"struct" => |@"struct"| if (@"struct".is_tuple) { |
| 537 | | var container = try self.startTuple( |
| 537 | var container = try self.beginTuple( |
| 538 | 538 | .{ .whitespace_style = .{ .fields = @"struct".fields.len } }, |
| 539 | 539 | ); |
| 540 | 540 | inline for (val) |field_value| { |
| 541 | 541 | try container.fieldArbitraryDepth(field_value, options); |
| 542 | 542 | } |
| 543 | | try container.finish(); |
| 543 | try container.end(); |
| 544 | 544 | } else { |
| 545 | 545 | // Decide which fields to emit |
| 546 | 546 | const fields, const skipped: [@"struct".fields.len]bool = if (options.emit_default_optional_fields) b: { |
| ... | ... | @@ -562,7 +562,7 @@ pub fn Serializer(Writer: type) type { |
| 562 | 562 | }; |
| 563 | 563 | |
| 564 | 564 | // Emit those fields |
| 565 | | var container = try self.startStruct( |
| 565 | var container = try self.beginStruct( |
| 566 | 566 | .{ .whitespace_style = .{ .fields = fields } }, |
| 567 | 567 | ); |
| 568 | 568 | inline for (@"struct".fields, skipped) |field_info, skip| { |
| ... | ... | @@ -574,7 +574,7 @@ pub fn Serializer(Writer: type) type { |
| 574 | 574 | ); |
| 575 | 575 | } |
| 576 | 576 | } |
| 577 | | try container.finish(); |
| 577 | try container.end(); |
| 578 | 578 | }, |
| 579 | 579 | .@"union" => |@"union"| { |
| 580 | 580 | comptime assert(@"union".tag_type != null); |
| ... | ... | @@ -582,7 +582,7 @@ pub fn Serializer(Writer: type) type { |
| 582 | 582 | inline else => |pl, tag| if (@TypeOf(pl) == void) |
| 583 | 583 | try self.writer.print(".{s}", .{@tagName(tag)}) |
| 584 | 584 | else { |
| 585 | | var container = try self.startStruct(.{ .whitespace_style = .{ .fields = 1 } }); |
| 585 | var container = try self.beginStruct(.{ .whitespace_style = .{ .fields = 1 } }); |
| 586 | 586 | |
| 587 | 587 | try container.fieldArbitraryDepth( |
| 588 | 588 | @tagName(tag), |
| ... | ... | @@ -590,7 +590,7 @@ pub fn Serializer(Writer: type) type { |
| 590 | 590 | options, |
| 591 | 591 | ); |
| 592 | 592 | |
| 593 | | try container.finish(); |
| 593 | try container.end(); |
| 594 | 594 | }, |
| 595 | 595 | } |
| 596 | 596 | }, |
| ... | ... | @@ -600,13 +600,13 @@ pub fn Serializer(Writer: type) type { |
| 600 | 600 | try self.writer.writeAll("null"); |
| 601 | 601 | }, |
| 602 | 602 | .vector => |vector| { |
| 603 | | var container = try self.startTuple( |
| 603 | var container = try self.beginTuple( |
| 604 | 604 | .{ .whitespace_style = .{ .fields = vector.len } }, |
| 605 | 605 | ); |
| 606 | 606 | for (0..vector.len) |i| { |
| 607 | 607 | try container.fieldArbitraryDepth(val[i], options); |
| 608 | 608 | } |
| 609 | | try container.finish(); |
| 609 | try container.end(); |
| 610 | 610 | }, |
| 611 | 611 | |
| 612 | 612 | else => comptime unreachable, |
| ... | ... | @@ -691,18 +691,18 @@ pub fn Serializer(Writer: type) type { |
| 691 | 691 | comptime assert(canSerializeType(@TypeOf(val))); |
| 692 | 692 | switch (@typeInfo(@TypeOf(val))) { |
| 693 | 693 | .@"struct" => { |
| 694 | | var container = try self.startTuple(.{ .whitespace_style = .{ .fields = val.len } }); |
| 694 | var container = try self.beginTuple(.{ .whitespace_style = .{ .fields = val.len } }); |
| 695 | 695 | inline for (val) |item_val| { |
| 696 | 696 | try container.fieldArbitraryDepth(item_val, options); |
| 697 | 697 | } |
| 698 | | try container.finish(); |
| 698 | try container.end(); |
| 699 | 699 | }, |
| 700 | 700 | .pointer, .array => { |
| 701 | | var container = try self.startTuple(.{ .whitespace_style = .{ .fields = val.len } }); |
| 701 | var container = try self.beginTuple(.{ .whitespace_style = .{ .fields = val.len } }); |
| 702 | 702 | for (val) |item_val| { |
| 703 | 703 | try container.fieldArbitraryDepth(item_val, options); |
| 704 | 704 | } |
| 705 | | try container.finish(); |
| 705 | try container.end(); |
| 706 | 706 | }, |
| 707 | 707 | else => comptime unreachable, |
| 708 | 708 | } |
| ... | ... | @@ -767,19 +767,19 @@ pub fn Serializer(Writer: type) type { |
| 767 | 767 | } |
| 768 | 768 | |
| 769 | 769 | /// Create a `Struct` for writing ZON structs field by field. |
| 770 | | pub fn startStruct( |
| 770 | pub fn beginStruct( |
| 771 | 771 | self: *Self, |
| 772 | 772 | options: SerializeContainerOptions, |
| 773 | 773 | ) Writer.Error!Struct { |
| 774 | | return Struct.start(self, options); |
| 774 | return Struct.begin(self, options); |
| 775 | 775 | } |
| 776 | 776 | |
| 777 | 777 | /// Creates a `Tuple` for writing ZON tuples field by field. |
| 778 | | pub fn startTuple( |
| 778 | pub fn beginTuple( |
| 779 | 779 | self: *Self, |
| 780 | 780 | options: SerializeContainerOptions, |
| 781 | 781 | ) Writer.Error!Tuple { |
| 782 | | return Tuple.start(self, options); |
| 782 | return Tuple.begin(self, options); |
| 783 | 783 | } |
| 784 | 784 | |
| 785 | 785 | fn indent(self: *Self) Writer.Error!void { |
| ... | ... | @@ -812,17 +812,17 @@ pub fn Serializer(Writer: type) type { |
| 812 | 812 | pub const Tuple = struct { |
| 813 | 813 | container: Container, |
| 814 | 814 | |
| 815 | | fn start(parent: *Self, options: SerializeContainerOptions) Writer.Error!Tuple { |
| 815 | fn begin(parent: *Self, options: SerializeContainerOptions) Writer.Error!Tuple { |
| 816 | 816 | return .{ |
| 817 | | .container = try Container.start(parent, .anon, options), |
| 817 | .container = try Container.begin(parent, .anon, options), |
| 818 | 818 | }; |
| 819 | 819 | } |
| 820 | 820 | |
| 821 | 821 | /// Finishes serializing the tuple. |
| 822 | 822 | /// |
| 823 | 823 | /// Prints a trailing comma as configured when appropriate, and the closing bracket. |
| 824 | | pub fn finish(self: *Tuple) Writer.Error!void { |
| 825 | | try self.container.finish(); |
| 824 | pub fn end(self: *Tuple) Writer.Error!void { |
| 825 | try self.container.end(); |
| 826 | 826 | self.* = undefined; |
| 827 | 827 | } |
| 828 | 828 | |
| ... | ... | @@ -855,6 +855,24 @@ pub fn Serializer(Writer: type) type { |
| 855 | 855 | try self.container.fieldArbitraryDepth(null, val, options); |
| 856 | 856 | } |
| 857 | 857 | |
| 858 | /// Starts a field with a struct as a value. Returns the struct. |
| 859 | pub fn beginStructField( |
| 860 | self: *Tuple, |
| 861 | options: SerializeContainerOptions, |
| 862 | ) Writer.Error!Struct { |
| 863 | try self.fieldPrefix(); |
| 864 | return self.container.serializer.beginStruct(options); |
| 865 | } |
| 866 | |
| 867 | /// Starts a field with a tuple as a value. Returns the tuple. |
| 868 | pub fn beginTupleField( |
| 869 | self: *Tuple, |
| 870 | options: SerializeContainerOptions, |
| 871 | ) Writer.Error!Tuple { |
| 872 | try self.fieldPrefix(); |
| 873 | return self.container.serializer.beginTuple(options); |
| 874 | } |
| 875 | |
| 858 | 876 | /// Print a field prefix. This prints any necessary commas, and whitespace as |
| 859 | 877 | /// configured. Useful if you want to serialize the field value yourself. |
| 860 | 878 | pub fn fieldPrefix(self: *Tuple) Writer.Error!void { |
| ... | ... | @@ -866,17 +884,17 @@ pub fn Serializer(Writer: type) type { |
| 866 | 884 | pub const Struct = struct { |
| 867 | 885 | container: Container, |
| 868 | 886 | |
| 869 | | fn start(parent: *Self, options: SerializeContainerOptions) Writer.Error!Struct { |
| 887 | fn begin(parent: *Self, options: SerializeContainerOptions) Writer.Error!Struct { |
| 870 | 888 | return .{ |
| 871 | | .container = try Container.start(parent, .named, options), |
| 889 | .container = try Container.begin(parent, .named, options), |
| 872 | 890 | }; |
| 873 | 891 | } |
| 874 | 892 | |
| 875 | 893 | /// Finishes serializing the struct. |
| 876 | 894 | /// |
| 877 | 895 | /// Prints a trailing comma as configured when appropriate, and the closing bracket. |
| 878 | | pub fn finish(self: *Struct) Writer.Error!void { |
| 879 | | try self.container.finish(); |
| 896 | pub fn end(self: *Struct) Writer.Error!void { |
| 897 | try self.container.end(); |
| 880 | 898 | self.* = undefined; |
| 881 | 899 | } |
| 882 | 900 | |
| ... | ... | @@ -912,6 +930,26 @@ pub fn Serializer(Writer: type) type { |
| 912 | 930 | try self.container.fieldArbitraryDepth(name, val, options); |
| 913 | 931 | } |
| 914 | 932 | |
| 933 | /// Starts a field with a struct as a value. Returns the struct. |
| 934 | pub fn beginStructField( |
| 935 | self: *Struct, |
| 936 | name: []const u8, |
| 937 | options: SerializeContainerOptions, |
| 938 | ) Writer.Error!Struct { |
| 939 | try self.fieldPrefix(name); |
| 940 | return self.container.serializer.beginStruct(options); |
| 941 | } |
| 942 | |
| 943 | /// Starts a field with a tuple as a value. Returns the tuple. |
| 944 | pub fn beginTupleField( |
| 945 | self: *Struct, |
| 946 | name: []const u8, |
| 947 | options: SerializeContainerOptions, |
| 948 | ) Writer.Error!Tuple { |
| 949 | try self.fieldPrefix(name); |
| 950 | return self.container.serializer.beginTuple(options); |
| 951 | } |
| 952 | |
| 915 | 953 | /// Print a field prefix. This prints any necessary commas, the field name (escaped if |
| 916 | 954 | /// necessary) and whitespace as configured. Useful if you want to serialize the field |
| 917 | 955 | /// value yourself. |
| ... | ... | @@ -928,7 +966,7 @@ pub fn Serializer(Writer: type) type { |
| 928 | 966 | options: SerializeContainerOptions, |
| 929 | 967 | empty: bool, |
| 930 | 968 | |
| 931 | | fn start( |
| 969 | fn begin( |
| 932 | 970 | sz: *Self, |
| 933 | 971 | field_style: FieldStyle, |
| 934 | 972 | options: SerializeContainerOptions, |
| ... | ... | @@ -943,7 +981,7 @@ pub fn Serializer(Writer: type) type { |
| 943 | 981 | }; |
| 944 | 982 | } |
| 945 | 983 | |
| 946 | | fn finish(self: *Container) Writer.Error!void { |
| 984 | fn end(self: *Container) Writer.Error!void { |
| 947 | 985 | if (self.options.shouldWrap()) self.serializer.indent_level -|= 1; |
| 948 | 986 | if (!self.empty) { |
| 949 | 987 | if (self.options.shouldWrap()) { |
| ... | ... | @@ -1139,52 +1177,52 @@ test "std.zon stringify whitespace, low level API" { |
| 1139 | 1177 | |
| 1140 | 1178 | // Empty containers |
| 1141 | 1179 | { |
| 1142 | | var container = try sz.startStruct(.{}); |
| 1143 | | try container.finish(); |
| 1180 | var container = try sz.beginStruct(.{}); |
| 1181 | try container.end(); |
| 1144 | 1182 | try std.testing.expectEqualStrings(".{}", buf.items); |
| 1145 | 1183 | buf.clearRetainingCapacity(); |
| 1146 | 1184 | } |
| 1147 | 1185 | |
| 1148 | 1186 | { |
| 1149 | | var container = try sz.startTuple(.{}); |
| 1150 | | try container.finish(); |
| 1187 | var container = try sz.beginTuple(.{}); |
| 1188 | try container.end(); |
| 1151 | 1189 | try std.testing.expectEqualStrings(".{}", buf.items); |
| 1152 | 1190 | buf.clearRetainingCapacity(); |
| 1153 | 1191 | } |
| 1154 | 1192 | |
| 1155 | 1193 | { |
| 1156 | | var container = try sz.startStruct(.{ .whitespace_style = .{ .wrap = false } }); |
| 1157 | | try container.finish(); |
| 1194 | var container = try sz.beginStruct(.{ .whitespace_style = .{ .wrap = false } }); |
| 1195 | try container.end(); |
| 1158 | 1196 | try std.testing.expectEqualStrings(".{}", buf.items); |
| 1159 | 1197 | buf.clearRetainingCapacity(); |
| 1160 | 1198 | } |
| 1161 | 1199 | |
| 1162 | 1200 | { |
| 1163 | | var container = try sz.startTuple(.{ .whitespace_style = .{ .wrap = false } }); |
| 1164 | | try container.finish(); |
| 1201 | var container = try sz.beginTuple(.{ .whitespace_style = .{ .wrap = false } }); |
| 1202 | try container.end(); |
| 1165 | 1203 | try std.testing.expectEqualStrings(".{}", buf.items); |
| 1166 | 1204 | buf.clearRetainingCapacity(); |
| 1167 | 1205 | } |
| 1168 | 1206 | |
| 1169 | 1207 | { |
| 1170 | | var container = try sz.startStruct(.{ .whitespace_style = .{ .fields = 0 } }); |
| 1171 | | try container.finish(); |
| 1208 | var container = try sz.beginStruct(.{ .whitespace_style = .{ .fields = 0 } }); |
| 1209 | try container.end(); |
| 1172 | 1210 | try std.testing.expectEqualStrings(".{}", buf.items); |
| 1173 | 1211 | buf.clearRetainingCapacity(); |
| 1174 | 1212 | } |
| 1175 | 1213 | |
| 1176 | 1214 | { |
| 1177 | | var container = try sz.startTuple(.{ .whitespace_style = .{ .fields = 0 } }); |
| 1178 | | try container.finish(); |
| 1215 | var container = try sz.beginTuple(.{ .whitespace_style = .{ .fields = 0 } }); |
| 1216 | try container.end(); |
| 1179 | 1217 | try std.testing.expectEqualStrings(".{}", buf.items); |
| 1180 | 1218 | buf.clearRetainingCapacity(); |
| 1181 | 1219 | } |
| 1182 | 1220 | |
| 1183 | 1221 | // Size 1 |
| 1184 | 1222 | { |
| 1185 | | var container = try sz.startStruct(.{}); |
| 1223 | var container = try sz.beginStruct(.{}); |
| 1186 | 1224 | try container.field("a", 1, .{}); |
| 1187 | | try container.finish(); |
| 1225 | try container.end(); |
| 1188 | 1226 | if (whitespace) { |
| 1189 | 1227 | try std.testing.expectEqualStrings( |
| 1190 | 1228 | \\.{ |
| ... | ... | @@ -1198,9 +1236,9 @@ test "std.zon stringify whitespace, low level API" { |
| 1198 | 1236 | } |
| 1199 | 1237 | |
| 1200 | 1238 | { |
| 1201 | | var container = try sz.startTuple(.{}); |
| 1239 | var container = try sz.beginTuple(.{}); |
| 1202 | 1240 | try container.field(1, .{}); |
| 1203 | | try container.finish(); |
| 1241 | try container.end(); |
| 1204 | 1242 | if (whitespace) { |
| 1205 | 1243 | try std.testing.expectEqualStrings( |
| 1206 | 1244 | \\.{ |
| ... | ... | @@ -1214,9 +1252,9 @@ test "std.zon stringify whitespace, low level API" { |
| 1214 | 1252 | } |
| 1215 | 1253 | |
| 1216 | 1254 | { |
| 1217 | | var container = try sz.startStruct(.{ .whitespace_style = .{ .wrap = false } }); |
| 1255 | var container = try sz.beginStruct(.{ .whitespace_style = .{ .wrap = false } }); |
| 1218 | 1256 | try container.field("a", 1, .{}); |
| 1219 | | try container.finish(); |
| 1257 | try container.end(); |
| 1220 | 1258 | if (whitespace) { |
| 1221 | 1259 | try std.testing.expectEqualStrings(".{ .a = 1 }", buf.items); |
| 1222 | 1260 | } else { |
| ... | ... | @@ -1228,9 +1266,9 @@ test "std.zon stringify whitespace, low level API" { |
| 1228 | 1266 | { |
| 1229 | 1267 | // We get extra spaces here, since we didn't know up front that there would only be one |
| 1230 | 1268 | // field. |
| 1231 | | var container = try sz.startTuple(.{ .whitespace_style = .{ .wrap = false } }); |
| 1269 | var container = try sz.beginTuple(.{ .whitespace_style = .{ .wrap = false } }); |
| 1232 | 1270 | try container.field(1, .{}); |
| 1233 | | try container.finish(); |
| 1271 | try container.end(); |
| 1234 | 1272 | if (whitespace) { |
| 1235 | 1273 | try std.testing.expectEqualStrings(".{ 1 }", buf.items); |
| 1236 | 1274 | } else { |
| ... | ... | @@ -1240,9 +1278,9 @@ test "std.zon stringify whitespace, low level API" { |
| 1240 | 1278 | } |
| 1241 | 1279 | |
| 1242 | 1280 | { |
| 1243 | | var container = try sz.startStruct(.{ .whitespace_style = .{ .fields = 1 } }); |
| 1281 | var container = try sz.beginStruct(.{ .whitespace_style = .{ .fields = 1 } }); |
| 1244 | 1282 | try container.field("a", 1, .{}); |
| 1245 | | try container.finish(); |
| 1283 | try container.end(); |
| 1246 | 1284 | if (whitespace) { |
| 1247 | 1285 | try std.testing.expectEqualStrings(".{ .a = 1 }", buf.items); |
| 1248 | 1286 | } else { |
| ... | ... | @@ -1252,19 +1290,19 @@ test "std.zon stringify whitespace, low level API" { |
| 1252 | 1290 | } |
| 1253 | 1291 | |
| 1254 | 1292 | { |
| 1255 | | var container = try sz.startTuple(.{ .whitespace_style = .{ .fields = 1 } }); |
| 1293 | var container = try sz.beginTuple(.{ .whitespace_style = .{ .fields = 1 } }); |
| 1256 | 1294 | try container.field(1, .{}); |
| 1257 | | try container.finish(); |
| 1295 | try container.end(); |
| 1258 | 1296 | try std.testing.expectEqualStrings(".{1}", buf.items); |
| 1259 | 1297 | buf.clearRetainingCapacity(); |
| 1260 | 1298 | } |
| 1261 | 1299 | |
| 1262 | 1300 | // Size 2 |
| 1263 | 1301 | { |
| 1264 | | var container = try sz.startStruct(.{}); |
| 1302 | var container = try sz.beginStruct(.{}); |
| 1265 | 1303 | try container.field("a", 1, .{}); |
| 1266 | 1304 | try container.field("b", 2, .{}); |
| 1267 | | try container.finish(); |
| 1305 | try container.end(); |
| 1268 | 1306 | if (whitespace) { |
| 1269 | 1307 | try std.testing.expectEqualStrings( |
| 1270 | 1308 | \\.{ |
| ... | ... | @@ -1279,10 +1317,10 @@ test "std.zon stringify whitespace, low level API" { |
| 1279 | 1317 | } |
| 1280 | 1318 | |
| 1281 | 1319 | { |
| 1282 | | var container = try sz.startTuple(.{}); |
| 1320 | var container = try sz.beginTuple(.{}); |
| 1283 | 1321 | try container.field(1, .{}); |
| 1284 | 1322 | try container.field(2, .{}); |
| 1285 | | try container.finish(); |
| 1323 | try container.end(); |
| 1286 | 1324 | if (whitespace) { |
| 1287 | 1325 | try std.testing.expectEqualStrings( |
| 1288 | 1326 | \\.{ |
| ... | ... | @@ -1297,10 +1335,10 @@ test "std.zon stringify whitespace, low level API" { |
| 1297 | 1335 | } |
| 1298 | 1336 | |
| 1299 | 1337 | { |
| 1300 | | var container = try sz.startStruct(.{ .whitespace_style = .{ .wrap = false } }); |
| 1338 | var container = try sz.beginStruct(.{ .whitespace_style = .{ .wrap = false } }); |
| 1301 | 1339 | try container.field("a", 1, .{}); |
| 1302 | 1340 | try container.field("b", 2, .{}); |
| 1303 | | try container.finish(); |
| 1341 | try container.end(); |
| 1304 | 1342 | if (whitespace) { |
| 1305 | 1343 | try std.testing.expectEqualStrings(".{ .a = 1, .b = 2 }", buf.items); |
| 1306 | 1344 | } else { |
| ... | ... | @@ -1310,10 +1348,10 @@ test "std.zon stringify whitespace, low level API" { |
| 1310 | 1348 | } |
| 1311 | 1349 | |
| 1312 | 1350 | { |
| 1313 | | var container = try sz.startTuple(.{ .whitespace_style = .{ .wrap = false } }); |
| 1351 | var container = try sz.beginTuple(.{ .whitespace_style = .{ .wrap = false } }); |
| 1314 | 1352 | try container.field(1, .{}); |
| 1315 | 1353 | try container.field(2, .{}); |
| 1316 | | try container.finish(); |
| 1354 | try container.end(); |
| 1317 | 1355 | if (whitespace) { |
| 1318 | 1356 | try std.testing.expectEqualStrings(".{ 1, 2 }", buf.items); |
| 1319 | 1357 | } else { |
| ... | ... | @@ -1323,10 +1361,10 @@ test "std.zon stringify whitespace, low level API" { |
| 1323 | 1361 | } |
| 1324 | 1362 | |
| 1325 | 1363 | { |
| 1326 | | var container = try sz.startStruct(.{ .whitespace_style = .{ .fields = 2 } }); |
| 1364 | var container = try sz.beginStruct(.{ .whitespace_style = .{ .fields = 2 } }); |
| 1327 | 1365 | try container.field("a", 1, .{}); |
| 1328 | 1366 | try container.field("b", 2, .{}); |
| 1329 | | try container.finish(); |
| 1367 | try container.end(); |
| 1330 | 1368 | if (whitespace) { |
| 1331 | 1369 | try std.testing.expectEqualStrings(".{ .a = 1, .b = 2 }", buf.items); |
| 1332 | 1370 | } else { |
| ... | ... | @@ -1336,10 +1374,10 @@ test "std.zon stringify whitespace, low level API" { |
| 1336 | 1374 | } |
| 1337 | 1375 | |
| 1338 | 1376 | { |
| 1339 | | var container = try sz.startTuple(.{ .whitespace_style = .{ .fields = 2 } }); |
| 1377 | var container = try sz.beginTuple(.{ .whitespace_style = .{ .fields = 2 } }); |
| 1340 | 1378 | try container.field(1, .{}); |
| 1341 | 1379 | try container.field(2, .{}); |
| 1342 | | try container.finish(); |
| 1380 | try container.end(); |
| 1343 | 1381 | if (whitespace) { |
| 1344 | 1382 | try std.testing.expectEqualStrings(".{ 1, 2 }", buf.items); |
| 1345 | 1383 | } else { |
| ... | ... | @@ -1350,11 +1388,11 @@ test "std.zon stringify whitespace, low level API" { |
| 1350 | 1388 | |
| 1351 | 1389 | // Size 3 |
| 1352 | 1390 | { |
| 1353 | | var container = try sz.startStruct(.{}); |
| 1391 | var container = try sz.beginStruct(.{}); |
| 1354 | 1392 | try container.field("a", 1, .{}); |
| 1355 | 1393 | try container.field("b", 2, .{}); |
| 1356 | 1394 | try container.field("c", 3, .{}); |
| 1357 | | try container.finish(); |
| 1395 | try container.end(); |
| 1358 | 1396 | if (whitespace) { |
| 1359 | 1397 | try std.testing.expectEqualStrings( |
| 1360 | 1398 | \\.{ |
| ... | ... | @@ -1370,11 +1408,11 @@ test "std.zon stringify whitespace, low level API" { |
| 1370 | 1408 | } |
| 1371 | 1409 | |
| 1372 | 1410 | { |
| 1373 | | var container = try sz.startTuple(.{}); |
| 1411 | var container = try sz.beginTuple(.{}); |
| 1374 | 1412 | try container.field(1, .{}); |
| 1375 | 1413 | try container.field(2, .{}); |
| 1376 | 1414 | try container.field(3, .{}); |
| 1377 | | try container.finish(); |
| 1415 | try container.end(); |
| 1378 | 1416 | if (whitespace) { |
| 1379 | 1417 | try std.testing.expectEqualStrings( |
| 1380 | 1418 | \\.{ |
| ... | ... | @@ -1390,11 +1428,11 @@ test "std.zon stringify whitespace, low level API" { |
| 1390 | 1428 | } |
| 1391 | 1429 | |
| 1392 | 1430 | { |
| 1393 | | var container = try sz.startStruct(.{ .whitespace_style = .{ .wrap = false } }); |
| 1431 | var container = try sz.beginStruct(.{ .whitespace_style = .{ .wrap = false } }); |
| 1394 | 1432 | try container.field("a", 1, .{}); |
| 1395 | 1433 | try container.field("b", 2, .{}); |
| 1396 | 1434 | try container.field("c", 3, .{}); |
| 1397 | | try container.finish(); |
| 1435 | try container.end(); |
| 1398 | 1436 | if (whitespace) { |
| 1399 | 1437 | try std.testing.expectEqualStrings(".{ .a = 1, .b = 2, .c = 3 }", buf.items); |
| 1400 | 1438 | } else { |
| ... | ... | @@ -1404,11 +1442,11 @@ test "std.zon stringify whitespace, low level API" { |
| 1404 | 1442 | } |
| 1405 | 1443 | |
| 1406 | 1444 | { |
| 1407 | | var container = try sz.startTuple(.{ .whitespace_style = .{ .wrap = false } }); |
| 1445 | var container = try sz.beginTuple(.{ .whitespace_style = .{ .wrap = false } }); |
| 1408 | 1446 | try container.field(1, .{}); |
| 1409 | 1447 | try container.field(2, .{}); |
| 1410 | 1448 | try container.field(3, .{}); |
| 1411 | | try container.finish(); |
| 1449 | try container.end(); |
| 1412 | 1450 | if (whitespace) { |
| 1413 | 1451 | try std.testing.expectEqualStrings(".{ 1, 2, 3 }", buf.items); |
| 1414 | 1452 | } else { |
| ... | ... | @@ -1418,11 +1456,11 @@ test "std.zon stringify whitespace, low level API" { |
| 1418 | 1456 | } |
| 1419 | 1457 | |
| 1420 | 1458 | { |
| 1421 | | var container = try sz.startStruct(.{ .whitespace_style = .{ .fields = 3 } }); |
| 1459 | var container = try sz.beginStruct(.{ .whitespace_style = .{ .fields = 3 } }); |
| 1422 | 1460 | try container.field("a", 1, .{}); |
| 1423 | 1461 | try container.field("b", 2, .{}); |
| 1424 | 1462 | try container.field("c", 3, .{}); |
| 1425 | | try container.finish(); |
| 1463 | try container.end(); |
| 1426 | 1464 | if (whitespace) { |
| 1427 | 1465 | try std.testing.expectEqualStrings( |
| 1428 | 1466 | \\.{ |
| ... | ... | @@ -1438,11 +1476,11 @@ test "std.zon stringify whitespace, low level API" { |
| 1438 | 1476 | } |
| 1439 | 1477 | |
| 1440 | 1478 | { |
| 1441 | | var container = try sz.startTuple(.{ .whitespace_style = .{ .fields = 3 } }); |
| 1479 | var container = try sz.beginTuple(.{ .whitespace_style = .{ .fields = 3 } }); |
| 1442 | 1480 | try container.field(1, .{}); |
| 1443 | 1481 | try container.field(2, .{}); |
| 1444 | 1482 | try container.field(3, .{}); |
| 1445 | | try container.finish(); |
| 1483 | try container.end(); |
| 1446 | 1484 | if (whitespace) { |
| 1447 | 1485 | try std.testing.expectEqualStrings( |
| 1448 | 1486 | \\.{ |
| ... | ... | @@ -1459,10 +1497,10 @@ test "std.zon stringify whitespace, low level API" { |
| 1459 | 1497 | |
| 1460 | 1498 | // Nested objects where the outer container doesn't wrap but the inner containers do |
| 1461 | 1499 | { |
| 1462 | | var container = try sz.startStruct(.{ .whitespace_style = .{ .wrap = false } }); |
| 1500 | var container = try sz.beginStruct(.{ .whitespace_style = .{ .wrap = false } }); |
| 1463 | 1501 | try container.field("first", .{ 1, 2, 3 }, .{}); |
| 1464 | 1502 | try container.field("second", .{ 4, 5, 6 }, .{}); |
| 1465 | | try container.finish(); |
| 1503 | try container.end(); |
| 1466 | 1504 | if (whitespace) { |
| 1467 | 1505 | try std.testing.expectEqualStrings( |
| 1468 | 1506 | \\.{ .first = .{ |
| ... | ... | @@ -2016,26 +2054,26 @@ test "std.zon depth limits" { |
| 2016 | 2054 | try sz.value(3, .{}); |
| 2017 | 2055 | try sz.valueArbitraryDepth(maybe_recurse, .{}); |
| 2018 | 2056 | |
| 2019 | | var s = try sz.startStruct(.{}); |
| 2057 | var s = try sz.beginStruct(.{}); |
| 2020 | 2058 | try std.testing.expectError(error.ExceededMaxDepth, s.fieldMaxDepth("a", 1, .{}, 0)); |
| 2021 | 2059 | try s.fieldMaxDepth("b", 4, .{}, 1); |
| 2022 | 2060 | try s.field("c", 5, .{}); |
| 2023 | 2061 | try s.fieldArbitraryDepth("d", maybe_recurse, .{}); |
| 2024 | | try s.finish(); |
| 2062 | try s.end(); |
| 2025 | 2063 | |
| 2026 | | var t = try sz.startTuple(.{}); |
| 2064 | var t = try sz.beginTuple(.{}); |
| 2027 | 2065 | try std.testing.expectError(error.ExceededMaxDepth, t.fieldMaxDepth(1, .{}, 0)); |
| 2028 | 2066 | try t.fieldMaxDepth(6, .{}, 1); |
| 2029 | 2067 | try t.field(7, .{}); |
| 2030 | 2068 | try t.fieldArbitraryDepth(maybe_recurse, .{}); |
| 2031 | | try t.finish(); |
| 2069 | try t.end(); |
| 2032 | 2070 | |
| 2033 | | var a = try sz.startTuple(.{}); |
| 2071 | var a = try sz.beginTuple(.{}); |
| 2034 | 2072 | try std.testing.expectError(error.ExceededMaxDepth, a.fieldMaxDepth(1, .{}, 0)); |
| 2035 | 2073 | try a.fieldMaxDepth(8, .{}, 1); |
| 2036 | 2074 | try a.field(9, .{}); |
| 2037 | 2075 | try a.fieldArbitraryDepth(maybe_recurse, .{}); |
| 2038 | | try a.finish(); |
| 2076 | try a.end(); |
| 2039 | 2077 | |
| 2040 | 2078 | try std.testing.expectEqualStrings( |
| 2041 | 2079 | \\23.{}.{ |
| ... | ... | @@ -2315,3 +2353,73 @@ test "std.zon pointers" { |
| 2315 | 2353 | , val, .{}); |
| 2316 | 2354 | } |
| 2317 | 2355 | } |
| 2356 | |
| 2357 | test "std.zon tuple/struct field" { |
| 2358 | var buf = std.ArrayList(u8).init(std.testing.allocator); |
| 2359 | defer buf.deinit(); |
| 2360 | var sz = serializer(buf.writer(), .{}); |
| 2361 | |
| 2362 | // Test on structs |
| 2363 | { |
| 2364 | var root = try sz.beginStruct(.{}); |
| 2365 | { |
| 2366 | var tuple = try root.beginTupleField("foo", .{}); |
| 2367 | try tuple.field(0, .{}); |
| 2368 | try tuple.field(1, .{}); |
| 2369 | try tuple.end(); |
| 2370 | } |
| 2371 | { |
| 2372 | var strct = try root.beginStructField("bar", .{}); |
| 2373 | try strct.field("a", 0, .{}); |
| 2374 | try strct.field("b", 1, .{}); |
| 2375 | try strct.end(); |
| 2376 | } |
| 2377 | try root.end(); |
| 2378 | |
| 2379 | try std.testing.expectEqualStrings( |
| 2380 | \\.{ |
| 2381 | \\ .foo = .{ |
| 2382 | \\ 0, |
| 2383 | \\ 1, |
| 2384 | \\ }, |
| 2385 | \\ .bar = .{ |
| 2386 | \\ .a = 0, |
| 2387 | \\ .b = 1, |
| 2388 | \\ }, |
| 2389 | \\} |
| 2390 | , buf.items); |
| 2391 | buf.clearRetainingCapacity(); |
| 2392 | } |
| 2393 | |
| 2394 | // Test on tuples |
| 2395 | { |
| 2396 | var root = try sz.beginTuple(.{}); |
| 2397 | { |
| 2398 | var tuple = try root.beginTupleField(.{}); |
| 2399 | try tuple.field(0, .{}); |
| 2400 | try tuple.field(1, .{}); |
| 2401 | try tuple.end(); |
| 2402 | } |
| 2403 | { |
| 2404 | var strct = try root.beginStructField(.{}); |
| 2405 | try strct.field("a", 0, .{}); |
| 2406 | try strct.field("b", 1, .{}); |
| 2407 | try strct.end(); |
| 2408 | } |
| 2409 | try root.end(); |
| 2410 | |
| 2411 | try std.testing.expectEqualStrings( |
| 2412 | \\.{ |
| 2413 | \\ .{ |
| 2414 | \\ 0, |
| 2415 | \\ 1, |
| 2416 | \\ }, |
| 2417 | \\ .{ |
| 2418 | \\ .a = 0, |
| 2419 | \\ .b = 1, |
| 2420 | \\ }, |
| 2421 | \\} |
| 2422 | , buf.items); |
| 2423 | buf.clearRetainingCapacity(); |
| 2424 | } |
| 2425 | } |