diff --git a/README.md b/README.md index a27d6cf..a940669 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Provides the necessary building blocks to develop Language Server Protocol imple # Installation > [!NOTE] -> The default branch requires Zig `0.15.0-dev.1145+3ae0ba096` or later. Checkout the `0.14.x` branch when using Zig 0.14 +> The default branch requires Zig `0.15.0-dev.1160+e43617e68` or later. Checkout the `0.14.x` branch when using Zig 0.14 ```bash # Initialize a `zig build` project if you haven't already diff --git a/build.zig b/build.zig index 7c6b946..238e9e3 100644 --- a/build.zig +++ b/build.zig @@ -1,7 +1,7 @@ const std = @import("std"); const builtin = @import("builtin"); -const minimum_zig_version = "0.15.0-dev.1145+3ae0ba096"; +const minimum_zig_version = "0.15.0-dev.1160+e43617e68"; pub fn build(b: *std.Build) void { comptime if (builtin.zig_version.order(std.SemanticVersion.parse(minimum_zig_version) catch unreachable) == .lt) { diff --git a/build.zig.zon b/build.zig.zon index 2e1efcb..f2fec73 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,7 +1,7 @@ .{ .name = .lsp_kit, .version = "0.1.0", - .minimum_zig_version = "0.15.0-dev.1145+3ae0ba096", + .minimum_zig_version = "0.15.0-dev.1160+e43617e68", .dependencies = .{}, .paths = .{ "build.zig", diff --git a/src/lsp.zig b/src/lsp.zig index 40c2a17..7ed2109 100644 --- a/src/lsp.zig +++ b/src/lsp.zig @@ -707,23 +707,23 @@ pub const JsonRPCMessage = union(enum) { test "emit_null_optional_fields" { try std.testing.expectFmt( \\{"jsonrpc":"2.0","method":"exit"} - , "{f}", .{parser.jsonFmt(JsonRPCMessage{ .notification = .{ .method = "exit", .params = null } }, .{ .emit_null_optional_fields = false })}); + , "{f}", .{std.json.fmt(JsonRPCMessage{ .notification = .{ .method = "exit", .params = null } }, .{ .emit_null_optional_fields = false })}); try std.testing.expectFmt( \\{"jsonrpc":"2.0","method":"exit","params":null} - , "{f}", .{parser.jsonFmt(JsonRPCMessage{ .notification = .{ .method = "exit", .params = null } }, .{ .emit_null_optional_fields = true })}); + , "{f}", .{std.json.fmt(JsonRPCMessage{ .notification = .{ .method = "exit", .params = null } }, .{ .emit_null_optional_fields = true })}); try std.testing.expectFmt( \\{"jsonrpc":"2.0","method":"exit","params":null} - , "{f}", .{parser.jsonFmt(JsonRPCMessage{ .notification = .{ .method = "exit", .params = .null } }, .{ .emit_null_optional_fields = false })}); + , "{f}", .{std.json.fmt(JsonRPCMessage{ .notification = .{ .method = "exit", .params = .null } }, .{ .emit_null_optional_fields = false })}); try std.testing.expectFmt( \\{"jsonrpc":"2.0","method":"exit","params":null} - , "{f}", .{parser.jsonFmt(JsonRPCMessage{ .notification = .{ .method = "exit", .params = .null } }, .{ .emit_null_optional_fields = true })}); + , "{f}", .{std.json.fmt(JsonRPCMessage{ .notification = .{ .method = "exit", .params = .null } }, .{ .emit_null_optional_fields = true })}); try std.testing.expectFmt( \\{"jsonrpc":"2.0","result":null} - , "{f}", .{parser.jsonFmt(JsonRPCMessage{ .response = .{ .id = null, .result_or_error = .{ .result = null } } }, .{ .emit_null_optional_fields = false })}); + , "{f}", .{std.json.fmt(JsonRPCMessage{ .response = .{ .id = null, .result_or_error = .{ .result = null } } }, .{ .emit_null_optional_fields = false })}); try std.testing.expectFmt( \\{"jsonrpc":"2.0","id":null,"result":null} - , "{f}", .{parser.jsonFmt(JsonRPCMessage{ .response = .{ .id = null, .result_or_error = .{ .result = null } } }, .{ .emit_null_optional_fields = true })}); + , "{f}", .{std.json.fmt(JsonRPCMessage{ .response = .{ .id = null, .result_or_error = .{ .result = null } } }, .{ .emit_null_optional_fields = true })}); } fn testParse(message: []const u8, expected: JsonRPCMessage, parse_options: std.json.ParseOptions) !void { @@ -738,10 +738,10 @@ pub const JsonRPCMessage = union(enum) { const parsed_from_value = try std.json.parseFromValue(JsonRPCMessage, allocator, parsed_value.value, parse_options); defer parsed_from_value.deinit(); - const from_slice_stringified = try std.json.stringifyAlloc(allocator, parsed_from_slice.value, .{ .whitespace = .indent_2 }); + const from_slice_stringified = try std.json.Stringify.valueAlloc(allocator, parsed_from_slice.value, .{ .whitespace = .indent_2 }); defer allocator.free(from_slice_stringified); - const from_value_stringified = try std.json.stringifyAlloc(allocator, parsed_from_value.value, .{ .whitespace = .indent_2 }); + const from_value_stringified = try std.json.Stringify.valueAlloc(allocator, parsed_from_value.value, .{ .whitespace = .indent_2 }); defer allocator.free(from_value_stringified); if (!std.mem.eql(u8, from_slice_stringified, from_value_stringified)) { @@ -872,13 +872,13 @@ test TypedJsonRPCRequest { try std.testing.expectFmt( \\{"jsonrpc":"2.0","id":42,"method":"name","params":null} - , "{f}", .{parser.jsonFmt(Request{ .id = .{ .number = 42 }, .method = "name", .params = null }, .{})}); + , "{f}", .{std.json.fmt(Request{ .id = .{ .number = 42 }, .method = "name", .params = null }, .{})}); try std.testing.expectFmt( \\{"jsonrpc":"2.0","id":"42","method":"name"} - , "{f}", .{parser.jsonFmt(Request{ .id = .{ .string = "42" }, .method = "name", .params = null }, .{ .emit_null_optional_fields = false })}); + , "{f}", .{std.json.fmt(Request{ .id = .{ .string = "42" }, .method = "name", .params = null }, .{ .emit_null_optional_fields = false })}); try std.testing.expectFmt( \\{"jsonrpc":"2.0","id":42,"method":"name","params":true} - , "{f}", .{parser.jsonFmt(Request{ .id = .{ .number = 42 }, .method = "name", .params = true }, .{})}); + , "{f}", .{std.json.fmt(Request{ .id = .{ .number = 42 }, .method = "name", .params = true }, .{})}); } pub fn TypedJsonRPCNotification( @@ -924,13 +924,13 @@ test TypedJsonRPCNotification { try std.testing.expectFmt( \\{"jsonrpc":"2.0","method":"name","params":null} - , "{f}", .{parser.jsonFmt(Notification{ .method = "name", .params = null }, .{})}); + , "{f}", .{std.json.fmt(Notification{ .method = "name", .params = null }, .{})}); try std.testing.expectFmt( \\{"jsonrpc":"2.0","method":"name"} - , "{f}", .{parser.jsonFmt(Notification{ .method = "name", .params = null }, .{ .emit_null_optional_fields = false })}); + , "{f}", .{std.json.fmt(Notification{ .method = "name", .params = null }, .{ .emit_null_optional_fields = false })}); try std.testing.expectFmt( \\{"jsonrpc":"2.0","method":"name","params":true} - , "{f}", .{parser.jsonFmt(Notification{ .method = "name", .params = true }, .{})}); + , "{f}", .{std.json.fmt(Notification{ .method = "name", .params = true }, .{})}); } pub fn TypedJsonRPCResponse( @@ -982,13 +982,13 @@ test TypedJsonRPCResponse { try std.testing.expectFmt( \\{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"message","data":null}} - , "{f}", .{parser.jsonFmt(Response{ + , "{f}", .{std.json.fmt(Response{ .id = null, .result_or_error = .{ .@"error" = .{ .code = .invalid_request, .message = "message", .data = .null } }, }, .{})}); try std.testing.expectFmt( \\{"jsonrpc":"2.0","id":5,"result":true} - , "{f}", .{parser.jsonFmt(Response{ + , "{f}", .{std.json.fmt(Response{ .id = .{ .number = 5 }, .result_or_error = .{ .result = true }, }, .{})}); @@ -1205,14 +1205,14 @@ pub const Transport = struct { method: []const u8, comptime Params: type, params: Params, - options: std.json.StringifyOptions, + options: std.json.Stringify.Options, ) (WriteError || std.mem.Allocator.Error)!void { const request: TypedJsonRPCRequest(Params) = .{ .id = id, .method = method, .params = params, }; - const json_message = try std.json.stringifyAlloc(allocator, request, options); + const json_message = try std.json.Stringify.valueAlloc(allocator, request, options); defer allocator.free(json_message); try transport.writeJsonMessage(json_message); } @@ -1223,13 +1223,13 @@ pub const Transport = struct { method: []const u8, comptime Params: type, params: Params, - options: std.json.StringifyOptions, + options: std.json.Stringify.Options, ) (WriteError || std.mem.Allocator.Error)!void { const request: TypedJsonRPCNotification(Params) = .{ .method = method, .params = params, }; - const json_message = try std.json.stringifyAlloc(allocator, request, options); + const json_message = try std.json.Stringify.valueAlloc(allocator, request, options); defer allocator.free(json_message); try transport.writeJsonMessage(json_message); } @@ -1240,13 +1240,13 @@ pub const Transport = struct { id: ?JsonRPCMessage.ID, comptime Result: type, result: Result, - options: std.json.StringifyOptions, + options: std.json.Stringify.Options, ) (WriteError || std.mem.Allocator.Error)!void { const request: TypedJsonRPCResponse(Result) = .{ .id = id, .result_or_error = .{ .result = result }, }; - const json_message = try std.json.stringifyAlloc(allocator, request, options); + const json_message = try std.json.Stringify.valueAlloc(allocator, request, options); defer allocator.free(json_message); try transport.writeJsonMessage(json_message); } @@ -1256,13 +1256,13 @@ pub const Transport = struct { allocator: std.mem.Allocator, id: ?JsonRPCMessage.ID, err: JsonRPCMessage.Response.Error, - options: std.json.StringifyOptions, + options: std.json.Stringify.Options, ) (WriteError || std.mem.Allocator.Error)!void { const request: TypedJsonRPCResponse(void) = .{ .id = id, .result_or_error = .{ .@"error" = err }, }; - const json_message = try std.json.stringifyAlloc(allocator, request, options); + const json_message = try std.json.Stringify.valueAlloc(allocator, request, options); defer allocator.free(json_message); try transport.writeJsonMessage(json_message); } @@ -1375,14 +1375,14 @@ pub fn writeRequest( method: []const u8, comptime Params: type, params: Params, - options: std.json.StringifyOptions, + options: std.json.Stringify.Options, ) (std.io.Writer.Error || std.mem.Allocator.Error)!void { const request: TypedJsonRPCRequest(Params) = .{ .id = id, .method = method, .params = params, }; - const json_message = try std.json.stringifyAlloc(allocator, request, options); + const json_message = try std.json.Stringify.valueAlloc(allocator, request, options); defer allocator.free(json_message); try writeJsonMessage(writer, json_message); } @@ -1418,13 +1418,13 @@ pub fn writeNotification( method: []const u8, comptime Params: type, params: Params, - options: std.json.StringifyOptions, + options: std.json.Stringify.Options, ) (std.io.Writer.Error || std.mem.Allocator.Error)!void { const request: TypedJsonRPCNotification(Params) = .{ .method = method, .params = params, }; - const json_message = try std.json.stringifyAlloc(allocator, request, options); + const json_message = try std.json.Stringify.valueAlloc(allocator, request, options); defer allocator.free(json_message); try writeJsonMessage(writer, json_message); } @@ -1458,13 +1458,13 @@ pub fn writeResponse( id: ?JsonRPCMessage.ID, comptime Result: type, result: Result, - options: std.json.StringifyOptions, + options: std.json.Stringify.Options, ) (std.io.Writer.Error || std.mem.Allocator.Error)!void { const request: TypedJsonRPCResponse(Result) = .{ .id = id, .result_or_error = .{ .result = result }, }; - const json_message = try std.json.stringifyAlloc(allocator, request, options); + const json_message = try std.json.Stringify.valueAlloc(allocator, request, options); defer allocator.free(json_message); try writeJsonMessage(writer, json_message); } @@ -1497,13 +1497,13 @@ pub fn writeErrorResponse( allocator: std.mem.Allocator, id: ?JsonRPCMessage.ID, err: JsonRPCMessage.Response.Error, - options: std.json.StringifyOptions, + options: std.json.Stringify.Options, ) (std.io.Writer.Error || std.mem.Allocator.Error)!void { const request: TypedJsonRPCResponse(void) = .{ .id = id, .result_or_error = .{ .@"error" = err }, }; - const json_message = try std.json.stringifyAlloc(allocator, request, options); + const json_message = try std.json.Stringify.valueAlloc(allocator, request, options); defer allocator.free(json_message); try writeJsonMessage(writer, json_message); } @@ -1571,7 +1571,7 @@ fn bufPrintLogMessageTypeErased( var writer: std.io.Writer = .fixed(buffer); writer.print( \\{{"jsonrpc":"2.0","method":"window/logMessage","params":{{"type":{f},"message":" - , .{parser.jsonFmt(message_type, .{})}) catch unreachable; + , .{std.json.fmt(message_type, .{})}) catch unreachable; const json_message_suffix = "\"}}".*; const ellipses = "...".*; @@ -2332,9 +2332,9 @@ fn testMessageWithOptions( const message_from_value = try std.json.parseFromValueLeaky(ExampleMessage, arena.allocator(), value, parse_options); const message_from_slice = try ExampleMessage.parseFromSliceLeaky(arena.allocator(), json_message, parse_options); - const message_string = try std.json.stringifyAlloc(arena.allocator(), message, .{ .whitespace = .indent_2 }); - const message_from_value_string = try std.json.stringifyAlloc(arena.allocator(), message_from_value, .{ .whitespace = .indent_2 }); - const message_from_slice_string = try std.json.stringifyAlloc(arena.allocator(), message_from_slice, .{ .whitespace = .indent_2 }); + const message_string = try std.json.Stringify.valueAlloc(arena.allocator(), message, .{ .whitespace = .indent_2 }); + const message_from_value_string = try std.json.Stringify.valueAlloc(arena.allocator(), message_from_value, .{ .whitespace = .indent_2 }); + const message_from_slice_string = try std.json.Stringify.valueAlloc(arena.allocator(), message_from_slice, .{ .whitespace = .indent_2 }); try std.testing.expectEqualStrings(message_string, message_from_value_string); try std.testing.expectEqualStrings(message_string, message_from_slice_string); @@ -2422,23 +2422,23 @@ test "Message - ignore_unknown_fields" { test "Message - stringify emit_null_optional_fields" { try std.testing.expectFmt( \\{"jsonrpc":"2.0","method":"exit"} - , "{f}", .{parser.jsonFmt(ExampleMessage{ .notification = .{ .params = .exit } }, .{ .emit_null_optional_fields = false })}); + , "{f}", .{std.json.fmt(ExampleMessage{ .notification = .{ .params = .exit } }, .{ .emit_null_optional_fields = false })}); try std.testing.expectFmt( \\{"jsonrpc":"2.0","method":"exit","params":null} - , "{f}", .{parser.jsonFmt(ExampleMessage{ .notification = .{ .params = .exit } }, .{ .emit_null_optional_fields = true })}); + , "{f}", .{std.json.fmt(ExampleMessage{ .notification = .{ .params = .exit } }, .{ .emit_null_optional_fields = true })}); try std.testing.expectFmt( \\{"jsonrpc":"2.0","method":"foo"} - , "{f}", .{parser.jsonFmt(ExampleMessage{ .notification = .{ .params = .{ .other = .{ .method = "foo", .params = null } } } }, .{ .emit_null_optional_fields = false })}); + , "{f}", .{std.json.fmt(ExampleMessage{ .notification = .{ .params = .{ .other = .{ .method = "foo", .params = null } } } }, .{ .emit_null_optional_fields = false })}); try std.testing.expectFmt( \\{"jsonrpc":"2.0","method":"foo","params":null} - , "{f}", .{parser.jsonFmt(ExampleMessage{ .notification = .{ .params = .{ .other = .{ .method = "foo", .params = null } } } }, .{ .emit_null_optional_fields = true })}); + , "{f}", .{std.json.fmt(ExampleMessage{ .notification = .{ .params = .{ .other = .{ .method = "foo", .params = null } } } }, .{ .emit_null_optional_fields = true })}); try std.testing.expectFmt( \\{"jsonrpc":"2.0","method":"foo","params":null} - , "{f}", .{parser.jsonFmt(ExampleMessage{ .notification = .{ .params = .{ .other = .{ .method = "foo", .params = .null } } } }, .{ .emit_null_optional_fields = false })}); + , "{f}", .{std.json.fmt(ExampleMessage{ .notification = .{ .params = .{ .other = .{ .method = "foo", .params = .null } } } }, .{ .emit_null_optional_fields = false })}); try std.testing.expectFmt( \\{"jsonrpc":"2.0","method":"foo","params":null} - , "{f}", .{parser.jsonFmt(ExampleMessage{ .notification = .{ .params = .{ .other = .{ .method = "foo", .params = .null } } } }, .{ .emit_null_optional_fields = true })}); + , "{f}", .{std.json.fmt(ExampleMessage{ .notification = .{ .params = .{ .other = .{ .method = "foo", .params = .null } } } }, .{ .emit_null_optional_fields = true })}); } test "Message.Request" { diff --git a/src/main.zig b/src/main.zig index d550940..defbd70 100644 --- a/src/main.zig +++ b/src/main.zig @@ -338,14 +338,14 @@ fn writeRequest(writer: *std.io.Writer, meta_model: MetaModel, request: MetaMode \\ , .{ request.method, - if (request.documentation) |documentation| jsonFmt(documentation, .{}) else null, + if (request.documentation) |documentation| std.json.fmt(documentation, .{}) else null, messageDirectionName(request.messageDirection), // NOTE: Multiparams not used here, so we dont have to implement them :) if (request.params) |params| fmtType(params.Type, &meta_model) else null, fmtType(request.result, &meta_model), if (request.partialResult) |ty| fmtType(ty, &meta_model) else null, if (request.errorData) |ty| fmtType(ty, &meta_model) else null, - if (request.registrationMethod) |method| jsonFmt(method, .{}) else null, + if (request.registrationMethod) |method| std.json.fmt(method, .{}) else null, if (request.registrationOptions) |ty| fmtType(ty, &meta_model) else null, }); } @@ -364,11 +364,11 @@ fn writeNotification(writer: *std.io.Writer, meta_model: MetaModel, notification \\ , .{ notification.method, - if (notification.documentation) |documentation| jsonFmt(documentation, .{}) else null, + if (notification.documentation) |documentation| std.json.fmt(documentation, .{}) else null, messageDirectionName(notification.messageDirection), // NOTE: Multiparams not used here, so we dont have to implement them :) if (notification.params) |params| fmtType(params.Type, &meta_model) else null, - if (notification.registrationMethod) |method| jsonFmt(method, .{}) else null, + if (notification.registrationMethod) |method| std.json.fmt(method, .{}) else null, if (notification.registrationOptions) |ty| fmtType(ty, &meta_model) else null, }); } @@ -479,23 +479,3 @@ fn writeMetaModel(writer: *std.io.Writer, meta_model: MetaModel) std.io.Writer.E } try writer.writeAll("};\n"); } - -/// Like `std.json.fmt` but supports `std.io.Writer`. -pub fn jsonFmt(value: anytype, options: std.json.StringifyOptions) std.fmt.Alt(FormatJson(@TypeOf(value)), FormatJson(@TypeOf(value)).format) { - return .{ .data = .{ .value = value, .options = options } }; -} - -fn FormatJson(comptime T: type) type { - return struct { - value: T, - options: std.json.StringifyOptions, - - pub fn format(data: @This(), writer: *std.io.Writer) std.io.Writer.Error!void { - const any_writer: std.io.AnyWriter = .{ - .context = @ptrCast(writer), - .writeFn = @ptrCast(&std.io.Writer.write), - }; - std.json.stringify(data.value, data.options, any_writer) catch |err| return @errorCast(err); - } - }; -} diff --git a/src/parser.zig b/src/parser.zig index ebcdbd6..3770501 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -2,26 +2,6 @@ const std = @import("std"); -/// Like `std.json.fmt` but supports `std.io.Writer`. -pub fn jsonFmt(value: anytype, options: std.json.StringifyOptions) std.fmt.Alt(FormatJson(@TypeOf(value)), FormatJson(@TypeOf(value)).format) { - return .{ .data = .{ .value = value, .options = options } }; -} - -fn FormatJson(comptime T: type) type { - return struct { - value: T, - options: std.json.StringifyOptions, - - pub fn format(data: @This(), writer: *std.io.Writer) std.io.Writer.Error!void { - const any_writer: std.io.AnyWriter = .{ - .context = writer, - .writeFn = @ptrCast(&std.io.Writer.write), // cast discards const qualifier but should be fine. I hope - }; - std.json.stringify(data.value, data.options, any_writer) catch |err| return @errorCast(err); - } - }; -} - pub fn Map(comptime Key: type, comptime Value: type) type { if (Key != []const u8) @compileError("TODO support non string Key's"); return std.json.ArrayHashMap(Value); @@ -120,8 +100,8 @@ test "UnionParser.jsonStringify" { pub const jsonStringify = UnionParser(@This()).jsonStringify; }; - try std.testing.expectFmt("5", "{f}", .{jsonFmt(U{ .number = 5 }, .{})}); - try std.testing.expectFmt("\"foo\"", "{f}", .{jsonFmt(U{ .string = "foo" }, .{})}); + try std.testing.expectFmt("5", "{f}", .{std.json.fmt(U{ .number = 5 }, .{})}); + try std.testing.expectFmt("\"foo\"", "{f}", .{std.json.fmt(U{ .string = "foo" }, .{})}); } pub fn EnumCustomStringValues(comptime T: type, comptime contains_empty_enum: bool) type { @@ -204,11 +184,11 @@ test EnumCustomStringValues { pub const jsonStringify = EnumCustomStringValues(@This(), false).jsonStringify; }; - try std.testing.expectFmt("\"foo\"", "{f}", .{jsonFmt(E{ .foo = {} }, .{})}); - try std.testing.expectFmt("\"bar\"", "{f}", .{jsonFmt(E{ .bar = {} }, .{})}); - try std.testing.expectFmt("\"baz\"", "{f}", .{jsonFmt(E{ .baz = {} }, .{})}); - try std.testing.expectFmt("\"\"", "{f}", .{jsonFmt(E{ .custom_value = "" }, .{})}); - try std.testing.expectFmt("\"boo\"", "{f}", .{jsonFmt(E{ .custom_value = "boo" }, .{})}); + try std.testing.expectFmt("\"foo\"", "{f}", .{std.json.fmt(E{ .foo = {} }, .{})}); + try std.testing.expectFmt("\"bar\"", "{f}", .{std.json.fmt(E{ .bar = {} }, .{})}); + try std.testing.expectFmt("\"baz\"", "{f}", .{std.json.fmt(E{ .baz = {} }, .{})}); + try std.testing.expectFmt("\"\"", "{f}", .{std.json.fmt(E{ .custom_value = "" }, .{})}); + try std.testing.expectFmt("\"boo\"", "{f}", .{std.json.fmt(E{ .custom_value = "boo" }, .{})}); try expectParseEqual(E, E.foo, "\"foo\""); try expectParseEqual(E, E.bar, "\"bar\""); @@ -239,13 +219,13 @@ test EnumCustomStringValues { const boo: E = .{ .unknown_value = "boo" }; const zoo: E = .{ .unknown_value = "zoo" }; - try std.testing.expectFmt("\"foo\"", "{f}", .{jsonFmt(foo, .{})}); - try std.testing.expectFmt("\"bar\"", "{f}", .{jsonFmt(bar, .{})}); - try std.testing.expectFmt("\"baz\"", "{f}", .{jsonFmt(baz, .{})}); - try std.testing.expectFmt("\"\"", "{f}", .{jsonFmt(empty, .{})}); - try std.testing.expectFmt("\"\"", "{f}", .{jsonFmt(custom_empty, .{})}); - try std.testing.expectFmt("\"boo\"", "{f}", .{jsonFmt(boo, .{})}); - try std.testing.expectFmt("\"zoo\"", "{f}", .{jsonFmt(zoo, .{})}); + try std.testing.expectFmt("\"foo\"", "{f}", .{std.json.fmt(foo, .{})}); + try std.testing.expectFmt("\"bar\"", "{f}", .{std.json.fmt(bar, .{})}); + try std.testing.expectFmt("\"baz\"", "{f}", .{std.json.fmt(baz, .{})}); + try std.testing.expectFmt("\"\"", "{f}", .{std.json.fmt(empty, .{})}); + try std.testing.expectFmt("\"\"", "{f}", .{std.json.fmt(custom_empty, .{})}); + try std.testing.expectFmt("\"boo\"", "{f}", .{std.json.fmt(boo, .{})}); + try std.testing.expectFmt("\"zoo\"", "{f}", .{std.json.fmt(zoo, .{})}); try expectParseEqual(E, foo, "\"foo\""); try expectParseEqual(E, bar, "\"bar\""); @@ -287,10 +267,10 @@ test EnumCustomStringValues { const true_empty: E = .{ .custom_value = "" }; const boo: E = .{ .custom_value = "boo" }; - try std.testing.expectFmt("\"foo\"", "{f}", .{jsonFmt(foo, .{})}); - try std.testing.expectFmt("\"empty\"", "{f}", .{jsonFmt(empty, .{})}); - try std.testing.expectFmt("\"\"", "{f}", .{jsonFmt(true_empty, .{})}); - try std.testing.expectFmt("\"boo\"", "{f}", .{jsonFmt(boo, .{})}); + try std.testing.expectFmt("\"foo\"", "{f}", .{std.json.fmt(foo, .{})}); + try std.testing.expectFmt("\"empty\"", "{f}", .{std.json.fmt(empty, .{})}); + try std.testing.expectFmt("\"\"", "{f}", .{std.json.fmt(true_empty, .{})}); + try std.testing.expectFmt("\"boo\"", "{f}", .{std.json.fmt(boo, .{})}); try expectParseEqual(E, foo, "\"foo\""); try expectParseEqual(E, empty, "\"empty\""); @@ -326,9 +306,9 @@ test EnumStringifyAsInt { baz, pub const jsonStringify = EnumStringifyAsInt(@This()).jsonStringify; }; - try std.testing.expectFmt("0", "{f}", .{jsonFmt(E.foo, .{})}); - try std.testing.expectFmt("1", "{f}", .{jsonFmt(E.bar, .{})}); - try std.testing.expectFmt("2", "{f}", .{jsonFmt(E.baz, .{})}); + try std.testing.expectFmt("0", "{f}", .{std.json.fmt(E.foo, .{})}); + try std.testing.expectFmt("1", "{f}", .{std.json.fmt(E.bar, .{})}); + try std.testing.expectFmt("2", "{f}", .{std.json.fmt(E.baz, .{})}); } { @@ -338,9 +318,9 @@ test EnumStringifyAsInt { baz = 5, pub const jsonStringify = EnumStringifyAsInt(@This()).jsonStringify; }; - try std.testing.expectFmt("2", "{f}", .{jsonFmt(E.foo, .{})}); - try std.testing.expectFmt("3", "{f}", .{jsonFmt(E.bar, .{})}); - try std.testing.expectFmt("5", "{f}", .{jsonFmt(E.baz, .{})}); + try std.testing.expectFmt("2", "{f}", .{std.json.fmt(E.foo, .{})}); + try std.testing.expectFmt("3", "{f}", .{std.json.fmt(E.bar, .{})}); + try std.testing.expectFmt("5", "{f}", .{std.json.fmt(E.baz, .{})}); } { @@ -351,10 +331,10 @@ test EnumStringifyAsInt { _, pub const jsonStringify = EnumStringifyAsInt(@This()).jsonStringify; }; - try std.testing.expectFmt("0", "{f}", .{jsonFmt(E.foo, .{})}); - try std.testing.expectFmt("3", "{f}", .{jsonFmt(E.bar, .{})}); - try std.testing.expectFmt("4", "{f}", .{jsonFmt(E.baz, .{})}); - try std.testing.expectFmt("7", "{f}", .{jsonFmt(@as(E, @enumFromInt(7)), .{})}); + try std.testing.expectFmt("0", "{f}", .{std.json.fmt(E.foo, .{})}); + try std.testing.expectFmt("3", "{f}", .{std.json.fmt(E.bar, .{})}); + try std.testing.expectFmt("4", "{f}", .{std.json.fmt(E.baz, .{})}); + try std.testing.expectFmt("7", "{f}", .{std.json.fmt(@as(E, @enumFromInt(7)), .{})}); } }