| ... | ... | @@ -40,15 +40,12 @@ pub const SerializeOptions = struct { |
| 40 | 40 | /// Serialize the given value as ZON. |
| 41 | 41 | /// |
| 42 | 42 | /// It is asserted at comptime that `@TypeOf(val)` is not a recursive type. |
| 43 | | pub fn serialize( |
| 44 | | val: anytype, |
| 45 | | options: SerializeOptions, |
| 46 | | writer: anytype, |
| 47 | | ) @TypeOf(writer).Error!void { |
| 48 | | var sz = serializer(writer, .{ |
| 49 | | .whitespace = options.whitespace, |
| 50 | | }); |
| 51 | | try sz.value(val, .{ |
| 43 | pub fn serialize(val: anytype, options: SerializeOptions, writer: *std.io.BufferedWriter) anyerror!void { |
| 44 | var s: Serializer = .{ |
| 45 | .writer = writer, |
| 46 | .options = .{ .whitespace = options.whitespace }, |
| 47 | }; |
| 48 | try s.value(val, .{ |
| 52 | 49 | .emit_codepoint_literals = options.emit_codepoint_literals, |
| 53 | 50 | .emit_strings_as_containers = options.emit_strings_as_containers, |
| 54 | 51 | .emit_default_optional_fields = options.emit_default_optional_fields, |
| ... | ... | @@ -62,13 +59,14 @@ pub fn serialize( |
| 62 | 59 | pub fn serializeMaxDepth( |
| 63 | 60 | val: anytype, |
| 64 | 61 | options: SerializeOptions, |
| 65 | | writer: anytype, |
| 62 | writer: *std.io.BufferedWriter, |
| 66 | 63 | depth: usize, |
| 67 | | ) (@TypeOf(writer).Error || error{ExceededMaxDepth})!void { |
| 68 | | var sz = serializer(writer, .{ |
| 69 | | .whitespace = options.whitespace, |
| 70 | | }); |
| 71 | | try sz.valueMaxDepth(val, .{ |
| 64 | ) anyerror!void { |
| 65 | var s: Serializer = .{ |
| 66 | .writer = writer, |
| 67 | .options = .{ .whitespace = options.whitespace }, |
| 68 | }; |
| 69 | try s.valueMaxDepth(val, .{ |
| 72 | 70 | .emit_codepoint_literals = options.emit_codepoint_literals, |
| 73 | 71 | .emit_strings_as_containers = options.emit_strings_as_containers, |
| 74 | 72 | .emit_default_optional_fields = options.emit_default_optional_fields, |
| ... | ... | @@ -81,44 +79,45 @@ pub fn serializeMaxDepth( |
| 81 | 79 | pub fn serializeArbitraryDepth( |
| 82 | 80 | val: anytype, |
| 83 | 81 | options: SerializeOptions, |
| 84 | | writer: anytype, |
| 85 | | ) @TypeOf(writer).Error!void { |
| 86 | | var sz = serializer(writer, .{ |
| 87 | | .whitespace = options.whitespace, |
| 88 | | }); |
| 89 | | try sz.valueArbitraryDepth(val, .{ |
| 82 | writer: *std.io.BufferedWriter, |
| 83 | ) anyerror!void { |
| 84 | var s: Serializer = .{ |
| 85 | .writer = writer, |
| 86 | .options = .{ .whitespace = options.whitespace }, |
| 87 | }; |
| 88 | try s.valueArbitraryDepth(val, .{ |
| 90 | 89 | .emit_codepoint_literals = options.emit_codepoint_literals, |
| 91 | 90 | .emit_strings_as_containers = options.emit_strings_as_containers, |
| 92 | 91 | .emit_default_optional_fields = options.emit_default_optional_fields, |
| 93 | 92 | }); |
| 94 | 93 | } |
| 95 | 94 | |
| 96 | | fn typeIsRecursive(comptime T: type) bool { |
| 97 | | return comptime typeIsRecursiveImpl(T, &.{}); |
| 95 | inline fn typeIsRecursive(comptime T: type) bool { |
| 96 | return comptime typeIsRecursiveInner(T, &.{}); |
| 98 | 97 | } |
| 99 | 98 | |
| 100 | | fn typeIsRecursiveImpl(comptime T: type, comptime prev_visited: []const type) bool { |
| 99 | fn typeIsRecursiveInner(comptime T: type, comptime prev_visited: []const type) bool { |
| 101 | 100 | for (prev_visited) |V| { |
| 102 | 101 | if (V == T) return true; |
| 103 | 102 | } |
| 104 | 103 | const visited = prev_visited ++ .{T}; |
| 105 | 104 | |
| 106 | 105 | return switch (@typeInfo(T)) { |
| 107 | | .pointer => |pointer| typeIsRecursiveImpl(pointer.child, visited), |
| 108 | | .optional => |optional| typeIsRecursiveImpl(optional.child, visited), |
| 109 | | .array => |array| typeIsRecursiveImpl(array.child, visited), |
| 110 | | .vector => |vector| typeIsRecursiveImpl(vector.child, visited), |
| 106 | .pointer => |pointer| typeIsRecursiveInner(pointer.child, visited), |
| 107 | .optional => |optional| typeIsRecursiveInner(optional.child, visited), |
| 108 | .array => |array| typeIsRecursiveInner(array.child, visited), |
| 109 | .vector => |vector| typeIsRecursiveInner(vector.child, visited), |
| 111 | 110 | .@"struct" => |@"struct"| for (@"struct".fields) |field| { |
| 112 | | if (typeIsRecursiveImpl(field.type, visited)) break true; |
| 111 | if (typeIsRecursiveInner(field.type, visited)) break true; |
| 113 | 112 | } else false, |
| 114 | 113 | .@"union" => |@"union"| inline for (@"union".fields) |field| { |
| 115 | | if (typeIsRecursiveImpl(field.type, visited)) break true; |
| 114 | if (typeIsRecursiveInner(field.type, visited)) break true; |
| 116 | 115 | } else false, |
| 117 | 116 | else => false, |
| 118 | 117 | }; |
| 119 | 118 | } |
| 120 | 119 | |
| 121 | | fn canSerializeType(T: type) bool { |
| 120 | inline fn canSerializeType(T: type) bool { |
| 122 | 121 | comptime return canSerializeTypeInner(T, &.{}, false); |
| 123 | 122 | } |
| 124 | 123 | |
| ... | ... | @@ -343,12 +342,6 @@ test "std.zon checkValueDepth" { |
| 343 | 342 | try expectValueDepthEquals(3, @as([]const []const u8, &.{&.{ 1, 2, 3 }})); |
| 344 | 343 | } |
| 345 | 344 | |
| 346 | | /// Options for `Serializer`. |
| 347 | | pub const SerializerOptions = struct { |
| 348 | | /// If false, only syntactically necessary whitespace is emitted. |
| 349 | | whitespace: bool = true, |
| 350 | | }; |
| 351 | | |
| 352 | 345 | /// Determines when to emit Unicode code point literals as opposed to integer literals. |
| 353 | 346 | pub const EmitCodepointLiterals = enum { |
| 354 | 347 | /// Never emit Unicode code point literals. |
| ... | ... | @@ -440,633 +433,610 @@ pub const SerializeContainerOptions = struct { |
| 440 | 433 | /// For manual serialization of containers, see: |
| 441 | 434 | /// * `beginStruct` |
| 442 | 435 | /// * `beginTuple` |
| 443 | | /// |
| 444 | | /// # Example |
| 445 | | /// ```zig |
| 446 | | /// var sz = serializer(writer, .{}); |
| 447 | | /// var vec2 = try sz.beginStruct(.{}); |
| 448 | | /// try vec2.field("x", 1.5, .{}); |
| 449 | | /// try vec2.fieldPrefix(); |
| 450 | | /// try sz.value(2.5); |
| 451 | | /// try vec2.end(); |
| 452 | | /// ``` |
| 453 | | pub fn Serializer(Writer: type) type { |
| 454 | | return struct { |
| 455 | | const Self = @This(); |
| 456 | | |
| 457 | | options: SerializerOptions, |
| 458 | | indent_level: u8, |
| 459 | | writer: Writer, |
| 460 | | |
| 461 | | /// Initialize a serializer. |
| 462 | | fn init(writer: Writer, options: SerializerOptions) Self { |
| 463 | | return .{ |
| 464 | | .options = options, |
| 465 | | .writer = writer, |
| 466 | | .indent_level = 0, |
| 467 | | }; |
| 468 | | } |
| 436 | pub const Serializer = struct { |
| 437 | options: Options, |
| 438 | indent_level: u8 = 0, |
| 439 | writer: *std.io.BufferedWriter, |
| 440 | |
| 441 | pub const Options = struct { |
| 442 | /// If false, only syntactically necessary whitespace is emitted. |
| 443 | whitespace: bool = true, |
| 444 | }; |
| 469 | 445 | |
| 470 | | /// Serialize a value, similar to `serialize`. |
| 471 | | pub fn value(self: *Self, val: anytype, options: ValueOptions) Writer.Error!void { |
| 472 | | comptime assert(!typeIsRecursive(@TypeOf(val))); |
| 473 | | return self.valueArbitraryDepth(val, options); |
| 474 | | } |
| 446 | /// Serialize a value, similar to `serialize`. |
| 447 | pub fn value(self: *Serializer, val: anytype, options: ValueOptions) anyerror!void { |
| 448 | comptime assert(!typeIsRecursive(@TypeOf(val))); |
| 449 | return self.valueArbitraryDepth(val, options); |
| 450 | } |
| 475 | 451 | |
| 476 | | /// Serialize a value, similar to `serializeMaxDepth`. |
| 477 | | pub fn valueMaxDepth( |
| 478 | | self: *Self, |
| 479 | | val: anytype, |
| 480 | | options: ValueOptions, |
| 481 | | depth: usize, |
| 482 | | ) (Writer.Error || error{ExceededMaxDepth})!void { |
| 483 | | try checkValueDepth(val, depth); |
| 484 | | return self.valueArbitraryDepth(val, options); |
| 485 | | } |
| 452 | /// Serialize a value, similar to `serializeMaxDepth`. |
| 453 | /// Can return `error.ExceededMaxDepth`. |
| 454 | pub fn valueMaxDepth(self: *Serializer, val: anytype, options: ValueOptions, depth: usize) anyerror!void { |
| 455 | try checkValueDepth(val, depth); |
| 456 | return self.valueArbitraryDepth(val, options); |
| 457 | } |
| 486 | 458 | |
| 487 | | /// Serialize a value, similar to `serializeArbitraryDepth`. |
| 488 | | pub fn valueArbitraryDepth( |
| 489 | | self: *Self, |
| 490 | | val: anytype, |
| 491 | | options: ValueOptions, |
| 492 | | ) Writer.Error!void { |
| 493 | | comptime assert(canSerializeType(@TypeOf(val))); |
| 494 | | switch (@typeInfo(@TypeOf(val))) { |
| 495 | | .int, .comptime_int => if (options.emit_codepoint_literals.emitAsCodepoint(val)) |c| { |
| 496 | | self.codePoint(c) catch |err| switch (err) { |
| 497 | | error.InvalidCodepoint => unreachable, // Already validated |
| 498 | | else => |e| return e, |
| 499 | | }; |
| 500 | | } else { |
| 501 | | try self.int(val); |
| 502 | | }, |
| 503 | | .float, .comptime_float => try self.float(val), |
| 504 | | .bool, .null => try std.fmt.format(self.writer, "{}", .{val}), |
| 505 | | .enum_literal => try self.ident(@tagName(val)), |
| 506 | | .@"enum" => try self.ident(@tagName(val)), |
| 507 | | .pointer => |pointer| { |
| 508 | | // Try to serialize as a string |
| 509 | | const item: ?type = switch (@typeInfo(pointer.child)) { |
| 510 | | .array => |array| array.child, |
| 511 | | else => if (pointer.size == .slice) pointer.child else null, |
| 512 | | }; |
| 513 | | if (item == u8 and |
| 514 | | (pointer.sentinel() == null or pointer.sentinel() == 0) and |
| 515 | | !options.emit_strings_as_containers) |
| 516 | | { |
| 517 | | return try self.string(val); |
| 518 | | } |
| 459 | /// Serialize a value, similar to `serializeArbitraryDepth`. |
| 460 | pub fn valueArbitraryDepth(self: *Serializer, val: anytype, options: ValueOptions) anyerror!void { |
| 461 | comptime assert(canSerializeType(@TypeOf(val))); |
| 462 | switch (@typeInfo(@TypeOf(val))) { |
| 463 | .int, .comptime_int => if (options.emit_codepoint_literals.emitAsCodepoint(val)) |c| { |
| 464 | self.codePoint(c) catch |err| switch (err) { |
| 465 | error.InvalidCodepoint => unreachable, // Already validated |
| 466 | else => |e| return e, |
| 467 | }; |
| 468 | } else { |
| 469 | try self.int(val); |
| 470 | }, |
| 471 | .float, .comptime_float => try self.float(val), |
| 472 | .bool, .null => try std.fmt.format(self.writer, "{}", .{val}), |
| 473 | .enum_literal => try self.ident(@tagName(val)), |
| 474 | .@"enum" => try self.ident(@tagName(val)), |
| 475 | .pointer => |pointer| { |
| 476 | // Try to serialize as a string |
| 477 | const item: ?type = switch (@typeInfo(pointer.child)) { |
| 478 | .array => |array| array.child, |
| 479 | else => if (pointer.size == .slice) pointer.child else null, |
| 480 | }; |
| 481 | if (item == u8 and |
| 482 | (pointer.sentinel() == null or pointer.sentinel() == 0) and |
| 483 | !options.emit_strings_as_containers) |
| 484 | { |
| 485 | return try self.string(val); |
| 486 | } |
| 519 | 487 | |
| 520 | | // Serialize as either a tuple or as the child type |
| 521 | | switch (pointer.size) { |
| 522 | | .slice => try self.tupleImpl(val, options), |
| 523 | | .one => try self.valueArbitraryDepth(val.*, options), |
| 524 | | else => comptime unreachable, |
| 525 | | } |
| 526 | | }, |
| 527 | | .array => { |
| 528 | | var container = try self.beginTuple( |
| 529 | | .{ .whitespace_style = .{ .fields = val.len } }, |
| 530 | | ); |
| 531 | | for (val) |item_val| { |
| 532 | | try container.fieldArbitraryDepth(item_val, options); |
| 533 | | } |
| 534 | | try container.end(); |
| 535 | | }, |
| 536 | | .@"struct" => |@"struct"| if (@"struct".is_tuple) { |
| 537 | | var container = try self.beginTuple( |
| 538 | | .{ .whitespace_style = .{ .fields = @"struct".fields.len } }, |
| 539 | | ); |
| 540 | | inline for (val) |field_value| { |
| 541 | | try container.fieldArbitraryDepth(field_value, options); |
| 542 | | } |
| 543 | | try container.end(); |
| 544 | | } else { |
| 545 | | // Decide which fields to emit |
| 546 | | const fields, const skipped: [@"struct".fields.len]bool = if (options.emit_default_optional_fields) b: { |
| 547 | | break :b .{ @"struct".fields.len, @splat(false) }; |
| 548 | | } else b: { |
| 549 | | var fields = @"struct".fields.len; |
| 550 | | var skipped: [@"struct".fields.len]bool = @splat(false); |
| 551 | | inline for (@"struct".fields, &skipped) |field_info, *skip| { |
| 552 | | if (field_info.default_value_ptr) |ptr| { |
| 553 | | const default: *const field_info.type = @ptrCast(@alignCast(ptr)); |
| 554 | | const field_value = @field(val, field_info.name); |
| 555 | | if (std.meta.eql(field_value, default.*)) { |
| 556 | | skip.* = true; |
| 557 | | fields -= 1; |
| 558 | | } |
| 488 | // Serialize as either a tuple or as the child type |
| 489 | switch (pointer.size) { |
| 490 | .slice => try self.tupleImpl(val, options), |
| 491 | .one => try self.valueArbitraryDepth(val.*, options), |
| 492 | else => comptime unreachable, |
| 493 | } |
| 494 | }, |
| 495 | .array => { |
| 496 | var container = try self.beginTuple( |
| 497 | .{ .whitespace_style = .{ .fields = val.len } }, |
| 498 | ); |
| 499 | for (val) |item_val| { |
| 500 | try container.fieldArbitraryDepth(item_val, options); |
| 501 | } |
| 502 | try container.end(); |
| 503 | }, |
| 504 | .@"struct" => |@"struct"| if (@"struct".is_tuple) { |
| 505 | var container = try self.beginTuple( |
| 506 | .{ .whitespace_style = .{ .fields = @"struct".fields.len } }, |
| 507 | ); |
| 508 | inline for (val) |field_value| { |
| 509 | try container.fieldArbitraryDepth(field_value, options); |
| 510 | } |
| 511 | try container.end(); |
| 512 | } else { |
| 513 | // Decide which fields to emit |
| 514 | const fields, const skipped: [@"struct".fields.len]bool = if (options.emit_default_optional_fields) b: { |
| 515 | break :b .{ @"struct".fields.len, @splat(false) }; |
| 516 | } else b: { |
| 517 | var fields = @"struct".fields.len; |
| 518 | var skipped: [@"struct".fields.len]bool = @splat(false); |
| 519 | inline for (@"struct".fields, &skipped) |field_info, *skip| { |
| 520 | if (field_info.default_value_ptr) |ptr| { |
| 521 | const default: *const field_info.type = @ptrCast(@alignCast(ptr)); |
| 522 | const field_value = @field(val, field_info.name); |
| 523 | if (std.meta.eql(field_value, default.*)) { |
| 524 | skip.* = true; |
| 525 | fields -= 1; |
| 559 | 526 | } |
| 560 | 527 | } |
| 561 | | break :b .{ fields, skipped }; |
| 562 | | }; |
| 563 | | |
| 564 | | // Emit those fields |
| 565 | | var container = try self.beginStruct( |
| 566 | | .{ .whitespace_style = .{ .fields = fields } }, |
| 567 | | ); |
| 568 | | inline for (@"struct".fields, skipped) |field_info, skip| { |
| 569 | | if (!skip) { |
| 570 | | try container.fieldArbitraryDepth( |
| 571 | | field_info.name, |
| 572 | | @field(val, field_info.name), |
| 573 | | options, |
| 574 | | ); |
| 575 | | } |
| 576 | | } |
| 577 | | try container.end(); |
| 578 | | }, |
| 579 | | .@"union" => |@"union"| { |
| 580 | | comptime assert(@"union".tag_type != null); |
| 581 | | switch (val) { |
| 582 | | inline else => |pl, tag| if (@TypeOf(pl) == void) |
| 583 | | try self.writer.print(".{s}", .{@tagName(tag)}) |
| 584 | | else { |
| 585 | | var container = try self.beginStruct(.{ .whitespace_style = .{ .fields = 1 } }); |
| 586 | | |
| 587 | | try container.fieldArbitraryDepth( |
| 588 | | @tagName(tag), |
| 589 | | pl, |
| 590 | | options, |
| 591 | | ); |
| 592 | | |
| 593 | | try container.end(); |
| 594 | | }, |
| 595 | 528 | } |
| 596 | | }, |
| 597 | | .optional => if (val) |inner| { |
| 598 | | try self.valueArbitraryDepth(inner, options); |
| 599 | | } else { |
| 600 | | try self.writer.writeAll("null"); |
| 601 | | }, |
| 602 | | .vector => |vector| { |
| 603 | | var container = try self.beginTuple( |
| 604 | | .{ .whitespace_style = .{ .fields = vector.len } }, |
| 605 | | ); |
| 606 | | for (0..vector.len) |i| { |
| 607 | | try container.fieldArbitraryDepth(val[i], options); |
| 529 | break :b .{ fields, skipped }; |
| 530 | }; |
| 531 | |
| 532 | // Emit those fields |
| 533 | var container = try self.beginStruct( |
| 534 | .{ .whitespace_style = .{ .fields = fields } }, |
| 535 | ); |
| 536 | inline for (@"struct".fields, skipped) |field_info, skip| { |
| 537 | if (!skip) { |
| 538 | try container.fieldArbitraryDepth( |
| 539 | field_info.name, |
| 540 | @field(val, field_info.name), |
| 541 | options, |
| 542 | ); |
| 608 | 543 | } |
| 609 | | try container.end(); |
| 610 | | }, |
| 544 | } |
| 545 | try container.end(); |
| 546 | }, |
| 547 | .@"union" => |@"union"| { |
| 548 | comptime assert(@"union".tag_type != null); |
| 549 | switch (val) { |
| 550 | inline else => |pl, tag| if (@TypeOf(pl) == void) |
| 551 | try self.writer.print(".{s}", .{@tagName(tag)}) |
| 552 | else { |
| 553 | var container = try self.beginStruct(.{ .whitespace_style = .{ .fields = 1 } }); |
| 554 | |
| 555 | try container.fieldArbitraryDepth( |
| 556 | @tagName(tag), |
| 557 | pl, |
| 558 | options, |
| 559 | ); |
| 560 | |
| 561 | try container.end(); |
| 562 | }, |
| 563 | } |
| 564 | }, |
| 565 | .optional => if (val) |inner| { |
| 566 | try self.valueArbitraryDepth(inner, options); |
| 567 | } else { |
| 568 | try self.writer.writeAll("null"); |
| 569 | }, |
| 570 | .vector => |vector| { |
| 571 | var container = try self.beginTuple( |
| 572 | .{ .whitespace_style = .{ .fields = vector.len } }, |
| 573 | ); |
| 574 | for (0..vector.len) |i| { |
| 575 | try container.fieldArbitraryDepth(val[i], options); |
| 576 | } |
| 577 | try container.end(); |
| 578 | }, |
| 611 | 579 | |
| 612 | | else => comptime unreachable, |
| 613 | | } |
| 580 | else => comptime unreachable, |
| 614 | 581 | } |
| 582 | } |
| 615 | 583 | |
| 616 | | /// Serialize an integer. |
| 617 | | pub fn int(self: *Self, val: anytype) Writer.Error!void { |
| 618 | | try std.fmt.formatInt(val, 10, .lower, .{}, self.writer); |
| 619 | | } |
| 620 | | |
| 621 | | /// Serialize a float. |
| 622 | | pub fn float(self: *Self, val: anytype) Writer.Error!void { |
| 623 | | switch (@typeInfo(@TypeOf(val))) { |
| 624 | | .float => if (std.math.isNan(val)) { |
| 625 | | return self.writer.writeAll("nan"); |
| 626 | | } else if (std.math.isPositiveInf(val)) { |
| 627 | | return self.writer.writeAll("inf"); |
| 628 | | } else if (std.math.isNegativeInf(val)) { |
| 629 | | return self.writer.writeAll("-inf"); |
| 630 | | } else if (std.math.isNegativeZero(val)) { |
| 631 | | return self.writer.writeAll("-0.0"); |
| 632 | | } else { |
| 633 | | try std.fmt.format(self.writer, "{d}", .{val}); |
| 634 | | }, |
| 635 | | .comptime_float => if (val == 0) { |
| 636 | | return self.writer.writeAll("0"); |
| 637 | | } else { |
| 638 | | try std.fmt.format(self.writer, "{d}", .{val}); |
| 639 | | }, |
| 640 | | else => comptime unreachable, |
| 641 | | } |
| 642 | | } |
| 584 | /// Serialize an integer. |
| 585 | pub fn int(self: *Serializer, val: anytype) anyerror!void { |
| 586 | try std.fmt.formatInt(val, 10, .lower, .{}, self.writer); |
| 587 | } |
| 643 | 588 | |
| 644 | | /// Serialize `name` as an identifier prefixed with `.`. |
| 645 | | /// |
| 646 | | /// Escapes the identifier if necessary. |
| 647 | | pub fn ident(self: *Self, name: []const u8) Writer.Error!void { |
| 648 | | try self.writer.print(".{p_}", .{std.zig.fmtId(name)}); |
| 589 | /// Serialize a float. |
| 590 | pub fn float(self: *Serializer, val: anytype) anyerror!void { |
| 591 | switch (@typeInfo(@TypeOf(val))) { |
| 592 | .float => if (std.math.isNan(val)) { |
| 593 | return self.writer.writeAll("nan"); |
| 594 | } else if (std.math.isPositiveInf(val)) { |
| 595 | return self.writer.writeAll("inf"); |
| 596 | } else if (std.math.isNegativeInf(val)) { |
| 597 | return self.writer.writeAll("-inf"); |
| 598 | } else { |
| 599 | try std.fmt.format(self.writer, "{d}", .{val}); |
| 600 | }, |
| 601 | .comptime_float => try std.fmt.format(self.writer, "{d}", .{val}), |
| 602 | else => comptime unreachable, |
| 649 | 603 | } |
| 604 | } |
| 650 | 605 | |
| 651 | | /// Serialize `val` as a Unicode codepoint. |
| 652 | | /// |
| 653 | | /// Returns `error.InvalidCodepoint` if `val` is not a valid Unicode codepoint. |
| 654 | | pub fn codePoint( |
| 655 | | self: *Self, |
| 656 | | val: u21, |
| 657 | | ) (Writer.Error || error{InvalidCodepoint})!void { |
| 658 | | var buf: [8]u8 = undefined; |
| 659 | | const len = std.unicode.utf8Encode(val, &buf) catch return error.InvalidCodepoint; |
| 660 | | const str = buf[0..len]; |
| 661 | | try std.fmt.format(self.writer, "'{'}'", .{std.zig.fmtEscapes(str)}); |
| 662 | | } |
| 663 | | |
| 664 | | /// Like `value`, but always serializes `val` as a tuple. |
| 665 | | /// |
| 666 | | /// Will fail at comptime if `val` is not a tuple, array, pointer to an array, or slice. |
| 667 | | pub fn tuple(self: *Self, val: anytype, options: ValueOptions) Writer.Error!void { |
| 668 | | comptime assert(!typeIsRecursive(@TypeOf(val))); |
| 669 | | try self.tupleArbitraryDepth(val, options); |
| 670 | | } |
| 606 | /// Serialize `name` as an identifier prefixed with `.`. |
| 607 | /// |
| 608 | /// Escapes the identifier if necessary. |
| 609 | pub fn ident(self: *Serializer, name: []const u8) anyerror!void { |
| 610 | try self.writer.print(".{p_}", .{std.zig.fmtId(name)}); |
| 611 | } |
| 671 | 612 | |
| 672 | | /// Like `tuple`, but recursive types are allowed. |
| 673 | | /// |
| 674 | | /// Returns `error.ExceededMaxDepth` if `depth` is exceeded. |
| 675 | | pub fn tupleMaxDepth( |
| 676 | | self: *Self, |
| 677 | | val: anytype, |
| 678 | | options: ValueOptions, |
| 679 | | depth: usize, |
| 680 | | ) (Writer.Error || error{ExceededMaxDepth})!void { |
| 681 | | try checkValueDepth(val, depth); |
| 682 | | try self.tupleArbitraryDepth(val, options); |
| 683 | | } |
| 613 | /// Serialize `val` as a Unicode codepoint. |
| 614 | /// |
| 615 | /// Returns `error.InvalidCodepoint` if `val` is not a valid Unicode codepoint. |
| 616 | pub fn codePoint( |
| 617 | self: *Serializer, |
| 618 | val: u21, |
| 619 | ) anyerror!void { |
| 620 | var buf: [8]u8 = undefined; |
| 621 | const len = std.unicode.utf8Encode(val, &buf) catch return error.InvalidCodepoint; |
| 622 | const str = buf[0..len]; |
| 623 | try std.fmt.format(self.writer, "'{'}'", .{std.zig.fmtEscapes(str)}); |
| 624 | } |
| 684 | 625 | |
| 685 | | /// Like `tuple`, but recursive types are allowed. |
| 686 | | /// |
| 687 | | /// It is the caller's responsibility to ensure that `val` does not contain cycles. |
| 688 | | pub fn tupleArbitraryDepth( |
| 689 | | self: *Self, |
| 690 | | val: anytype, |
| 691 | | options: ValueOptions, |
| 692 | | ) Writer.Error!void { |
| 693 | | try self.tupleImpl(val, options); |
| 694 | | } |
| 626 | /// Like `value`, but always serializes `val` as a tuple. |
| 627 | /// |
| 628 | /// Will fail at comptime if `val` is not a tuple, array, pointer to an array, or slice. |
| 629 | pub fn tuple(self: *Serializer, val: anytype, options: ValueOptions) anyerror!void { |
| 630 | comptime assert(!typeIsRecursive(@TypeOf(val))); |
| 631 | try self.tupleArbitraryDepth(val, options); |
| 632 | } |
| 695 | 633 | |
| 696 | | fn tupleImpl(self: *Self, val: anytype, options: ValueOptions) Writer.Error!void { |
| 697 | | comptime assert(canSerializeType(@TypeOf(val))); |
| 698 | | switch (@typeInfo(@TypeOf(val))) { |
| 699 | | .@"struct" => { |
| 700 | | var container = try self.beginTuple(.{ .whitespace_style = .{ .fields = val.len } }); |
| 701 | | inline for (val) |item_val| { |
| 702 | | try container.fieldArbitraryDepth(item_val, options); |
| 703 | | } |
| 704 | | try container.end(); |
| 705 | | }, |
| 706 | | .pointer, .array => { |
| 707 | | var container = try self.beginTuple(.{ .whitespace_style = .{ .fields = val.len } }); |
| 708 | | for (val) |item_val| { |
| 709 | | try container.fieldArbitraryDepth(item_val, options); |
| 710 | | } |
| 711 | | try container.end(); |
| 712 | | }, |
| 713 | | else => comptime unreachable, |
| 714 | | } |
| 715 | | } |
| 634 | /// Like `tuple`, but recursive types are allowed. |
| 635 | /// |
| 636 | /// Returns `error.ExceededMaxDepth` if `depth` is exceeded. |
| 637 | pub fn tupleMaxDepth( |
| 638 | self: *Serializer, |
| 639 | val: anytype, |
| 640 | options: ValueOptions, |
| 641 | depth: usize, |
| 642 | ) anyerror!void { |
| 643 | try checkValueDepth(val, depth); |
| 644 | try self.tupleArbitraryDepth(val, options); |
| 645 | } |
| 646 | |
| 647 | /// Like `tuple`, but recursive types are allowed. |
| 648 | /// |
| 649 | /// It is the caller's responsibility to ensure that `val` does not contain cycles. |
| 650 | pub fn tupleArbitraryDepth( |
| 651 | self: *Serializer, |
| 652 | val: anytype, |
| 653 | options: ValueOptions, |
| 654 | ) anyerror!void { |
| 655 | try self.tupleImpl(val, options); |
| 656 | } |
| 716 | 657 | |
| 717 | | /// Like `value`, but always serializes `val` as a string. |
| 718 | | pub fn string(self: *Self, val: []const u8) Writer.Error!void { |
| 719 | | try std.fmt.format(self.writer, "\"{}\"", .{std.zig.fmtEscapes(val)}); |
| 658 | fn tupleImpl(self: *Serializer, val: anytype, options: ValueOptions) anyerror!void { |
| 659 | comptime assert(canSerializeType(@TypeOf(val))); |
| 660 | switch (@typeInfo(@TypeOf(val))) { |
| 661 | .@"struct" => { |
| 662 | var container = try self.beginTuple(.{ .whitespace_style = .{ .fields = val.len } }); |
| 663 | inline for (val) |item_val| { |
| 664 | try container.fieldArbitraryDepth(item_val, options); |
| 665 | } |
| 666 | try container.end(); |
| 667 | }, |
| 668 | .pointer, .array => { |
| 669 | var container = try self.beginTuple(.{ .whitespace_style = .{ .fields = val.len } }); |
| 670 | for (val) |item_val| { |
| 671 | try container.fieldArbitraryDepth(item_val, options); |
| 672 | } |
| 673 | try container.end(); |
| 674 | }, |
| 675 | else => comptime unreachable, |
| 720 | 676 | } |
| 677 | } |
| 721 | 678 | |
| 722 | | /// Options for formatting multiline strings. |
| 723 | | pub const MultilineStringOptions = struct { |
| 724 | | /// If top level is true, whitespace before and after the multiline string is elided. |
| 725 | | /// If it is true, a newline is printed, then the value, followed by a newline, and if |
| 726 | | /// whitespace is true any necessary indentation follows. |
| 727 | | top_level: bool = false, |
| 728 | | }; |
| 679 | /// Like `value`, but always serializes `val` as a string. |
| 680 | pub fn string(self: *Serializer, val: []const u8) anyerror!void { |
| 681 | try std.fmt.format(self.writer, "\"{}\"", .{std.zig.fmtEscapes(val)}); |
| 682 | } |
| 729 | 683 | |
| 730 | | /// Like `value`, but always serializes to a multiline string literal. |
| 731 | | /// |
| 732 | | /// Returns `error.InnerCarriageReturn` if `val` contains a CR not followed by a newline, |
| 733 | | /// since multiline strings cannot represent CR without a following newline. |
| 734 | | pub fn multilineString( |
| 735 | | self: *Self, |
| 736 | | val: []const u8, |
| 737 | | options: MultilineStringOptions, |
| 738 | | ) (Writer.Error || error{InnerCarriageReturn})!void { |
| 739 | | // Make sure the string does not contain any carriage returns not followed by a newline |
| 740 | | var i: usize = 0; |
| 741 | | while (i < val.len) : (i += 1) { |
| 742 | | if (val[i] == '\r') { |
| 743 | | if (i + 1 < val.len) { |
| 744 | | if (val[i + 1] == '\n') { |
| 745 | | i += 1; |
| 746 | | continue; |
| 747 | | } |
| 684 | /// Options for formatting multiline strings. |
| 685 | pub const MultilineStringOptions = struct { |
| 686 | /// If top level is true, whitespace before and after the multiline string is elided. |
| 687 | /// If it is true, a newline is printed, then the value, followed by a newline, and if |
| 688 | /// whitespace is true any necessary indentation follows. |
| 689 | top_level: bool = false, |
| 690 | }; |
| 691 | |
| 692 | /// Like `value`, but always serializes to a multiline string literal. |
| 693 | /// |
| 694 | /// Returns `error.InnerCarriageReturn` if `val` contains a CR not followed by a newline, |
| 695 | /// since multiline strings cannot represent CR without a following newline. |
| 696 | pub fn multilineString( |
| 697 | self: *Serializer, |
| 698 | val: []const u8, |
| 699 | options: MultilineStringOptions, |
| 700 | ) anyerror!void { |
| 701 | // Make sure the string does not contain any carriage returns not followed by a newline |
| 702 | var i: usize = 0; |
| 703 | while (i < val.len) : (i += 1) { |
| 704 | if (val[i] == '\r') { |
| 705 | if (i + 1 < val.len) { |
| 706 | if (val[i + 1] == '\n') { |
| 707 | i += 1; |
| 708 | continue; |
| 748 | 709 | } |
| 749 | | return error.InnerCarriageReturn; |
| 750 | 710 | } |
| 711 | return error.InnerCarriageReturn; |
| 751 | 712 | } |
| 713 | } |
| 752 | 714 | |
| 753 | | if (!options.top_level) { |
| 754 | | try self.newline(); |
| 755 | | try self.indent(); |
| 756 | | } |
| 715 | if (!options.top_level) { |
| 716 | try self.newline(); |
| 717 | try self.indent(); |
| 718 | } |
| 757 | 719 | |
| 758 | | try self.writer.writeAll("\\\\"); |
| 759 | | for (val) |c| { |
| 760 | | if (c != '\r') { |
| 761 | | try self.writer.writeByte(c); // We write newlines here even if whitespace off |
| 762 | | if (c == '\n') { |
| 763 | | try self.indent(); |
| 764 | | try self.writer.writeAll("\\\\"); |
| 765 | | } |
| 720 | try self.writer.writeAll("\\\\"); |
| 721 | for (val) |c| { |
| 722 | if (c != '\r') { |
| 723 | try self.writer.writeByte(c); // We write newlines here even if whitespace off |
| 724 | if (c == '\n') { |
| 725 | try self.indent(); |
| 726 | try self.writer.writeAll("\\\\"); |
| 766 | 727 | } |
| 767 | 728 | } |
| 768 | | |
| 769 | | if (!options.top_level) { |
| 770 | | try self.writer.writeByte('\n'); // Even if whitespace off |
| 771 | | try self.indent(); |
| 772 | | } |
| 773 | 729 | } |
| 774 | 730 | |
| 775 | | /// Create a `Struct` for writing ZON structs field by field. |
| 776 | | pub fn beginStruct( |
| 777 | | self: *Self, |
| 778 | | options: SerializeContainerOptions, |
| 779 | | ) Writer.Error!Struct { |
| 780 | | return Struct.begin(self, options); |
| 731 | if (!options.top_level) { |
| 732 | try self.writer.writeByte('\n'); // Even if whitespace off |
| 733 | try self.indent(); |
| 781 | 734 | } |
| 735 | } |
| 782 | 736 | |
| 783 | | /// Creates a `Tuple` for writing ZON tuples field by field. |
| 784 | | pub fn beginTuple( |
| 785 | | self: *Self, |
| 786 | | options: SerializeContainerOptions, |
| 787 | | ) Writer.Error!Tuple { |
| 788 | | return Tuple.begin(self, options); |
| 789 | | } |
| 737 | /// Create a `Struct` for writing ZON structs field by field. |
| 738 | pub fn beginStruct( |
| 739 | self: *Serializer, |
| 740 | options: SerializeContainerOptions, |
| 741 | ) anyerror!Struct { |
| 742 | return Struct.begin(self, options); |
| 743 | } |
| 790 | 744 | |
| 791 | | fn indent(self: *Self) Writer.Error!void { |
| 792 | | if (self.options.whitespace) { |
| 793 | | try self.writer.writeByteNTimes(' ', 4 * self.indent_level); |
| 794 | | } |
| 745 | /// Creates a `Tuple` for writing ZON tuples field by field. |
| 746 | pub fn beginTuple( |
| 747 | self: *Serializer, |
| 748 | options: SerializeContainerOptions, |
| 749 | ) anyerror!Tuple { |
| 750 | return Tuple.begin(self, options); |
| 751 | } |
| 752 | |
| 753 | fn indent(self: *Serializer) anyerror!void { |
| 754 | if (self.options.whitespace) { |
| 755 | try self.writer.writeByteNTimes(' ', 4 * self.indent_level); |
| 795 | 756 | } |
| 757 | } |
| 796 | 758 | |
| 797 | | fn newline(self: *Self) Writer.Error!void { |
| 798 | | if (self.options.whitespace) { |
| 799 | | try self.writer.writeByte('\n'); |
| 800 | | } |
| 759 | fn newline(self: *Serializer) anyerror!void { |
| 760 | if (self.options.whitespace) { |
| 761 | try self.writer.writeByte('\n'); |
| 801 | 762 | } |
| 763 | } |
| 802 | 764 | |
| 803 | | fn newlineOrSpace(self: *Self, len: usize) Writer.Error!void { |
| 804 | | if (self.containerShouldWrap(len)) { |
| 805 | | try self.newline(); |
| 806 | | } else { |
| 807 | | try self.space(); |
| 808 | | } |
| 765 | fn newlineOrSpace(self: *Serializer, len: usize) anyerror!void { |
| 766 | if (self.containerShouldWrap(len)) { |
| 767 | try self.newline(); |
| 768 | } else { |
| 769 | try self.space(); |
| 809 | 770 | } |
| 771 | } |
| 810 | 772 | |
| 811 | | fn space(self: *Self) Writer.Error!void { |
| 812 | | if (self.options.whitespace) { |
| 813 | | try self.writer.writeByte(' '); |
| 814 | | } |
| 773 | fn space(self: *Serializer) anyerror!void { |
| 774 | if (self.options.whitespace) { |
| 775 | try self.writer.writeByte(' '); |
| 815 | 776 | } |
| 777 | } |
| 816 | 778 | |
| 817 | | /// Writes ZON tuples field by field. |
| 818 | | pub const Tuple = struct { |
| 819 | | container: Container, |
| 779 | /// Writes ZON tuples field by field. |
| 780 | pub const Tuple = struct { |
| 781 | container: Container, |
| 820 | 782 | |
| 821 | | fn begin(parent: *Self, options: SerializeContainerOptions) Writer.Error!Tuple { |
| 822 | | return .{ |
| 823 | | .container = try Container.begin(parent, .anon, options), |
| 824 | | }; |
| 825 | | } |
| 783 | fn begin(parent: *Serializer, options: SerializeContainerOptions) anyerror!Tuple { |
| 784 | return .{ |
| 785 | .container = try Container.begin(parent, .anon, options), |
| 786 | }; |
| 787 | } |
| 826 | 788 | |
| 827 | | /// Finishes serializing the tuple. |
| 828 | | /// |
| 829 | | /// Prints a trailing comma as configured when appropriate, and the closing bracket. |
| 830 | | pub fn end(self: *Tuple) Writer.Error!void { |
| 831 | | try self.container.end(); |
| 832 | | self.* = undefined; |
| 833 | | } |
| 789 | /// Finishes serializing the tuple. |
| 790 | /// |
| 791 | /// Prints a trailing comma as configured when appropriate, and the closing bracket. |
| 792 | pub fn end(self: *Tuple) anyerror!void { |
| 793 | try self.container.end(); |
| 794 | self.* = undefined; |
| 795 | } |
| 834 | 796 | |
| 835 | | /// Serialize a field. Equivalent to calling `fieldPrefix` followed by `value`. |
| 836 | | pub fn field( |
| 837 | | self: *Tuple, |
| 838 | | val: anytype, |
| 839 | | options: ValueOptions, |
| 840 | | ) Writer.Error!void { |
| 841 | | try self.container.field(null, val, options); |
| 842 | | } |
| 797 | /// Serialize a field. Equivalent to calling `fieldPrefix` followed by `value`. |
| 798 | pub fn field( |
| 799 | self: *Tuple, |
| 800 | val: anytype, |
| 801 | options: ValueOptions, |
| 802 | ) anyerror!void { |
| 803 | try self.container.field(null, val, options); |
| 804 | } |
| 843 | 805 | |
| 844 | | /// Serialize a field. Equivalent to calling `fieldPrefix` followed by `valueMaxDepth`. |
| 845 | | pub fn fieldMaxDepth( |
| 846 | | self: *Tuple, |
| 847 | | val: anytype, |
| 848 | | options: ValueOptions, |
| 849 | | depth: usize, |
| 850 | | ) (Writer.Error || error{ExceededMaxDepth})!void { |
| 851 | | try self.container.fieldMaxDepth(null, val, options, depth); |
| 852 | | } |
| 806 | /// Serialize a field. Equivalent to calling `fieldPrefix` followed by `valueMaxDepth`. |
| 807 | /// Returns `error.ExceededMaxDepth` if `depth` is exceeded. |
| 808 | pub fn fieldMaxDepth( |
| 809 | self: *Tuple, |
| 810 | val: anytype, |
| 811 | options: ValueOptions, |
| 812 | depth: usize, |
| 813 | ) anyerror!void { |
| 814 | try self.container.fieldMaxDepth(null, val, options, depth); |
| 815 | } |
| 853 | 816 | |
| 854 | | /// Serialize a field. Equivalent to calling `fieldPrefix` followed by |
| 855 | | /// `valueArbitraryDepth`. |
| 856 | | pub fn fieldArbitraryDepth( |
| 857 | | self: *Tuple, |
| 858 | | val: anytype, |
| 859 | | options: ValueOptions, |
| 860 | | ) Writer.Error!void { |
| 861 | | try self.container.fieldArbitraryDepth(null, val, options); |
| 862 | | } |
| 817 | /// Serialize a field. Equivalent to calling `fieldPrefix` followed by |
| 818 | /// `valueArbitraryDepth`. |
| 819 | pub fn fieldArbitraryDepth( |
| 820 | self: *Tuple, |
| 821 | val: anytype, |
| 822 | options: ValueOptions, |
| 823 | ) anyerror!void { |
| 824 | try self.container.fieldArbitraryDepth(null, val, options); |
| 825 | } |
| 863 | 826 | |
| 864 | | /// Starts a field with a struct as a value. Returns the struct. |
| 865 | | pub fn beginStructField( |
| 866 | | self: *Tuple, |
| 867 | | options: SerializeContainerOptions, |
| 868 | | ) Writer.Error!Struct { |
| 869 | | try self.fieldPrefix(); |
| 870 | | return self.container.serializer.beginStruct(options); |
| 871 | | } |
| 827 | /// Starts a field with a struct as a value. Returns the struct. |
| 828 | pub fn beginStructField( |
| 829 | self: *Tuple, |
| 830 | options: SerializeContainerOptions, |
| 831 | ) anyerror!Struct { |
| 832 | try self.fieldPrefix(); |
| 833 | return self.container.serializer.beginStruct(options); |
| 834 | } |
| 872 | 835 | |
| 873 | | /// Starts a field with a tuple as a value. Returns the tuple. |
| 874 | | pub fn beginTupleField( |
| 875 | | self: *Tuple, |
| 876 | | options: SerializeContainerOptions, |
| 877 | | ) Writer.Error!Tuple { |
| 878 | | try self.fieldPrefix(); |
| 879 | | return self.container.serializer.beginTuple(options); |
| 880 | | } |
| 836 | /// Starts a field with a tuple as a value. Returns the tuple. |
| 837 | pub fn beginTupleField( |
| 838 | self: *Tuple, |
| 839 | options: SerializeContainerOptions, |
| 840 | ) anyerror!Tuple { |
| 841 | try self.fieldPrefix(); |
| 842 | return self.container.serializer.beginTuple(options); |
| 843 | } |
| 881 | 844 | |
| 882 | | /// Print a field prefix. This prints any necessary commas, and whitespace as |
| 883 | | /// configured. Useful if you want to serialize the field value yourself. |
| 884 | | pub fn fieldPrefix(self: *Tuple) Writer.Error!void { |
| 885 | | try self.container.fieldPrefix(null); |
| 886 | | } |
| 887 | | }; |
| 845 | /// Print a field prefix. This prints any necessary commas, and whitespace as |
| 846 | /// configured. Useful if you want to serialize the field value yourself. |
| 847 | pub fn fieldPrefix(self: *Tuple) anyerror!void { |
| 848 | try self.container.fieldPrefix(null); |
| 849 | } |
| 850 | }; |
| 888 | 851 | |
| 889 | | /// Writes ZON structs field by field. |
| 890 | | pub const Struct = struct { |
| 891 | | container: Container, |
| 852 | /// Writes ZON structs field by field. |
| 853 | pub const Struct = struct { |
| 854 | container: Container, |
| 892 | 855 | |
| 893 | | fn begin(parent: *Self, options: SerializeContainerOptions) Writer.Error!Struct { |
| 894 | | return .{ |
| 895 | | .container = try Container.begin(parent, .named, options), |
| 896 | | }; |
| 897 | | } |
| 856 | fn begin(parent: *Serializer, options: SerializeContainerOptions) anyerror!Struct { |
| 857 | return .{ |
| 858 | .container = try Container.begin(parent, .named, options), |
| 859 | }; |
| 860 | } |
| 898 | 861 | |
| 899 | | /// Finishes serializing the struct. |
| 900 | | /// |
| 901 | | /// Prints a trailing comma as configured when appropriate, and the closing bracket. |
| 902 | | pub fn end(self: *Struct) Writer.Error!void { |
| 903 | | try self.container.end(); |
| 904 | | self.* = undefined; |
| 905 | | } |
| 862 | /// Finishes serializing the struct. |
| 863 | /// |
| 864 | /// Prints a trailing comma as configured when appropriate, and the closing bracket. |
| 865 | pub fn end(self: *Struct) anyerror!void { |
| 866 | try self.container.end(); |
| 867 | self.* = undefined; |
| 868 | } |
| 906 | 869 | |
| 907 | | /// Serialize a field. Equivalent to calling `fieldPrefix` followed by `value`. |
| 908 | | pub fn field( |
| 909 | | self: *Struct, |
| 910 | | name: []const u8, |
| 911 | | val: anytype, |
| 912 | | options: ValueOptions, |
| 913 | | ) Writer.Error!void { |
| 914 | | try self.container.field(name, val, options); |
| 915 | | } |
| 870 | /// Serialize a field. Equivalent to calling `fieldPrefix` followed by `value`. |
| 871 | pub fn field( |
| 872 | self: *Struct, |
| 873 | name: []const u8, |
| 874 | val: anytype, |
| 875 | options: ValueOptions, |
| 876 | ) anyerror!void { |
| 877 | try self.container.field(name, val, options); |
| 878 | } |
| 916 | 879 | |
| 917 | | /// Serialize a field. Equivalent to calling `fieldPrefix` followed by `valueMaxDepth`. |
| 918 | | pub fn fieldMaxDepth( |
| 919 | | self: *Struct, |
| 920 | | name: []const u8, |
| 921 | | val: anytype, |
| 922 | | options: ValueOptions, |
| 923 | | depth: usize, |
| 924 | | ) (Writer.Error || error{ExceededMaxDepth})!void { |
| 925 | | try self.container.fieldMaxDepth(name, val, options, depth); |
| 926 | | } |
| 880 | /// Serialize a field. Equivalent to calling `fieldPrefix` followed by `valueMaxDepth`. |
| 881 | /// Returns `error.ExceededMaxDepth` if `depth` is exceeded. |
| 882 | pub fn fieldMaxDepth( |
| 883 | self: *Struct, |
| 884 | name: []const u8, |
| 885 | val: anytype, |
| 886 | options: ValueOptions, |
| 887 | depth: usize, |
| 888 | ) anyerror!void { |
| 889 | try self.container.fieldMaxDepth(name, val, options, depth); |
| 890 | } |
| 927 | 891 | |
| 928 | | /// Serialize a field. Equivalent to calling `fieldPrefix` followed by |
| 929 | | /// `valueArbitraryDepth`. |
| 930 | | pub fn fieldArbitraryDepth( |
| 931 | | self: *Struct, |
| 932 | | name: []const u8, |
| 933 | | val: anytype, |
| 934 | | options: ValueOptions, |
| 935 | | ) Writer.Error!void { |
| 936 | | try self.container.fieldArbitraryDepth(name, val, options); |
| 937 | | } |
| 892 | /// Serialize a field. Equivalent to calling `fieldPrefix` followed by |
| 893 | /// `valueArbitraryDepth`. |
| 894 | pub fn fieldArbitraryDepth( |
| 895 | self: *Struct, |
| 896 | name: []const u8, |
| 897 | val: anytype, |
| 898 | options: ValueOptions, |
| 899 | ) anyerror!void { |
| 900 | try self.container.fieldArbitraryDepth(name, val, options); |
| 901 | } |
| 938 | 902 | |
| 939 | | /// Starts a field with a struct as a value. Returns the struct. |
| 940 | | pub fn beginStructField( |
| 941 | | self: *Struct, |
| 942 | | name: []const u8, |
| 943 | | options: SerializeContainerOptions, |
| 944 | | ) Writer.Error!Struct { |
| 945 | | try self.fieldPrefix(name); |
| 946 | | return self.container.serializer.beginStruct(options); |
| 947 | | } |
| 903 | /// Starts a field with a struct as a value. Returns the struct. |
| 904 | pub fn beginStructField( |
| 905 | self: *Struct, |
| 906 | name: []const u8, |
| 907 | options: SerializeContainerOptions, |
| 908 | ) anyerror!Struct { |
| 909 | try self.fieldPrefix(name); |
| 910 | return self.container.serializer.beginStruct(options); |
| 911 | } |
| 948 | 912 | |
| 949 | | /// Starts a field with a tuple as a value. Returns the tuple. |
| 950 | | pub fn beginTupleField( |
| 951 | | self: *Struct, |
| 952 | | name: []const u8, |
| 953 | | options: SerializeContainerOptions, |
| 954 | | ) Writer.Error!Tuple { |
| 955 | | try self.fieldPrefix(name); |
| 956 | | return self.container.serializer.beginTuple(options); |
| 957 | | } |
| 913 | /// Starts a field with a tuple as a value. Returns the tuple. |
| 914 | pub fn beginTupleField( |
| 915 | self: *Struct, |
| 916 | name: []const u8, |
| 917 | options: SerializeContainerOptions, |
| 918 | ) anyerror!Tuple { |
| 919 | try self.fieldPrefix(name); |
| 920 | return self.container.serializer.beginTuple(options); |
| 921 | } |
| 958 | 922 | |
| 959 | | /// Print a field prefix. This prints any necessary commas, the field name (escaped if |
| 960 | | /// necessary) and whitespace as configured. Useful if you want to serialize the field |
| 961 | | /// value yourself. |
| 962 | | pub fn fieldPrefix(self: *Struct, name: []const u8) Writer.Error!void { |
| 963 | | try self.container.fieldPrefix(name); |
| 964 | | } |
| 965 | | }; |
| 923 | /// Print a field prefix. This prints any necessary commas, the field name (escaped if |
| 924 | /// necessary) and whitespace as configured. Useful if you want to serialize the field |
| 925 | /// value yourself. |
| 926 | pub fn fieldPrefix(self: *Struct, name: []const u8) anyerror!void { |
| 927 | try self.container.fieldPrefix(name); |
| 928 | } |
| 929 | }; |
| 966 | 930 | |
| 967 | | const Container = struct { |
| 968 | | const FieldStyle = enum { named, anon }; |
| 931 | const Container = struct { |
| 932 | const FieldStyle = enum { named, anon }; |
| 969 | 933 | |
| 970 | | serializer: *Self, |
| 934 | serializer: *Serializer, |
| 935 | field_style: FieldStyle, |
| 936 | options: SerializeContainerOptions, |
| 937 | empty: bool, |
| 938 | |
| 939 | fn begin( |
| 940 | sz: *Serializer, |
| 971 | 941 | field_style: FieldStyle, |
| 972 | 942 | options: SerializeContainerOptions, |
| 973 | | empty: bool, |
| 974 | | |
| 975 | | fn begin( |
| 976 | | sz: *Self, |
| 977 | | field_style: FieldStyle, |
| 978 | | options: SerializeContainerOptions, |
| 979 | | ) Writer.Error!Container { |
| 980 | | if (options.shouldWrap()) sz.indent_level +|= 1; |
| 981 | | try sz.writer.writeAll(".{"); |
| 982 | | return .{ |
| 983 | | .serializer = sz, |
| 984 | | .field_style = field_style, |
| 985 | | .options = options, |
| 986 | | .empty = true, |
| 987 | | }; |
| 988 | | } |
| 989 | | |
| 990 | | fn end(self: *Container) Writer.Error!void { |
| 991 | | if (self.options.shouldWrap()) self.serializer.indent_level -|= 1; |
| 992 | | if (!self.empty) { |
| 993 | | if (self.options.shouldWrap()) { |
| 994 | | if (self.serializer.options.whitespace) { |
| 995 | | try self.serializer.writer.writeByte(','); |
| 996 | | } |
| 997 | | try self.serializer.newline(); |
| 998 | | try self.serializer.indent(); |
| 999 | | } else if (!self.shouldElideSpaces()) { |
| 1000 | | try self.serializer.space(); |
| 1001 | | } |
| 1002 | | } |
| 1003 | | try self.serializer.writer.writeByte('}'); |
| 1004 | | self.* = undefined; |
| 1005 | | } |
| 943 | ) anyerror!Container { |
| 944 | if (options.shouldWrap()) sz.indent_level +|= 1; |
| 945 | try sz.writer.writeAll(".{"); |
| 946 | return .{ |
| 947 | .serializer = sz, |
| 948 | .field_style = field_style, |
| 949 | .options = options, |
| 950 | .empty = true, |
| 951 | }; |
| 952 | } |
| 1006 | 953 | |
| 1007 | | fn fieldPrefix(self: *Container, name: ?[]const u8) Writer.Error!void { |
| 1008 | | if (!self.empty) { |
| 1009 | | try self.serializer.writer.writeByte(','); |
| 1010 | | } |
| 1011 | | self.empty = false; |
| 954 | fn end(self: *Container) anyerror!void { |
| 955 | if (self.options.shouldWrap()) self.serializer.indent_level -|= 1; |
| 956 | if (!self.empty) { |
| 1012 | 957 | if (self.options.shouldWrap()) { |
| 958 | if (self.serializer.options.whitespace) { |
| 959 | try self.serializer.writer.writeByte(','); |
| 960 | } |
| 1013 | 961 | try self.serializer.newline(); |
| 962 | try self.serializer.indent(); |
| 1014 | 963 | } else if (!self.shouldElideSpaces()) { |
| 1015 | 964 | try self.serializer.space(); |
| 1016 | 965 | } |
| 1017 | | if (self.options.shouldWrap()) try self.serializer.indent(); |
| 1018 | | if (name) |n| { |
| 1019 | | try self.serializer.ident(n); |
| 1020 | | try self.serializer.space(); |
| 1021 | | try self.serializer.writer.writeByte('='); |
| 1022 | | try self.serializer.space(); |
| 1023 | | } |
| 1024 | 966 | } |
| 967 | try self.serializer.writer.writeByte('}'); |
| 968 | self.* = undefined; |
| 969 | } |
| 1025 | 970 | |
| 1026 | | fn field( |
| 1027 | | self: *Container, |
| 1028 | | name: ?[]const u8, |
| 1029 | | val: anytype, |
| 1030 | | options: ValueOptions, |
| 1031 | | ) Writer.Error!void { |
| 1032 | | comptime assert(!typeIsRecursive(@TypeOf(val))); |
| 1033 | | try self.fieldArbitraryDepth(name, val, options); |
| 971 | fn fieldPrefix(self: *Container, name: ?[]const u8) anyerror!void { |
| 972 | if (!self.empty) { |
| 973 | try self.serializer.writer.writeByte(','); |
| 1034 | 974 | } |
| 1035 | | |
| 1036 | | fn fieldMaxDepth( |
| 1037 | | self: *Container, |
| 1038 | | name: ?[]const u8, |
| 1039 | | val: anytype, |
| 1040 | | options: ValueOptions, |
| 1041 | | depth: usize, |
| 1042 | | ) (Writer.Error || error{ExceededMaxDepth})!void { |
| 1043 | | try checkValueDepth(val, depth); |
| 1044 | | try self.fieldArbitraryDepth(name, val, options); |
| 975 | self.empty = false; |
| 976 | if (self.options.shouldWrap()) { |
| 977 | try self.serializer.newline(); |
| 978 | } else if (!self.shouldElideSpaces()) { |
| 979 | try self.serializer.space(); |
| 1045 | 980 | } |
| 1046 | | |
| 1047 | | fn fieldArbitraryDepth( |
| 1048 | | self: *Container, |
| 1049 | | name: ?[]const u8, |
| 1050 | | val: anytype, |
| 1051 | | options: ValueOptions, |
| 1052 | | ) Writer.Error!void { |
| 1053 | | try self.fieldPrefix(name); |
| 1054 | | try self.serializer.valueArbitraryDepth(val, options); |
| 981 | if (self.options.shouldWrap()) try self.serializer.indent(); |
| 982 | if (name) |n| { |
| 983 | try self.serializer.ident(n); |
| 984 | try self.serializer.space(); |
| 985 | try self.serializer.writer.writeByte('='); |
| 986 | try self.serializer.space(); |
| 1055 | 987 | } |
| 988 | } |
| 1056 | 989 | |
| 1057 | | fn shouldElideSpaces(self: *const Container) bool { |
| 1058 | | return switch (self.options.whitespace_style) { |
| 1059 | | .fields => |fields| self.field_style != .named and fields == 1, |
| 1060 | | else => false, |
| 1061 | | }; |
| 1062 | | } |
| 1063 | | }; |
| 990 | fn field( |
| 991 | self: *Container, |
| 992 | name: ?[]const u8, |
| 993 | val: anytype, |
| 994 | options: ValueOptions, |
| 995 | ) anyerror!void { |
| 996 | comptime assert(!typeIsRecursive(@TypeOf(val))); |
| 997 | try self.fieldArbitraryDepth(name, val, options); |
| 998 | } |
| 999 | |
| 1000 | /// Returns `error.ExceededMaxDepth` if `depth` is exceeded. |
| 1001 | fn fieldMaxDepth( |
| 1002 | self: *Container, |
| 1003 | name: ?[]const u8, |
| 1004 | val: anytype, |
| 1005 | options: ValueOptions, |
| 1006 | depth: usize, |
| 1007 | ) anyerror!void { |
| 1008 | try checkValueDepth(val, depth); |
| 1009 | try self.fieldArbitraryDepth(name, val, options); |
| 1010 | } |
| 1011 | |
| 1012 | fn fieldArbitraryDepth( |
| 1013 | self: *Container, |
| 1014 | name: ?[]const u8, |
| 1015 | val: anytype, |
| 1016 | options: ValueOptions, |
| 1017 | ) anyerror!void { |
| 1018 | try self.fieldPrefix(name); |
| 1019 | try self.serializer.valueArbitraryDepth(val, options); |
| 1020 | } |
| 1021 | |
| 1022 | fn shouldElideSpaces(self: *const Container) bool { |
| 1023 | return switch (self.options.whitespace_style) { |
| 1024 | .fields => |fields| self.field_style != .named and fields == 1, |
| 1025 | else => false, |
| 1026 | }; |
| 1027 | } |
| 1064 | 1028 | }; |
| 1065 | | } |
| 1029 | }; |
| 1066 | 1030 | |
| 1067 | | /// Creates a new `Serializer` with the given writer and options. |
| 1068 | | pub fn serializer(writer: anytype, options: SerializerOptions) Serializer(@TypeOf(writer)) { |
| 1069 | | return .init(writer, options); |
| 1031 | test Serializer { |
| 1032 | var s: Serializer = .{ |
| 1033 | .writer = std.io.null_writer, |
| 1034 | }; |
| 1035 | var vec2 = try s.beginStruct(.{}); |
| 1036 | try vec2.field("x", 1.5, .{}); |
| 1037 | try vec2.fieldPrefix(); |
| 1038 | try s.value(2.5); |
| 1039 | try vec2.end(); |
| 1070 | 1040 | } |
| 1071 | 1041 | |
| 1072 | 1042 | fn expectSerializeEqual( |
| ... | ... | @@ -1074,10 +1044,12 @@ fn expectSerializeEqual( |
| 1074 | 1044 | value: anytype, |
| 1075 | 1045 | options: SerializeOptions, |
| 1076 | 1046 | ) !void { |
| 1077 | | var buf = std.ArrayList(u8).init(std.testing.allocator); |
| 1078 | | defer buf.deinit(); |
| 1079 | | try serialize(value, options, buf.writer()); |
| 1080 | | try std.testing.expectEqualStrings(expected, buf.items); |
| 1047 | var aw: std.io.AllocatingWriter = undefined; |
| 1048 | defer aw.deinit(); |
| 1049 | const bw = aw.init(std.testing.allocator); |
| 1050 | |
| 1051 | try serialize(value, options, bw); |
| 1052 | try std.testing.expectEqualStrings(expected, aw.getWritten()); |
| 1081 | 1053 | } |
| 1082 | 1054 | |
| 1083 | 1055 | test "std.zon stringify whitespace, high level API" { |
| ... | ... | @@ -1174,59 +1146,59 @@ test "std.zon stringify whitespace, high level API" { |
| 1174 | 1146 | } |
| 1175 | 1147 | |
| 1176 | 1148 | test "std.zon stringify whitespace, low level API" { |
| 1177 | | var buf = std.ArrayList(u8).init(std.testing.allocator); |
| 1178 | | defer buf.deinit(); |
| 1179 | | var sz = serializer(buf.writer(), .{}); |
| 1149 | var aw: std.io.AllocatingWriter = undefined; |
| 1150 | defer aw.deinit(); |
| 1151 | var s: Serializer = .{ .writer = aw.init(std.testing.allocator) }; |
| 1180 | 1152 | |
| 1181 | | inline for (.{ true, false }) |whitespace| { |
| 1182 | | sz.options = .{ .whitespace = whitespace }; |
| 1153 | for ([2]bool{ true, false }) |whitespace| { |
| 1154 | s.options = .{ .whitespace = whitespace }; |
| 1183 | 1155 | |
| 1184 | 1156 | // Empty containers |
| 1185 | 1157 | { |
| 1186 | | var container = try sz.beginStruct(.{}); |
| 1158 | var container = try s.beginStruct(.{}); |
| 1187 | 1159 | try container.end(); |
| 1188 | | try std.testing.expectEqualStrings(".{}", buf.items); |
| 1189 | | buf.clearRetainingCapacity(); |
| 1160 | try std.testing.expectEqualStrings(".{}", aw.getWritten()); |
| 1161 | aw.clearRetainingCapacity(); |
| 1190 | 1162 | } |
| 1191 | 1163 | |
| 1192 | 1164 | { |
| 1193 | | var container = try sz.beginTuple(.{}); |
| 1165 | var container = try s.beginTuple(.{}); |
| 1194 | 1166 | try container.end(); |
| 1195 | | try std.testing.expectEqualStrings(".{}", buf.items); |
| 1196 | | buf.clearRetainingCapacity(); |
| 1167 | try std.testing.expectEqualStrings(".{}", aw.getWritten()); |
| 1168 | aw.clearRetainingCapacity(); |
| 1197 | 1169 | } |
| 1198 | 1170 | |
| 1199 | 1171 | { |
| 1200 | | var container = try sz.beginStruct(.{ .whitespace_style = .{ .wrap = false } }); |
| 1172 | var container = try s.beginStruct(.{ .whitespace_style = .{ .wrap = false } }); |
| 1201 | 1173 | try container.end(); |
| 1202 | | try std.testing.expectEqualStrings(".{}", buf.items); |
| 1203 | | buf.clearRetainingCapacity(); |
| 1174 | try std.testing.expectEqualStrings(".{}", aw.getWritten()); |
| 1175 | aw.clearRetainingCapacity(); |
| 1204 | 1176 | } |
| 1205 | 1177 | |
| 1206 | 1178 | { |
| 1207 | | var container = try sz.beginTuple(.{ .whitespace_style = .{ .wrap = false } }); |
| 1179 | var container = try s.beginTuple(.{ .whitespace_style = .{ .wrap = false } }); |
| 1208 | 1180 | try container.end(); |
| 1209 | | try std.testing.expectEqualStrings(".{}", buf.items); |
| 1210 | | buf.clearRetainingCapacity(); |
| 1181 | try std.testing.expectEqualStrings(".{}", aw.getWritten()); |
| 1182 | aw.clearRetainingCapacity(); |
| 1211 | 1183 | } |
| 1212 | 1184 | |
| 1213 | 1185 | { |
| 1214 | | var container = try sz.beginStruct(.{ .whitespace_style = .{ .fields = 0 } }); |
| 1186 | var container = try s.beginStruct(.{ .whitespace_style = .{ .fields = 0 } }); |
| 1215 | 1187 | try container.end(); |
| 1216 | | try std.testing.expectEqualStrings(".{}", buf.items); |
| 1217 | | buf.clearRetainingCapacity(); |
| 1188 | try std.testing.expectEqualStrings(".{}", aw.getWritten()); |
| 1189 | aw.clearRetainingCapacity(); |
| 1218 | 1190 | } |
| 1219 | 1191 | |
| 1220 | 1192 | { |
| 1221 | | var container = try sz.beginTuple(.{ .whitespace_style = .{ .fields = 0 } }); |
| 1193 | var container = try s.beginTuple(.{ .whitespace_style = .{ .fields = 0 } }); |
| 1222 | 1194 | try container.end(); |
| 1223 | | try std.testing.expectEqualStrings(".{}", buf.items); |
| 1224 | | buf.clearRetainingCapacity(); |
| 1195 | try std.testing.expectEqualStrings(".{}", aw.getWritten()); |
| 1196 | aw.clearRetainingCapacity(); |
| 1225 | 1197 | } |
| 1226 | 1198 | |
| 1227 | 1199 | // Size 1 |
| 1228 | 1200 | { |
| 1229 | | var container = try sz.beginStruct(.{}); |
| 1201 | var container = try s.beginStruct(.{}); |
| 1230 | 1202 | try container.field("a", 1, .{}); |
| 1231 | 1203 | try container.end(); |
| 1232 | 1204 | if (whitespace) { |
| ... | ... | @@ -1234,15 +1206,15 @@ test "std.zon stringify whitespace, low level API" { |
| 1234 | 1206 | \\.{ |
| 1235 | 1207 | \\ .a = 1, |
| 1236 | 1208 | \\} |
| 1237 | | , buf.items); |
| 1209 | , aw.getWritten()); |
| 1238 | 1210 | } else { |
| 1239 | | try std.testing.expectEqualStrings(".{.a=1}", buf.items); |
| 1211 | try std.testing.expectEqualStrings(".{.a=1}", aw.getWritten()); |
| 1240 | 1212 | } |
| 1241 | | buf.clearRetainingCapacity(); |
| 1213 | aw.clearRetainingCapacity(); |
| 1242 | 1214 | } |
| 1243 | 1215 | |
| 1244 | 1216 | { |
| 1245 | | var container = try sz.beginTuple(.{}); |
| 1217 | var container = try s.beginTuple(.{}); |
| 1246 | 1218 | try container.field(1, .{}); |
| 1247 | 1219 | try container.end(); |
| 1248 | 1220 | if (whitespace) { |
| ... | ... | @@ -1250,62 +1222,62 @@ test "std.zon stringify whitespace, low level API" { |
| 1250 | 1222 | \\.{ |
| 1251 | 1223 | \\ 1, |
| 1252 | 1224 | \\} |
| 1253 | | , buf.items); |
| 1225 | , aw.getWritten()); |
| 1254 | 1226 | } else { |
| 1255 | | try std.testing.expectEqualStrings(".{1}", buf.items); |
| 1227 | try std.testing.expectEqualStrings(".{1}", aw.getWritten()); |
| 1256 | 1228 | } |
| 1257 | | buf.clearRetainingCapacity(); |
| 1229 | aw.clearRetainingCapacity(); |
| 1258 | 1230 | } |
| 1259 | 1231 | |
| 1260 | 1232 | { |
| 1261 | | var container = try sz.beginStruct(.{ .whitespace_style = .{ .wrap = false } }); |
| 1233 | var container = try s.beginStruct(.{ .whitespace_style = .{ .wrap = false } }); |
| 1262 | 1234 | try container.field("a", 1, .{}); |
| 1263 | 1235 | try container.end(); |
| 1264 | 1236 | if (whitespace) { |
| 1265 | | try std.testing.expectEqualStrings(".{ .a = 1 }", buf.items); |
| 1237 | try std.testing.expectEqualStrings(".{ .a = 1 }", aw.getWritten()); |
| 1266 | 1238 | } else { |
| 1267 | | try std.testing.expectEqualStrings(".{.a=1}", buf.items); |
| 1239 | try std.testing.expectEqualStrings(".{.a=1}", aw.getWritten()); |
| 1268 | 1240 | } |
| 1269 | | buf.clearRetainingCapacity(); |
| 1241 | aw.clearRetainingCapacity(); |
| 1270 | 1242 | } |
| 1271 | 1243 | |
| 1272 | 1244 | { |
| 1273 | 1245 | // We get extra spaces here, since we didn't know up front that there would only be one |
| 1274 | 1246 | // field. |
| 1275 | | var container = try sz.beginTuple(.{ .whitespace_style = .{ .wrap = false } }); |
| 1247 | var container = try s.beginTuple(.{ .whitespace_style = .{ .wrap = false } }); |
| 1276 | 1248 | try container.field(1, .{}); |
| 1277 | 1249 | try container.end(); |
| 1278 | 1250 | if (whitespace) { |
| 1279 | | try std.testing.expectEqualStrings(".{ 1 }", buf.items); |
| 1251 | try std.testing.expectEqualStrings(".{ 1 }", aw.getWritten()); |
| 1280 | 1252 | } else { |
| 1281 | | try std.testing.expectEqualStrings(".{1}", buf.items); |
| 1253 | try std.testing.expectEqualStrings(".{1}", aw.getWritten()); |
| 1282 | 1254 | } |
| 1283 | | buf.clearRetainingCapacity(); |
| 1255 | aw.clearRetainingCapacity(); |
| 1284 | 1256 | } |
| 1285 | 1257 | |
| 1286 | 1258 | { |
| 1287 | | var container = try sz.beginStruct(.{ .whitespace_style = .{ .fields = 1 } }); |
| 1259 | var container = try s.beginStruct(.{ .whitespace_style = .{ .fields = 1 } }); |
| 1288 | 1260 | try container.field("a", 1, .{}); |
| 1289 | 1261 | try container.end(); |
| 1290 | 1262 | if (whitespace) { |
| 1291 | | try std.testing.expectEqualStrings(".{ .a = 1 }", buf.items); |
| 1263 | try std.testing.expectEqualStrings(".{ .a = 1 }", aw.getWritten()); |
| 1292 | 1264 | } else { |
| 1293 | | try std.testing.expectEqualStrings(".{.a=1}", buf.items); |
| 1265 | try std.testing.expectEqualStrings(".{.a=1}", aw.getWritten()); |
| 1294 | 1266 | } |
| 1295 | | buf.clearRetainingCapacity(); |
| 1267 | aw.clearRetainingCapacity(); |
| 1296 | 1268 | } |
| 1297 | 1269 | |
| 1298 | 1270 | { |
| 1299 | | var container = try sz.beginTuple(.{ .whitespace_style = .{ .fields = 1 } }); |
| 1271 | var container = try s.beginTuple(.{ .whitespace_style = .{ .fields = 1 } }); |
| 1300 | 1272 | try container.field(1, .{}); |
| 1301 | 1273 | try container.end(); |
| 1302 | | try std.testing.expectEqualStrings(".{1}", buf.items); |
| 1303 | | buf.clearRetainingCapacity(); |
| 1274 | try std.testing.expectEqualStrings(".{1}", aw.getWritten()); |
| 1275 | aw.clearRetainingCapacity(); |
| 1304 | 1276 | } |
| 1305 | 1277 | |
| 1306 | 1278 | // Size 2 |
| 1307 | 1279 | { |
| 1308 | | var container = try sz.beginStruct(.{}); |
| 1280 | var container = try s.beginStruct(.{}); |
| 1309 | 1281 | try container.field("a", 1, .{}); |
| 1310 | 1282 | try container.field("b", 2, .{}); |
| 1311 | 1283 | try container.end(); |
| ... | ... | @@ -1315,15 +1287,15 @@ test "std.zon stringify whitespace, low level API" { |
| 1315 | 1287 | \\ .a = 1, |
| 1316 | 1288 | \\ .b = 2, |
| 1317 | 1289 | \\} |
| 1318 | | , buf.items); |
| 1290 | , aw.getWritten()); |
| 1319 | 1291 | } else { |
| 1320 | | try std.testing.expectEqualStrings(".{.a=1,.b=2}", buf.items); |
| 1292 | try std.testing.expectEqualStrings(".{.a=1,.b=2}", aw.getWritten()); |
| 1321 | 1293 | } |
| 1322 | | buf.clearRetainingCapacity(); |
| 1294 | aw.clearRetainingCapacity(); |
| 1323 | 1295 | } |
| 1324 | 1296 | |
| 1325 | 1297 | { |
| 1326 | | var container = try sz.beginTuple(.{}); |
| 1298 | var container = try s.beginTuple(.{}); |
| 1327 | 1299 | try container.field(1, .{}); |
| 1328 | 1300 | try container.field(2, .{}); |
| 1329 | 1301 | try container.end(); |
| ... | ... | @@ -1333,68 +1305,68 @@ test "std.zon stringify whitespace, low level API" { |
| 1333 | 1305 | \\ 1, |
| 1334 | 1306 | \\ 2, |
| 1335 | 1307 | \\} |
| 1336 | | , buf.items); |
| 1308 | , aw.getWritten()); |
| 1337 | 1309 | } else { |
| 1338 | | try std.testing.expectEqualStrings(".{1,2}", buf.items); |
| 1310 | try std.testing.expectEqualStrings(".{1,2}", aw.getWritten()); |
| 1339 | 1311 | } |
| 1340 | | buf.clearRetainingCapacity(); |
| 1312 | aw.clearRetainingCapacity(); |
| 1341 | 1313 | } |
| 1342 | 1314 | |
| 1343 | 1315 | { |
| 1344 | | var container = try sz.beginStruct(.{ .whitespace_style = .{ .wrap = false } }); |
| 1316 | var container = try s.beginStruct(.{ .whitespace_style = .{ .wrap = false } }); |
| 1345 | 1317 | try container.field("a", 1, .{}); |
| 1346 | 1318 | try container.field("b", 2, .{}); |
| 1347 | 1319 | try container.end(); |
| 1348 | 1320 | if (whitespace) { |
| 1349 | | try std.testing.expectEqualStrings(".{ .a = 1, .b = 2 }", buf.items); |
| 1321 | try std.testing.expectEqualStrings(".{ .a = 1, .b = 2 }", aw.getWritten()); |
| 1350 | 1322 | } else { |
| 1351 | | try std.testing.expectEqualStrings(".{.a=1,.b=2}", buf.items); |
| 1323 | try std.testing.expectEqualStrings(".{.a=1,.b=2}", aw.getWritten()); |
| 1352 | 1324 | } |
| 1353 | | buf.clearRetainingCapacity(); |
| 1325 | aw.clearRetainingCapacity(); |
| 1354 | 1326 | } |
| 1355 | 1327 | |
| 1356 | 1328 | { |
| 1357 | | var container = try sz.beginTuple(.{ .whitespace_style = .{ .wrap = false } }); |
| 1329 | var container = try s.beginTuple(.{ .whitespace_style = .{ .wrap = false } }); |
| 1358 | 1330 | try container.field(1, .{}); |
| 1359 | 1331 | try container.field(2, .{}); |
| 1360 | 1332 | try container.end(); |
| 1361 | 1333 | if (whitespace) { |
| 1362 | | try std.testing.expectEqualStrings(".{ 1, 2 }", buf.items); |
| 1334 | try std.testing.expectEqualStrings(".{ 1, 2 }", aw.getWritten()); |
| 1363 | 1335 | } else { |
| 1364 | | try std.testing.expectEqualStrings(".{1,2}", buf.items); |
| 1336 | try std.testing.expectEqualStrings(".{1,2}", aw.getWritten()); |
| 1365 | 1337 | } |
| 1366 | | buf.clearRetainingCapacity(); |
| 1338 | aw.clearRetainingCapacity(); |
| 1367 | 1339 | } |
| 1368 | 1340 | |
| 1369 | 1341 | { |
| 1370 | | var container = try sz.beginStruct(.{ .whitespace_style = .{ .fields = 2 } }); |
| 1342 | var container = try s.beginStruct(.{ .whitespace_style = .{ .fields = 2 } }); |
| 1371 | 1343 | try container.field("a", 1, .{}); |
| 1372 | 1344 | try container.field("b", 2, .{}); |
| 1373 | 1345 | try container.end(); |
| 1374 | 1346 | if (whitespace) { |
| 1375 | | try std.testing.expectEqualStrings(".{ .a = 1, .b = 2 }", buf.items); |
| 1347 | try std.testing.expectEqualStrings(".{ .a = 1, .b = 2 }", aw.getWritten()); |
| 1376 | 1348 | } else { |
| 1377 | | try std.testing.expectEqualStrings(".{.a=1,.b=2}", buf.items); |
| 1349 | try std.testing.expectEqualStrings(".{.a=1,.b=2}", aw.getWritten()); |
| 1378 | 1350 | } |
| 1379 | | buf.clearRetainingCapacity(); |
| 1351 | aw.clearRetainingCapacity(); |
| 1380 | 1352 | } |
| 1381 | 1353 | |
| 1382 | 1354 | { |
| 1383 | | var container = try sz.beginTuple(.{ .whitespace_style = .{ .fields = 2 } }); |
| 1355 | var container = try s.beginTuple(.{ .whitespace_style = .{ .fields = 2 } }); |
| 1384 | 1356 | try container.field(1, .{}); |
| 1385 | 1357 | try container.field(2, .{}); |
| 1386 | 1358 | try container.end(); |
| 1387 | 1359 | if (whitespace) { |
| 1388 | | try std.testing.expectEqualStrings(".{ 1, 2 }", buf.items); |
| 1360 | try std.testing.expectEqualStrings(".{ 1, 2 }", aw.getWritten()); |
| 1389 | 1361 | } else { |
| 1390 | | try std.testing.expectEqualStrings(".{1,2}", buf.items); |
| 1362 | try std.testing.expectEqualStrings(".{1,2}", aw.getWritten()); |
| 1391 | 1363 | } |
| 1392 | | buf.clearRetainingCapacity(); |
| 1364 | aw.clearRetainingCapacity(); |
| 1393 | 1365 | } |
| 1394 | 1366 | |
| 1395 | 1367 | // Size 3 |
| 1396 | 1368 | { |
| 1397 | | var container = try sz.beginStruct(.{}); |
| 1369 | var container = try s.beginStruct(.{}); |
| 1398 | 1370 | try container.field("a", 1, .{}); |
| 1399 | 1371 | try container.field("b", 2, .{}); |
| 1400 | 1372 | try container.field("c", 3, .{}); |
| ... | ... | @@ -1406,15 +1378,15 @@ test "std.zon stringify whitespace, low level API" { |
| 1406 | 1378 | \\ .b = 2, |
| 1407 | 1379 | \\ .c = 3, |
| 1408 | 1380 | \\} |
| 1409 | | , buf.items); |
| 1381 | , aw.getWritten()); |
| 1410 | 1382 | } else { |
| 1411 | | try std.testing.expectEqualStrings(".{.a=1,.b=2,.c=3}", buf.items); |
| 1383 | try std.testing.expectEqualStrings(".{.a=1,.b=2,.c=3}", aw.getWritten()); |
| 1412 | 1384 | } |
| 1413 | | buf.clearRetainingCapacity(); |
| 1385 | aw.clearRetainingCapacity(); |
| 1414 | 1386 | } |
| 1415 | 1387 | |
| 1416 | 1388 | { |
| 1417 | | var container = try sz.beginTuple(.{}); |
| 1389 | var container = try s.beginTuple(.{}); |
| 1418 | 1390 | try container.field(1, .{}); |
| 1419 | 1391 | try container.field(2, .{}); |
| 1420 | 1392 | try container.field(3, .{}); |
| ... | ... | @@ -1426,43 +1398,43 @@ test "std.zon stringify whitespace, low level API" { |
| 1426 | 1398 | \\ 2, |
| 1427 | 1399 | \\ 3, |
| 1428 | 1400 | \\} |
| 1429 | | , buf.items); |
| 1401 | , aw.getWritten()); |
| 1430 | 1402 | } else { |
| 1431 | | try std.testing.expectEqualStrings(".{1,2,3}", buf.items); |
| 1403 | try std.testing.expectEqualStrings(".{1,2,3}", aw.getWritten()); |
| 1432 | 1404 | } |
| 1433 | | buf.clearRetainingCapacity(); |
| 1405 | aw.clearRetainingCapacity(); |
| 1434 | 1406 | } |
| 1435 | 1407 | |
| 1436 | 1408 | { |
| 1437 | | var container = try sz.beginStruct(.{ .whitespace_style = .{ .wrap = false } }); |
| 1409 | var container = try s.beginStruct(.{ .whitespace_style = .{ .wrap = false } }); |
| 1438 | 1410 | try container.field("a", 1, .{}); |
| 1439 | 1411 | try container.field("b", 2, .{}); |
| 1440 | 1412 | try container.field("c", 3, .{}); |
| 1441 | 1413 | try container.end(); |
| 1442 | 1414 | if (whitespace) { |
| 1443 | | try std.testing.expectEqualStrings(".{ .a = 1, .b = 2, .c = 3 }", buf.items); |
| 1415 | try std.testing.expectEqualStrings(".{ .a = 1, .b = 2, .c = 3 }", aw.getWritten()); |
| 1444 | 1416 | } else { |
| 1445 | | try std.testing.expectEqualStrings(".{.a=1,.b=2,.c=3}", buf.items); |
| 1417 | try std.testing.expectEqualStrings(".{.a=1,.b=2,.c=3}", aw.getWritten()); |
| 1446 | 1418 | } |
| 1447 | | buf.clearRetainingCapacity(); |
| 1419 | aw.clearRetainingCapacity(); |
| 1448 | 1420 | } |
| 1449 | 1421 | |
| 1450 | 1422 | { |
| 1451 | | var container = try sz.beginTuple(.{ .whitespace_style = .{ .wrap = false } }); |
| 1423 | var container = try s.beginTuple(.{ .whitespace_style = .{ .wrap = false } }); |
| 1452 | 1424 | try container.field(1, .{}); |
| 1453 | 1425 | try container.field(2, .{}); |
| 1454 | 1426 | try container.field(3, .{}); |
| 1455 | 1427 | try container.end(); |
| 1456 | 1428 | if (whitespace) { |
| 1457 | | try std.testing.expectEqualStrings(".{ 1, 2, 3 }", buf.items); |
| 1429 | try std.testing.expectEqualStrings(".{ 1, 2, 3 }", aw.getWritten()); |
| 1458 | 1430 | } else { |
| 1459 | | try std.testing.expectEqualStrings(".{1,2,3}", buf.items); |
| 1431 | try std.testing.expectEqualStrings(".{1,2,3}", aw.getWritten()); |
| 1460 | 1432 | } |
| 1461 | | buf.clearRetainingCapacity(); |
| 1433 | aw.clearRetainingCapacity(); |
| 1462 | 1434 | } |
| 1463 | 1435 | |
| 1464 | 1436 | { |
| 1465 | | var container = try sz.beginStruct(.{ .whitespace_style = .{ .fields = 3 } }); |
| 1437 | var container = try s.beginStruct(.{ .whitespace_style = .{ .fields = 3 } }); |
| 1466 | 1438 | try container.field("a", 1, .{}); |
| 1467 | 1439 | try container.field("b", 2, .{}); |
| 1468 | 1440 | try container.field("c", 3, .{}); |
| ... | ... | @@ -1474,15 +1446,15 @@ test "std.zon stringify whitespace, low level API" { |
| 1474 | 1446 | \\ .b = 2, |
| 1475 | 1447 | \\ .c = 3, |
| 1476 | 1448 | \\} |
| 1477 | | , buf.items); |
| 1449 | , aw.getWritten()); |
| 1478 | 1450 | } else { |
| 1479 | | try std.testing.expectEqualStrings(".{.a=1,.b=2,.c=3}", buf.items); |
| 1451 | try std.testing.expectEqualStrings(".{.a=1,.b=2,.c=3}", aw.getWritten()); |
| 1480 | 1452 | } |
| 1481 | | buf.clearRetainingCapacity(); |
| 1453 | aw.clearRetainingCapacity(); |
| 1482 | 1454 | } |
| 1483 | 1455 | |
| 1484 | 1456 | { |
| 1485 | | var container = try sz.beginTuple(.{ .whitespace_style = .{ .fields = 3 } }); |
| 1457 | var container = try s.beginTuple(.{ .whitespace_style = .{ .fields = 3 } }); |
| 1486 | 1458 | try container.field(1, .{}); |
| 1487 | 1459 | try container.field(2, .{}); |
| 1488 | 1460 | try container.field(3, .{}); |
| ... | ... | @@ -1494,16 +1466,16 @@ test "std.zon stringify whitespace, low level API" { |
| 1494 | 1466 | \\ 2, |
| 1495 | 1467 | \\ 3, |
| 1496 | 1468 | \\} |
| 1497 | | , buf.items); |
| 1469 | , aw.getWritten()); |
| 1498 | 1470 | } else { |
| 1499 | | try std.testing.expectEqualStrings(".{1,2,3}", buf.items); |
| 1471 | try std.testing.expectEqualStrings(".{1,2,3}", aw.getWritten()); |
| 1500 | 1472 | } |
| 1501 | | buf.clearRetainingCapacity(); |
| 1473 | aw.clearRetainingCapacity(); |
| 1502 | 1474 | } |
| 1503 | 1475 | |
| 1504 | 1476 | // Nested objects where the outer container doesn't wrap but the inner containers do |
| 1505 | 1477 | { |
| 1506 | | var container = try sz.beginStruct(.{ .whitespace_style = .{ .wrap = false } }); |
| 1478 | var container = try s.beginStruct(.{ .whitespace_style = .{ .wrap = false } }); |
| 1507 | 1479 | try container.field("first", .{ 1, 2, 3 }, .{}); |
| 1508 | 1480 | try container.field("second", .{ 4, 5, 6 }, .{}); |
| 1509 | 1481 | try container.end(); |
| ... | ... | @@ -1518,139 +1490,139 @@ test "std.zon stringify whitespace, low level API" { |
| 1518 | 1490 | \\ 5, |
| 1519 | 1491 | \\ 6, |
| 1520 | 1492 | \\} } |
| 1521 | | , buf.items); |
| 1493 | , aw.getWritten()); |
| 1522 | 1494 | } else { |
| 1523 | 1495 | try std.testing.expectEqualStrings( |
| 1524 | 1496 | ".{.first=.{1,2,3},.second=.{4,5,6}}", |
| 1525 | | buf.items, |
| 1497 | aw.getWritten(), |
| 1526 | 1498 | ); |
| 1527 | 1499 | } |
| 1528 | | buf.clearRetainingCapacity(); |
| 1500 | aw.clearRetainingCapacity(); |
| 1529 | 1501 | } |
| 1530 | 1502 | } |
| 1531 | 1503 | } |
| 1532 | 1504 | |
| 1533 | 1505 | test "std.zon stringify utf8 codepoints" { |
| 1534 | | var buf = std.ArrayList(u8).init(std.testing.allocator); |
| 1535 | | defer buf.deinit(); |
| 1536 | | var sz = serializer(buf.writer(), .{}); |
| 1506 | var aw: std.io.AllocatingWriter = undefined; |
| 1507 | defer aw.deinit(); |
| 1508 | var s: Serializer = .{ .writer = aw.init(std.testing.allocator) }; |
| 1537 | 1509 | |
| 1538 | 1510 | // Printable ASCII |
| 1539 | | try sz.int('a'); |
| 1540 | | try std.testing.expectEqualStrings("97", buf.items); |
| 1541 | | buf.clearRetainingCapacity(); |
| 1511 | try s.int('a'); |
| 1512 | try std.testing.expectEqualStrings("97", aw.getWritten()); |
| 1513 | aw.clearRetainingCapacity(); |
| 1542 | 1514 | |
| 1543 | | try sz.codePoint('a'); |
| 1544 | | try std.testing.expectEqualStrings("'a'", buf.items); |
| 1545 | | buf.clearRetainingCapacity(); |
| 1515 | try s.codePoint('a'); |
| 1516 | try std.testing.expectEqualStrings("'a'", aw.getWritten()); |
| 1517 | aw.clearRetainingCapacity(); |
| 1546 | 1518 | |
| 1547 | | try sz.value('a', .{ .emit_codepoint_literals = .always }); |
| 1548 | | try std.testing.expectEqualStrings("'a'", buf.items); |
| 1549 | | buf.clearRetainingCapacity(); |
| 1519 | try s.value('a', .{ .emit_codepoint_literals = .always }); |
| 1520 | try std.testing.expectEqualStrings("'a'", aw.getWritten()); |
| 1521 | aw.clearRetainingCapacity(); |
| 1550 | 1522 | |
| 1551 | | try sz.value('a', .{ .emit_codepoint_literals = .printable_ascii }); |
| 1552 | | try std.testing.expectEqualStrings("'a'", buf.items); |
| 1553 | | buf.clearRetainingCapacity(); |
| 1523 | try s.value('a', .{ .emit_codepoint_literals = .printable_ascii }); |
| 1524 | try std.testing.expectEqualStrings("'a'", aw.getWritten()); |
| 1525 | aw.clearRetainingCapacity(); |
| 1554 | 1526 | |
| 1555 | | try sz.value('a', .{ .emit_codepoint_literals = .never }); |
| 1556 | | try std.testing.expectEqualStrings("97", buf.items); |
| 1557 | | buf.clearRetainingCapacity(); |
| 1527 | try s.value('a', .{ .emit_codepoint_literals = .never }); |
| 1528 | try std.testing.expectEqualStrings("97", aw.getWritten()); |
| 1529 | aw.clearRetainingCapacity(); |
| 1558 | 1530 | |
| 1559 | 1531 | // Short escaped codepoint |
| 1560 | | try sz.int('\n'); |
| 1561 | | try std.testing.expectEqualStrings("10", buf.items); |
| 1562 | | buf.clearRetainingCapacity(); |
| 1532 | try s.int('\n'); |
| 1533 | try std.testing.expectEqualStrings("10", aw.getWritten()); |
| 1534 | aw.clearRetainingCapacity(); |
| 1563 | 1535 | |
| 1564 | | try sz.codePoint('\n'); |
| 1565 | | try std.testing.expectEqualStrings("'\\n'", buf.items); |
| 1566 | | buf.clearRetainingCapacity(); |
| 1536 | try s.codePoint('\n'); |
| 1537 | try std.testing.expectEqualStrings("'\\n'", aw.getWritten()); |
| 1538 | aw.clearRetainingCapacity(); |
| 1567 | 1539 | |
| 1568 | | try sz.value('\n', .{ .emit_codepoint_literals = .always }); |
| 1569 | | try std.testing.expectEqualStrings("'\\n'", buf.items); |
| 1570 | | buf.clearRetainingCapacity(); |
| 1540 | try s.value('\n', .{ .emit_codepoint_literals = .always }); |
| 1541 | try std.testing.expectEqualStrings("'\\n'", aw.getWritten()); |
| 1542 | aw.clearRetainingCapacity(); |
| 1571 | 1543 | |
| 1572 | | try sz.value('\n', .{ .emit_codepoint_literals = .printable_ascii }); |
| 1573 | | try std.testing.expectEqualStrings("10", buf.items); |
| 1574 | | buf.clearRetainingCapacity(); |
| 1544 | try s.value('\n', .{ .emit_codepoint_literals = .printable_ascii }); |
| 1545 | try std.testing.expectEqualStrings("10", aw.getWritten()); |
| 1546 | aw.clearRetainingCapacity(); |
| 1575 | 1547 | |
| 1576 | | try sz.value('\n', .{ .emit_codepoint_literals = .never }); |
| 1577 | | try std.testing.expectEqualStrings("10", buf.items); |
| 1578 | | buf.clearRetainingCapacity(); |
| 1548 | try s.value('\n', .{ .emit_codepoint_literals = .never }); |
| 1549 | try std.testing.expectEqualStrings("10", aw.getWritten()); |
| 1550 | aw.clearRetainingCapacity(); |
| 1579 | 1551 | |
| 1580 | 1552 | // Large codepoint |
| 1581 | | try sz.int('⚡'); |
| 1582 | | try std.testing.expectEqualStrings("9889", buf.items); |
| 1583 | | buf.clearRetainingCapacity(); |
| 1553 | try s.int('⚡'); |
| 1554 | try std.testing.expectEqualStrings("9889", aw.getWritten()); |
| 1555 | aw.clearRetainingCapacity(); |
| 1584 | 1556 | |
| 1585 | | try sz.codePoint('⚡'); |
| 1586 | | try std.testing.expectEqualStrings("'\\xe2\\x9a\\xa1'", buf.items); |
| 1587 | | buf.clearRetainingCapacity(); |
| 1557 | try s.codePoint('⚡'); |
| 1558 | try std.testing.expectEqualStrings("'\\xe2\\x9a\\xa1'", aw.getWritten()); |
| 1559 | aw.clearRetainingCapacity(); |
| 1588 | 1560 | |
| 1589 | | try sz.value('⚡', .{ .emit_codepoint_literals = .always }); |
| 1590 | | try std.testing.expectEqualStrings("'\\xe2\\x9a\\xa1'", buf.items); |
| 1591 | | buf.clearRetainingCapacity(); |
| 1561 | try s.value('⚡', .{ .emit_codepoint_literals = .always }); |
| 1562 | try std.testing.expectEqualStrings("'\\xe2\\x9a\\xa1'", aw.getWritten()); |
| 1563 | aw.clearRetainingCapacity(); |
| 1592 | 1564 | |
| 1593 | | try sz.value('⚡', .{ .emit_codepoint_literals = .printable_ascii }); |
| 1594 | | try std.testing.expectEqualStrings("9889", buf.items); |
| 1595 | | buf.clearRetainingCapacity(); |
| 1565 | try s.value('⚡', .{ .emit_codepoint_literals = .printable_ascii }); |
| 1566 | try std.testing.expectEqualStrings("9889", aw.getWritten()); |
| 1567 | aw.clearRetainingCapacity(); |
| 1596 | 1568 | |
| 1597 | | try sz.value('⚡', .{ .emit_codepoint_literals = .never }); |
| 1598 | | try std.testing.expectEqualStrings("9889", buf.items); |
| 1599 | | buf.clearRetainingCapacity(); |
| 1569 | try s.value('⚡', .{ .emit_codepoint_literals = .never }); |
| 1570 | try std.testing.expectEqualStrings("9889", aw.getWritten()); |
| 1571 | aw.clearRetainingCapacity(); |
| 1600 | 1572 | |
| 1601 | 1573 | // Invalid codepoint |
| 1602 | | try std.testing.expectError(error.InvalidCodepoint, sz.codePoint(0x110000 + 1)); |
| 1574 | try std.testing.expectError(error.InvalidCodepoint, s.codePoint(0x110000 + 1)); |
| 1603 | 1575 | |
| 1604 | | try sz.int(0x110000 + 1); |
| 1605 | | try std.testing.expectEqualStrings("1114113", buf.items); |
| 1606 | | buf.clearRetainingCapacity(); |
| 1576 | try s.int(0x110000 + 1); |
| 1577 | try std.testing.expectEqualStrings("1114113", aw.getWritten()); |
| 1578 | aw.clearRetainingCapacity(); |
| 1607 | 1579 | |
| 1608 | | try sz.value(0x110000 + 1, .{ .emit_codepoint_literals = .always }); |
| 1609 | | try std.testing.expectEqualStrings("1114113", buf.items); |
| 1610 | | buf.clearRetainingCapacity(); |
| 1580 | try s.value(0x110000 + 1, .{ .emit_codepoint_literals = .always }); |
| 1581 | try std.testing.expectEqualStrings("1114113", aw.getWritten()); |
| 1582 | aw.clearRetainingCapacity(); |
| 1611 | 1583 | |
| 1612 | | try sz.value(0x110000 + 1, .{ .emit_codepoint_literals = .printable_ascii }); |
| 1613 | | try std.testing.expectEqualStrings("1114113", buf.items); |
| 1614 | | buf.clearRetainingCapacity(); |
| 1584 | try s.value(0x110000 + 1, .{ .emit_codepoint_literals = .printable_ascii }); |
| 1585 | try std.testing.expectEqualStrings("1114113", aw.getWritten()); |
| 1586 | aw.clearRetainingCapacity(); |
| 1615 | 1587 | |
| 1616 | | try sz.value(0x110000 + 1, .{ .emit_codepoint_literals = .never }); |
| 1617 | | try std.testing.expectEqualStrings("1114113", buf.items); |
| 1618 | | buf.clearRetainingCapacity(); |
| 1588 | try s.value(0x110000 + 1, .{ .emit_codepoint_literals = .never }); |
| 1589 | try std.testing.expectEqualStrings("1114113", aw.getWritten()); |
| 1590 | aw.clearRetainingCapacity(); |
| 1619 | 1591 | |
| 1620 | 1592 | // Valid codepoint, not a codepoint type |
| 1621 | | try sz.value(@as(u22, 'a'), .{ .emit_codepoint_literals = .always }); |
| 1622 | | try std.testing.expectEqualStrings("97", buf.items); |
| 1623 | | buf.clearRetainingCapacity(); |
| 1593 | try s.value(@as(u22, 'a'), .{ .emit_codepoint_literals = .always }); |
| 1594 | try std.testing.expectEqualStrings("97", aw.getWritten()); |
| 1595 | aw.clearRetainingCapacity(); |
| 1624 | 1596 | |
| 1625 | | try sz.value(@as(u22, 'a'), .{ .emit_codepoint_literals = .printable_ascii }); |
| 1626 | | try std.testing.expectEqualStrings("97", buf.items); |
| 1627 | | buf.clearRetainingCapacity(); |
| 1597 | try s.value(@as(u22, 'a'), .{ .emit_codepoint_literals = .printable_ascii }); |
| 1598 | try std.testing.expectEqualStrings("97", aw.getWritten()); |
| 1599 | aw.clearRetainingCapacity(); |
| 1628 | 1600 | |
| 1629 | | try sz.value(@as(i32, 'a'), .{ .emit_codepoint_literals = .never }); |
| 1630 | | try std.testing.expectEqualStrings("97", buf.items); |
| 1631 | | buf.clearRetainingCapacity(); |
| 1601 | try s.value(@as(i32, 'a'), .{ .emit_codepoint_literals = .never }); |
| 1602 | try std.testing.expectEqualStrings("97", aw.getWritten()); |
| 1603 | aw.clearRetainingCapacity(); |
| 1632 | 1604 | |
| 1633 | 1605 | // Make sure value options are passed to children |
| 1634 | | try sz.value(.{ .c = '⚡' }, .{ .emit_codepoint_literals = .always }); |
| 1635 | | try std.testing.expectEqualStrings(".{ .c = '\\xe2\\x9a\\xa1' }", buf.items); |
| 1636 | | buf.clearRetainingCapacity(); |
| 1606 | try s.value(.{ .c = '⚡' }, .{ .emit_codepoint_literals = .always }); |
| 1607 | try std.testing.expectEqualStrings(".{ .c = '\\xe2\\x9a\\xa1' }", aw.getWritten()); |
| 1608 | aw.clearRetainingCapacity(); |
| 1637 | 1609 | |
| 1638 | | try sz.value(.{ .c = '⚡' }, .{ .emit_codepoint_literals = .never }); |
| 1639 | | try std.testing.expectEqualStrings(".{ .c = 9889 }", buf.items); |
| 1640 | | buf.clearRetainingCapacity(); |
| 1610 | try s.value(.{ .c = '⚡' }, .{ .emit_codepoint_literals = .never }); |
| 1611 | try std.testing.expectEqualStrings(".{ .c = 9889 }", aw.getWritten()); |
| 1612 | aw.clearRetainingCapacity(); |
| 1641 | 1613 | } |
| 1642 | 1614 | |
| 1643 | 1615 | test "std.zon stringify strings" { |
| 1644 | | var buf = std.ArrayList(u8).init(std.testing.allocator); |
| 1645 | | defer buf.deinit(); |
| 1646 | | var sz = serializer(buf.writer(), .{}); |
| 1616 | var aw: std.io.AllocatingWriter = undefined; |
| 1617 | defer aw.deinit(); |
| 1618 | var s: Serializer = .{ .writer = aw.init(std.testing.allocator) }; |
| 1647 | 1619 | |
| 1648 | 1620 | // Minimal case |
| 1649 | | try sz.string("abc⚡\n"); |
| 1650 | | try std.testing.expectEqualStrings("\"abc\\xe2\\x9a\\xa1\\n\"", buf.items); |
| 1651 | | buf.clearRetainingCapacity(); |
| 1621 | try s.string("abc⚡\n"); |
| 1622 | try std.testing.expectEqualStrings("\"abc\\xe2\\x9a\\xa1\\n\"", aw.getWritten()); |
| 1623 | aw.clearRetainingCapacity(); |
| 1652 | 1624 | |
| 1653 | | try sz.tuple("abc⚡\n", .{}); |
| 1625 | try s.tuple("abc⚡\n", .{}); |
| 1654 | 1626 | try std.testing.expectEqualStrings( |
| 1655 | 1627 | \\.{ |
| 1656 | 1628 | \\ 97, |
| ... | ... | @@ -1661,14 +1633,14 @@ test "std.zon stringify strings" { |
| 1661 | 1633 | \\ 161, |
| 1662 | 1634 | \\ 10, |
| 1663 | 1635 | \\} |
| 1664 | | , buf.items); |
| 1665 | | buf.clearRetainingCapacity(); |
| 1636 | , aw.getWritten()); |
| 1637 | aw.clearRetainingCapacity(); |
| 1666 | 1638 | |
| 1667 | | try sz.value("abc⚡\n", .{}); |
| 1668 | | try std.testing.expectEqualStrings("\"abc\\xe2\\x9a\\xa1\\n\"", buf.items); |
| 1669 | | buf.clearRetainingCapacity(); |
| 1639 | try s.value("abc⚡\n", .{}); |
| 1640 | try std.testing.expectEqualStrings("\"abc\\xe2\\x9a\\xa1\\n\"", aw.getWritten()); |
| 1641 | aw.clearRetainingCapacity(); |
| 1670 | 1642 | |
| 1671 | | try sz.value("abc⚡\n", .{ .emit_strings_as_containers = true }); |
| 1643 | try s.value("abc⚡\n", .{ .emit_strings_as_containers = true }); |
| 1672 | 1644 | try std.testing.expectEqualStrings( |
| 1673 | 1645 | \\.{ |
| 1674 | 1646 | \\ 97, |
| ... | ... | @@ -1679,113 +1651,113 @@ test "std.zon stringify strings" { |
| 1679 | 1651 | \\ 161, |
| 1680 | 1652 | \\ 10, |
| 1681 | 1653 | \\} |
| 1682 | | , buf.items); |
| 1683 | | buf.clearRetainingCapacity(); |
| 1654 | , aw.getWritten()); |
| 1655 | aw.clearRetainingCapacity(); |
| 1684 | 1656 | |
| 1685 | 1657 | // Value options are inherited by children |
| 1686 | | try sz.value(.{ .str = "abc" }, .{}); |
| 1687 | | try std.testing.expectEqualStrings(".{ .str = \"abc\" }", buf.items); |
| 1688 | | buf.clearRetainingCapacity(); |
| 1658 | try s.value(.{ .str = "abc" }, .{}); |
| 1659 | try std.testing.expectEqualStrings(".{ .str = \"abc\" }", aw.getWritten()); |
| 1660 | aw.clearRetainingCapacity(); |
| 1689 | 1661 | |
| 1690 | | try sz.value(.{ .str = "abc" }, .{ .emit_strings_as_containers = true }); |
| 1662 | try s.value(.{ .str = "abc" }, .{ .emit_strings_as_containers = true }); |
| 1691 | 1663 | try std.testing.expectEqualStrings( |
| 1692 | 1664 | \\.{ .str = .{ |
| 1693 | 1665 | \\ 97, |
| 1694 | 1666 | \\ 98, |
| 1695 | 1667 | \\ 99, |
| 1696 | 1668 | \\} } |
| 1697 | | , buf.items); |
| 1698 | | buf.clearRetainingCapacity(); |
| 1669 | , aw.getWritten()); |
| 1670 | aw.clearRetainingCapacity(); |
| 1699 | 1671 | |
| 1700 | 1672 | // Arrays (rather than pointers to arrays) of u8s are not considered strings, so that data can |
| 1701 | 1673 | // round trip correctly. |
| 1702 | | try sz.value("abc".*, .{}); |
| 1674 | try s.value("abc".*, .{}); |
| 1703 | 1675 | try std.testing.expectEqualStrings( |
| 1704 | 1676 | \\.{ |
| 1705 | 1677 | \\ 97, |
| 1706 | 1678 | \\ 98, |
| 1707 | 1679 | \\ 99, |
| 1708 | 1680 | \\} |
| 1709 | | , buf.items); |
| 1710 | | buf.clearRetainingCapacity(); |
| 1681 | , aw.getWritten()); |
| 1682 | aw.clearRetainingCapacity(); |
| 1711 | 1683 | } |
| 1712 | 1684 | |
| 1713 | 1685 | test "std.zon stringify multiline strings" { |
| 1714 | | var buf = std.ArrayList(u8).init(std.testing.allocator); |
| 1715 | | defer buf.deinit(); |
| 1716 | | var sz = serializer(buf.writer(), .{}); |
| 1686 | var aw: std.io.AllocatingWriter = undefined; |
| 1687 | defer aw.deinit(); |
| 1688 | var s: Serializer = .{ .writer = aw.init(std.testing.allocator) }; |
| 1717 | 1689 | |
| 1718 | 1690 | inline for (.{ true, false }) |whitespace| { |
| 1719 | | sz.options.whitespace = whitespace; |
| 1691 | s.options.whitespace = whitespace; |
| 1720 | 1692 | |
| 1721 | 1693 | { |
| 1722 | | try sz.multilineString("", .{ .top_level = true }); |
| 1723 | | try std.testing.expectEqualStrings("\\\\", buf.items); |
| 1724 | | buf.clearRetainingCapacity(); |
| 1694 | try s.multilineString("", .{ .top_level = true }); |
| 1695 | try std.testing.expectEqualStrings("\\\\", aw.getWritten()); |
| 1696 | aw.clearRetainingCapacity(); |
| 1725 | 1697 | } |
| 1726 | 1698 | |
| 1727 | 1699 | { |
| 1728 | | try sz.multilineString("abc⚡", .{ .top_level = true }); |
| 1729 | | try std.testing.expectEqualStrings("\\\\abc⚡", buf.items); |
| 1730 | | buf.clearRetainingCapacity(); |
| 1700 | try s.multilineString("abc⚡", .{ .top_level = true }); |
| 1701 | try std.testing.expectEqualStrings("\\\\abc⚡", aw.getWritten()); |
| 1702 | aw.clearRetainingCapacity(); |
| 1731 | 1703 | } |
| 1732 | 1704 | |
| 1733 | 1705 | { |
| 1734 | | try sz.multilineString("abc⚡\ndef", .{ .top_level = true }); |
| 1735 | | try std.testing.expectEqualStrings("\\\\abc⚡\n\\\\def", buf.items); |
| 1736 | | buf.clearRetainingCapacity(); |
| 1706 | try s.multilineString("abc⚡\ndef", .{ .top_level = true }); |
| 1707 | try std.testing.expectEqualStrings("\\\\abc⚡\n\\\\def", aw.getWritten()); |
| 1708 | aw.clearRetainingCapacity(); |
| 1737 | 1709 | } |
| 1738 | 1710 | |
| 1739 | 1711 | { |
| 1740 | | try sz.multilineString("abc⚡\r\ndef", .{ .top_level = true }); |
| 1741 | | try std.testing.expectEqualStrings("\\\\abc⚡\n\\\\def", buf.items); |
| 1742 | | buf.clearRetainingCapacity(); |
| 1712 | try s.multilineString("abc⚡\r\ndef", .{ .top_level = true }); |
| 1713 | try std.testing.expectEqualStrings("\\\\abc⚡\n\\\\def", aw.getWritten()); |
| 1714 | aw.clearRetainingCapacity(); |
| 1743 | 1715 | } |
| 1744 | 1716 | |
| 1745 | 1717 | { |
| 1746 | | try sz.multilineString("\nabc⚡", .{ .top_level = true }); |
| 1747 | | try std.testing.expectEqualStrings("\\\\\n\\\\abc⚡", buf.items); |
| 1748 | | buf.clearRetainingCapacity(); |
| 1718 | try s.multilineString("\nabc⚡", .{ .top_level = true }); |
| 1719 | try std.testing.expectEqualStrings("\\\\\n\\\\abc⚡", aw.getWritten()); |
| 1720 | aw.clearRetainingCapacity(); |
| 1749 | 1721 | } |
| 1750 | 1722 | |
| 1751 | 1723 | { |
| 1752 | | try sz.multilineString("\r\nabc⚡", .{ .top_level = true }); |
| 1753 | | try std.testing.expectEqualStrings("\\\\\n\\\\abc⚡", buf.items); |
| 1754 | | buf.clearRetainingCapacity(); |
| 1724 | try s.multilineString("\r\nabc⚡", .{ .top_level = true }); |
| 1725 | try std.testing.expectEqualStrings("\\\\\n\\\\abc⚡", aw.getWritten()); |
| 1726 | aw.clearRetainingCapacity(); |
| 1755 | 1727 | } |
| 1756 | 1728 | |
| 1757 | 1729 | { |
| 1758 | | try sz.multilineString("abc\ndef", .{}); |
| 1730 | try s.multilineString("abc\ndef", .{}); |
| 1759 | 1731 | if (whitespace) { |
| 1760 | | try std.testing.expectEqualStrings("\n\\\\abc\n\\\\def\n", buf.items); |
| 1732 | try std.testing.expectEqualStrings("\n\\\\abc\n\\\\def\n", aw.getWritten()); |
| 1761 | 1733 | } else { |
| 1762 | | try std.testing.expectEqualStrings("\\\\abc\n\\\\def\n", buf.items); |
| 1734 | try std.testing.expectEqualStrings("\\\\abc\n\\\\def\n", aw.getWritten()); |
| 1763 | 1735 | } |
| 1764 | | buf.clearRetainingCapacity(); |
| 1736 | aw.clearRetainingCapacity(); |
| 1765 | 1737 | } |
| 1766 | 1738 | |
| 1767 | 1739 | { |
| 1768 | 1740 | const str: []const u8 = &.{ 'a', '\r', 'c' }; |
| 1769 | | try sz.string(str); |
| 1770 | | try std.testing.expectEqualStrings("\"a\\rc\"", buf.items); |
| 1771 | | buf.clearRetainingCapacity(); |
| 1741 | try s.string(str); |
| 1742 | try std.testing.expectEqualStrings("\"a\\rc\"", aw.getWritten()); |
| 1743 | aw.clearRetainingCapacity(); |
| 1772 | 1744 | } |
| 1773 | 1745 | |
| 1774 | 1746 | { |
| 1775 | 1747 | try std.testing.expectError( |
| 1776 | 1748 | error.InnerCarriageReturn, |
| 1777 | | sz.multilineString(@as([]const u8, &.{ 'a', '\r', 'c' }), .{}), |
| 1749 | s.multilineString(@as([]const u8, &.{ 'a', '\r', 'c' }), .{}), |
| 1778 | 1750 | ); |
| 1779 | 1751 | try std.testing.expectError( |
| 1780 | 1752 | error.InnerCarriageReturn, |
| 1781 | | sz.multilineString(@as([]const u8, &.{ 'a', '\r', 'c', '\n' }), .{}), |
| 1753 | s.multilineString(@as([]const u8, &.{ 'a', '\r', 'c', '\n' }), .{}), |
| 1782 | 1754 | ); |
| 1783 | 1755 | try std.testing.expectError( |
| 1784 | 1756 | error.InnerCarriageReturn, |
| 1785 | | sz.multilineString(@as([]const u8, &.{ 'a', '\r', 'c', '\r', '\n' }), .{}), |
| 1757 | s.multilineString(@as([]const u8, &.{ 'a', '\r', 'c', '\r', '\n' }), .{}), |
| 1786 | 1758 | ); |
| 1787 | | try std.testing.expectEqualStrings("", buf.items); |
| 1788 | | buf.clearRetainingCapacity(); |
| 1759 | try std.testing.expectEqualStrings("", aw.getWritten()); |
| 1760 | aw.clearRetainingCapacity(); |
| 1789 | 1761 | } |
| 1790 | 1762 | } |
| 1791 | 1763 | } |
| ... | ... | @@ -1931,42 +1903,43 @@ test "std.zon stringify skip default fields" { |
| 1931 | 1903 | } |
| 1932 | 1904 | |
| 1933 | 1905 | test "std.zon depth limits" { |
| 1934 | | var buf = std.ArrayList(u8).init(std.testing.allocator); |
| 1935 | | defer buf.deinit(); |
| 1906 | var aw: std.io.AllocatingWriter = undefined; |
| 1907 | defer aw.deinit(); |
| 1908 | const bw = aw.init(std.testing.allocator); |
| 1936 | 1909 | |
| 1937 | 1910 | const Recurse = struct { r: []const @This() }; |
| 1938 | 1911 | |
| 1939 | 1912 | // Normal operation |
| 1940 | | try serializeMaxDepth(.{ 1, .{ 2, 3 } }, .{}, buf.writer(), 16); |
| 1941 | | try std.testing.expectEqualStrings(".{ 1, .{ 2, 3 } }", buf.items); |
| 1942 | | buf.clearRetainingCapacity(); |
| 1913 | try serializeMaxDepth(.{ 1, .{ 2, 3 } }, .{}, bw, 16); |
| 1914 | try std.testing.expectEqualStrings(".{ 1, .{ 2, 3 } }", aw.getWritten()); |
| 1915 | aw.clearRetainingCapacity(); |
| 1943 | 1916 | |
| 1944 | | try serializeArbitraryDepth(.{ 1, .{ 2, 3 } }, .{}, buf.writer()); |
| 1945 | | try std.testing.expectEqualStrings(".{ 1, .{ 2, 3 } }", buf.items); |
| 1946 | | buf.clearRetainingCapacity(); |
| 1917 | try serializeArbitraryDepth(.{ 1, .{ 2, 3 } }, .{}, bw); |
| 1918 | try std.testing.expectEqualStrings(".{ 1, .{ 2, 3 } }", aw.getWritten()); |
| 1919 | aw.clearRetainingCapacity(); |
| 1947 | 1920 | |
| 1948 | 1921 | // Max depth failing on non recursive type |
| 1949 | 1922 | try std.testing.expectError( |
| 1950 | 1923 | error.ExceededMaxDepth, |
| 1951 | | serializeMaxDepth(.{ 1, .{ 2, .{ 3, 4 } } }, .{}, buf.writer(), 3), |
| 1924 | serializeMaxDepth(.{ 1, .{ 2, .{ 3, 4 } } }, .{}, bw, 3), |
| 1952 | 1925 | ); |
| 1953 | | try std.testing.expectEqualStrings("", buf.items); |
| 1954 | | buf.clearRetainingCapacity(); |
| 1926 | try std.testing.expectEqualStrings("", aw.getWritten()); |
| 1927 | aw.clearRetainingCapacity(); |
| 1955 | 1928 | |
| 1956 | 1929 | // Max depth passing on recursive type |
| 1957 | 1930 | { |
| 1958 | 1931 | const maybe_recurse = Recurse{ .r = &.{} }; |
| 1959 | | try serializeMaxDepth(maybe_recurse, .{}, buf.writer(), 2); |
| 1960 | | try std.testing.expectEqualStrings(".{ .r = .{} }", buf.items); |
| 1961 | | buf.clearRetainingCapacity(); |
| 1932 | try serializeMaxDepth(maybe_recurse, .{}, bw, 2); |
| 1933 | try std.testing.expectEqualStrings(".{ .r = .{} }", aw.getWritten()); |
| 1934 | aw.clearRetainingCapacity(); |
| 1962 | 1935 | } |
| 1963 | 1936 | |
| 1964 | 1937 | // Unchecked passing on recursive type |
| 1965 | 1938 | { |
| 1966 | 1939 | const maybe_recurse = Recurse{ .r = &.{} }; |
| 1967 | | try serializeArbitraryDepth(maybe_recurse, .{}, buf.writer()); |
| 1968 | | try std.testing.expectEqualStrings(".{ .r = .{} }", buf.items); |
| 1969 | | buf.clearRetainingCapacity(); |
| 1940 | try serializeArbitraryDepth(maybe_recurse, .{}, bw); |
| 1941 | try std.testing.expectEqualStrings(".{ .r = .{} }", aw.getWritten()); |
| 1942 | aw.clearRetainingCapacity(); |
| 1970 | 1943 | } |
| 1971 | 1944 | |
| 1972 | 1945 | // Max depth failing on recursive type due to depth |
| ... | ... | @@ -1975,10 +1948,10 @@ test "std.zon depth limits" { |
| 1975 | 1948 | maybe_recurse.r = &.{.{ .r = &.{} }}; |
| 1976 | 1949 | try std.testing.expectError( |
| 1977 | 1950 | error.ExceededMaxDepth, |
| 1978 | | serializeMaxDepth(maybe_recurse, .{}, buf.writer(), 2), |
| 1951 | serializeMaxDepth(maybe_recurse, .{}, bw, 2), |
| 1979 | 1952 | ); |
| 1980 | | try std.testing.expectEqualStrings("", buf.items); |
| 1981 | | buf.clearRetainingCapacity(); |
| 1953 | try std.testing.expectEqualStrings("", aw.getWritten()); |
| 1954 | aw.clearRetainingCapacity(); |
| 1982 | 1955 | } |
| 1983 | 1956 | |
| 1984 | 1957 | // Same but for a slice |
| ... | ... | @@ -1988,23 +1961,23 @@ test "std.zon depth limits" { |
| 1988 | 1961 | |
| 1989 | 1962 | try std.testing.expectError( |
| 1990 | 1963 | error.ExceededMaxDepth, |
| 1991 | | serializeMaxDepth(maybe_recurse, .{}, buf.writer(), 2), |
| 1964 | serializeMaxDepth(maybe_recurse, .{}, bw, 2), |
| 1992 | 1965 | ); |
| 1993 | | try std.testing.expectEqualStrings("", buf.items); |
| 1994 | | buf.clearRetainingCapacity(); |
| 1966 | try std.testing.expectEqualStrings("", aw.getWritten()); |
| 1967 | aw.clearRetainingCapacity(); |
| 1995 | 1968 | |
| 1996 | | var sz = serializer(buf.writer(), .{}); |
| 1969 | var s: Serializer = .{ .writer = bw }; |
| 1997 | 1970 | |
| 1998 | 1971 | try std.testing.expectError( |
| 1999 | 1972 | error.ExceededMaxDepth, |
| 2000 | | sz.tupleMaxDepth(maybe_recurse, .{}, 2), |
| 1973 | s.tupleMaxDepth(maybe_recurse, .{}, 2), |
| 2001 | 1974 | ); |
| 2002 | | try std.testing.expectEqualStrings("", buf.items); |
| 2003 | | buf.clearRetainingCapacity(); |
| 1975 | try std.testing.expectEqualStrings("", aw.getWritten()); |
| 1976 | aw.clearRetainingCapacity(); |
| 2004 | 1977 | |
| 2005 | | try sz.tupleArbitraryDepth(maybe_recurse, .{}); |
| 2006 | | try std.testing.expectEqualStrings(".{.{ .r = .{} }}", buf.items); |
| 2007 | | buf.clearRetainingCapacity(); |
| 1978 | try s.tupleArbitraryDepth(maybe_recurse, .{}); |
| 1979 | try std.testing.expectEqualStrings(".{.{ .r = .{} }}", aw.getWritten()); |
| 1980 | aw.clearRetainingCapacity(); |
| 2008 | 1981 | } |
| 2009 | 1982 | |
| 2010 | 1983 | // A slice succeeding |
| ... | ... | @@ -2012,19 +1985,19 @@ test "std.zon depth limits" { |
| 2012 | 1985 | var temp: [1]Recurse = .{.{ .r = &.{} }}; |
| 2013 | 1986 | const maybe_recurse: []const Recurse = &temp; |
| 2014 | 1987 | |
| 2015 | | try serializeMaxDepth(maybe_recurse, .{}, buf.writer(), 3); |
| 2016 | | try std.testing.expectEqualStrings(".{.{ .r = .{} }}", buf.items); |
| 2017 | | buf.clearRetainingCapacity(); |
| 1988 | try serializeMaxDepth(maybe_recurse, .{}, bw, 3); |
| 1989 | try std.testing.expectEqualStrings(".{.{ .r = .{} }}", aw.getWritten()); |
| 1990 | aw.clearRetainingCapacity(); |
| 2018 | 1991 | |
| 2019 | | var sz = serializer(buf.writer(), .{}); |
| 1992 | var s: Serializer = .{ .writer = bw }; |
| 2020 | 1993 | |
| 2021 | | try sz.tupleMaxDepth(maybe_recurse, .{}, 3); |
| 2022 | | try std.testing.expectEqualStrings(".{.{ .r = .{} }}", buf.items); |
| 2023 | | buf.clearRetainingCapacity(); |
| 1994 | try s.tupleMaxDepth(maybe_recurse, .{}, 3); |
| 1995 | try std.testing.expectEqualStrings(".{.{ .r = .{} }}", aw.getWritten()); |
| 1996 | aw.clearRetainingCapacity(); |
| 2024 | 1997 | |
| 2025 | | try sz.tupleArbitraryDepth(maybe_recurse, .{}); |
| 2026 | | try std.testing.expectEqualStrings(".{.{ .r = .{} }}", buf.items); |
| 2027 | | buf.clearRetainingCapacity(); |
| 1998 | try s.tupleArbitraryDepth(maybe_recurse, .{}); |
| 1999 | try std.testing.expectEqualStrings(".{.{ .r = .{} }}", aw.getWritten()); |
| 2000 | aw.clearRetainingCapacity(); |
| 2028 | 2001 | } |
| 2029 | 2002 | |
| 2030 | 2003 | // Max depth failing on recursive type due to recursion |
| ... | ... | @@ -2035,46 +2008,46 @@ test "std.zon depth limits" { |
| 2035 | 2008 | |
| 2036 | 2009 | try std.testing.expectError( |
| 2037 | 2010 | error.ExceededMaxDepth, |
| 2038 | | serializeMaxDepth(maybe_recurse, .{}, buf.writer(), 128), |
| 2011 | serializeMaxDepth(maybe_recurse, .{}, bw, 128), |
| 2039 | 2012 | ); |
| 2040 | | try std.testing.expectEqualStrings("", buf.items); |
| 2041 | | buf.clearRetainingCapacity(); |
| 2013 | try std.testing.expectEqualStrings("", aw.getWritten()); |
| 2014 | aw.clearRetainingCapacity(); |
| 2042 | 2015 | |
| 2043 | | var sz = serializer(buf.writer(), .{}); |
| 2016 | var s: Serializer = .{ .writer = bw }; |
| 2044 | 2017 | try std.testing.expectError( |
| 2045 | 2018 | error.ExceededMaxDepth, |
| 2046 | | sz.tupleMaxDepth(maybe_recurse, .{}, 128), |
| 2019 | s.tupleMaxDepth(maybe_recurse, .{}, 128), |
| 2047 | 2020 | ); |
| 2048 | | try std.testing.expectEqualStrings("", buf.items); |
| 2049 | | buf.clearRetainingCapacity(); |
| 2021 | try std.testing.expectEqualStrings("", aw.getWritten()); |
| 2022 | aw.clearRetainingCapacity(); |
| 2050 | 2023 | } |
| 2051 | 2024 | |
| 2052 | 2025 | // Max depth on other parts of the lower level API |
| 2053 | 2026 | { |
| 2054 | | var sz = serializer(buf.writer(), .{}); |
| 2027 | var s: Serializer = .{ .writer = bw }; |
| 2055 | 2028 | |
| 2056 | 2029 | const maybe_recurse: []const Recurse = &.{}; |
| 2057 | 2030 | |
| 2058 | | try std.testing.expectError(error.ExceededMaxDepth, sz.valueMaxDepth(1, .{}, 0)); |
| 2059 | | try sz.valueMaxDepth(2, .{}, 1); |
| 2060 | | try sz.value(3, .{}); |
| 2061 | | try sz.valueArbitraryDepth(maybe_recurse, .{}); |
| 2031 | try std.testing.expectError(error.ExceededMaxDepth, s.valueMaxDepth(1, .{}, 0)); |
| 2032 | try s.valueMaxDepth(2, .{}, 1); |
| 2033 | try s.value(3, .{}); |
| 2034 | try s.valueArbitraryDepth(maybe_recurse, .{}); |
| 2062 | 2035 | |
| 2063 | | var s = try sz.beginStruct(.{}); |
| 2064 | | try std.testing.expectError(error.ExceededMaxDepth, s.fieldMaxDepth("a", 1, .{}, 0)); |
| 2065 | | try s.fieldMaxDepth("b", 4, .{}, 1); |
| 2066 | | try s.field("c", 5, .{}); |
| 2067 | | try s.fieldArbitraryDepth("d", maybe_recurse, .{}); |
| 2068 | | try s.end(); |
| 2036 | var wip_struct = try s.beginStruct(.{}); |
| 2037 | try std.testing.expectError(error.ExceededMaxDepth, wip_struct.fieldMaxDepth("a", 1, .{}, 0)); |
| 2038 | try wip_struct.fieldMaxDepth("b", 4, .{}, 1); |
| 2039 | try wip_struct.field("c", 5, .{}); |
| 2040 | try wip_struct.fieldArbitraryDepth("d", maybe_recurse, .{}); |
| 2041 | try wip_struct.end(); |
| 2069 | 2042 | |
| 2070 | | var t = try sz.beginTuple(.{}); |
| 2043 | var t = try s.beginTuple(.{}); |
| 2071 | 2044 | try std.testing.expectError(error.ExceededMaxDepth, t.fieldMaxDepth(1, .{}, 0)); |
| 2072 | 2045 | try t.fieldMaxDepth(6, .{}, 1); |
| 2073 | 2046 | try t.field(7, .{}); |
| 2074 | 2047 | try t.fieldArbitraryDepth(maybe_recurse, .{}); |
| 2075 | 2048 | try t.end(); |
| 2076 | 2049 | |
| 2077 | | var a = try sz.beginTuple(.{}); |
| 2050 | var a = try s.beginTuple(.{}); |
| 2078 | 2051 | try std.testing.expectError(error.ExceededMaxDepth, a.fieldMaxDepth(1, .{}, 0)); |
| 2079 | 2052 | try a.fieldMaxDepth(8, .{}, 1); |
| 2080 | 2053 | try a.field(9, .{}); |
| ... | ... | @@ -2095,7 +2068,7 @@ test "std.zon depth limits" { |
| 2095 | 2068 | \\ 9, |
| 2096 | 2069 | \\ .{}, |
| 2097 | 2070 | \\} |
| 2098 | | , buf.items); |
| 2071 | , aw.getWritten()); |
| 2099 | 2072 | } |
| 2100 | 2073 | } |
| 2101 | 2074 | |
| ... | ... | @@ -2191,42 +2164,42 @@ test "std.zon stringify primitives" { |
| 2191 | 2164 | } |
| 2192 | 2165 | |
| 2193 | 2166 | test "std.zon stringify ident" { |
| 2194 | | var buf = std.ArrayList(u8).init(std.testing.allocator); |
| 2195 | | defer buf.deinit(); |
| 2196 | | var sz = serializer(buf.writer(), .{}); |
| 2167 | var aw: std.io.AllocatingWriter = undefined; |
| 2168 | defer aw.deinit(); |
| 2169 | var s: Serializer = .{ .writer = aw.init(std.testing.allocator) }; |
| 2197 | 2170 | |
| 2198 | 2171 | try expectSerializeEqual(".{ .a = 0 }", .{ .a = 0 }, .{}); |
| 2199 | | try sz.ident("a"); |
| 2200 | | try std.testing.expectEqualStrings(".a", buf.items); |
| 2201 | | buf.clearRetainingCapacity(); |
| 2172 | try s.ident("a"); |
| 2173 | try std.testing.expectEqualStrings(".a", aw.getWritten()); |
| 2174 | aw.clearRetainingCapacity(); |
| 2202 | 2175 | |
| 2203 | | try sz.ident("foo_1"); |
| 2204 | | try std.testing.expectEqualStrings(".foo_1", buf.items); |
| 2205 | | buf.clearRetainingCapacity(); |
| 2176 | try s.ident("foo_1"); |
| 2177 | try std.testing.expectEqualStrings(".foo_1", aw.getWritten()); |
| 2178 | aw.clearRetainingCapacity(); |
| 2206 | 2179 | |
| 2207 | | try sz.ident("_foo_1"); |
| 2208 | | try std.testing.expectEqualStrings("._foo_1", buf.items); |
| 2209 | | buf.clearRetainingCapacity(); |
| 2180 | try s.ident("_foo_1"); |
| 2181 | try std.testing.expectEqualStrings("._foo_1", aw.getWritten()); |
| 2182 | aw.clearRetainingCapacity(); |
| 2210 | 2183 | |
| 2211 | | try sz.ident("foo bar"); |
| 2212 | | try std.testing.expectEqualStrings(".@\"foo bar\"", buf.items); |
| 2213 | | buf.clearRetainingCapacity(); |
| 2184 | try s.ident("foo bar"); |
| 2185 | try std.testing.expectEqualStrings(".@\"foo bar\"", aw.getWritten()); |
| 2186 | aw.clearRetainingCapacity(); |
| 2214 | 2187 | |
| 2215 | | try sz.ident("1foo"); |
| 2216 | | try std.testing.expectEqualStrings(".@\"1foo\"", buf.items); |
| 2217 | | buf.clearRetainingCapacity(); |
| 2188 | try s.ident("1foo"); |
| 2189 | try std.testing.expectEqualStrings(".@\"1foo\"", aw.getWritten()); |
| 2190 | aw.clearRetainingCapacity(); |
| 2218 | 2191 | |
| 2219 | | try sz.ident("var"); |
| 2220 | | try std.testing.expectEqualStrings(".@\"var\"", buf.items); |
| 2221 | | buf.clearRetainingCapacity(); |
| 2192 | try s.ident("var"); |
| 2193 | try std.testing.expectEqualStrings(".@\"var\"", aw.getWritten()); |
| 2194 | aw.clearRetainingCapacity(); |
| 2222 | 2195 | |
| 2223 | | try sz.ident("true"); |
| 2224 | | try std.testing.expectEqualStrings(".true", buf.items); |
| 2225 | | buf.clearRetainingCapacity(); |
| 2196 | try s.ident("true"); |
| 2197 | try std.testing.expectEqualStrings(".true", aw.getWritten()); |
| 2198 | aw.clearRetainingCapacity(); |
| 2226 | 2199 | |
| 2227 | | try sz.ident("_"); |
| 2228 | | try std.testing.expectEqualStrings("._", buf.items); |
| 2229 | | buf.clearRetainingCapacity(); |
| 2200 | try s.ident("_"); |
| 2201 | try std.testing.expectEqualStrings("._", aw.getWritten()); |
| 2202 | aw.clearRetainingCapacity(); |
| 2230 | 2203 | |
| 2231 | 2204 | const Enum = enum { |
| 2232 | 2205 | @"foo bar", |
| ... | ... | @@ -2238,40 +2211,40 @@ test "std.zon stringify ident" { |
| 2238 | 2211 | } |
| 2239 | 2212 | |
| 2240 | 2213 | test "std.zon stringify as tuple" { |
| 2241 | | var buf = std.ArrayList(u8).init(std.testing.allocator); |
| 2242 | | defer buf.deinit(); |
| 2243 | | var sz = serializer(buf.writer(), .{}); |
| 2214 | var aw: std.io.AllocatingWriter = undefined; |
| 2215 | defer aw.deinit(); |
| 2216 | var s: Serializer = .{ .writer = aw.init(std.testing.allocator) }; |
| 2244 | 2217 | |
| 2245 | 2218 | // Tuples |
| 2246 | | try sz.tuple(.{ 1, 2 }, .{}); |
| 2247 | | try std.testing.expectEqualStrings(".{ 1, 2 }", buf.items); |
| 2248 | | buf.clearRetainingCapacity(); |
| 2219 | try s.tuple(.{ 1, 2 }, .{}); |
| 2220 | try std.testing.expectEqualStrings(".{ 1, 2 }", aw.getWritten()); |
| 2221 | aw.clearRetainingCapacity(); |
| 2249 | 2222 | |
| 2250 | 2223 | // Slice |
| 2251 | | try sz.tuple(@as([]const u8, &.{ 1, 2 }), .{}); |
| 2252 | | try std.testing.expectEqualStrings(".{ 1, 2 }", buf.items); |
| 2253 | | buf.clearRetainingCapacity(); |
| 2224 | try s.tuple(@as([]const u8, &.{ 1, 2 }), .{}); |
| 2225 | try std.testing.expectEqualStrings(".{ 1, 2 }", aw.getWritten()); |
| 2226 | aw.clearRetainingCapacity(); |
| 2254 | 2227 | |
| 2255 | 2228 | // Array |
| 2256 | | try sz.tuple([2]u8{ 1, 2 }, .{}); |
| 2257 | | try std.testing.expectEqualStrings(".{ 1, 2 }", buf.items); |
| 2258 | | buf.clearRetainingCapacity(); |
| 2229 | try s.tuple([2]u8{ 1, 2 }, .{}); |
| 2230 | try std.testing.expectEqualStrings(".{ 1, 2 }", aw.getWritten()); |
| 2231 | aw.clearRetainingCapacity(); |
| 2259 | 2232 | } |
| 2260 | 2233 | |
| 2261 | 2234 | test "std.zon stringify as float" { |
| 2262 | | var buf = std.ArrayList(u8).init(std.testing.allocator); |
| 2263 | | defer buf.deinit(); |
| 2264 | | var sz = serializer(buf.writer(), .{}); |
| 2235 | var aw: std.io.AllocatingWriter = undefined; |
| 2236 | defer aw.deinit(); |
| 2237 | var s: Serializer = .{ .writer = aw.init(std.testing.allocator) }; |
| 2265 | 2238 | |
| 2266 | 2239 | // Comptime float |
| 2267 | | try sz.float(2.5); |
| 2268 | | try std.testing.expectEqualStrings("2.5", buf.items); |
| 2269 | | buf.clearRetainingCapacity(); |
| 2240 | try s.float(2.5); |
| 2241 | try std.testing.expectEqualStrings("2.5", aw.getWritten()); |
| 2242 | aw.clearRetainingCapacity(); |
| 2270 | 2243 | |
| 2271 | 2244 | // Sized float |
| 2272 | | try sz.float(@as(f32, 2.5)); |
| 2273 | | try std.testing.expectEqualStrings("2.5", buf.items); |
| 2274 | | buf.clearRetainingCapacity(); |
| 2245 | try s.float(@as(f32, 2.5)); |
| 2246 | try std.testing.expectEqualStrings("2.5", aw.getWritten()); |
| 2247 | aw.clearRetainingCapacity(); |
| 2275 | 2248 | } |
| 2276 | 2249 | |
| 2277 | 2250 | test "std.zon stringify vector" { |
| ... | ... | @@ -2363,13 +2336,13 @@ test "std.zon pointers" { |
| 2363 | 2336 | } |
| 2364 | 2337 | |
| 2365 | 2338 | test "std.zon tuple/struct field" { |
| 2366 | | var buf = std.ArrayList(u8).init(std.testing.allocator); |
| 2367 | | defer buf.deinit(); |
| 2368 | | var sz = serializer(buf.writer(), .{}); |
| 2339 | var aw: std.io.AllocatingWriter = undefined; |
| 2340 | defer aw.deinit(); |
| 2341 | var s: Serializer = .{ .writer = aw.init(std.testing.allocator) }; |
| 2369 | 2342 | |
| 2370 | 2343 | // Test on structs |
| 2371 | 2344 | { |
| 2372 | | var root = try sz.beginStruct(.{}); |
| 2345 | var root = try s.beginStruct(.{}); |
| 2373 | 2346 | { |
| 2374 | 2347 | var tuple = try root.beginTupleField("foo", .{}); |
| 2375 | 2348 | try tuple.field(0, .{}); |
| ... | ... | @@ -2395,13 +2368,13 @@ test "std.zon tuple/struct field" { |
| 2395 | 2368 | \\ .b = 1, |
| 2396 | 2369 | \\ }, |
| 2397 | 2370 | \\} |
| 2398 | | , buf.items); |
| 2399 | | buf.clearRetainingCapacity(); |
| 2371 | , aw.getWritten()); |
| 2372 | aw.clearRetainingCapacity(); |
| 2400 | 2373 | } |
| 2401 | 2374 | |
| 2402 | 2375 | // Test on tuples |
| 2403 | 2376 | { |
| 2404 | | var root = try sz.beginTuple(.{}); |
| 2377 | var root = try s.beginTuple(.{}); |
| 2405 | 2378 | { |
| 2406 | 2379 | var tuple = try root.beginTupleField(.{}); |
| 2407 | 2380 | try tuple.field(0, .{}); |
| ... | ... | @@ -2427,7 +2400,7 @@ test "std.zon tuple/struct field" { |
| 2427 | 2400 | \\ .b = 1, |
| 2428 | 2401 | \\ }, |
| 2429 | 2402 | \\} |
| 2430 | | , buf.items); |
| 2431 | | buf.clearRetainingCapacity(); |
| 2403 | , aw.getWritten()); |
| 2404 | aw.clearRetainingCapacity(); |
| 2432 | 2405 | } |
| 2433 | 2406 | } |