feat: serialize ErrorLabel to json
This commit is contained in:
parent
184745e83d
commit
69a3fa57a8
61
src/errors/error_label.zig
Normal file
61
src/errors/error_label.zig
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub const ErrorLabel = struct {
|
||||||
|
message: []const u8,
|
||||||
|
start: usize,
|
||||||
|
end: usize,
|
||||||
|
|
||||||
|
/// Converts this struct into JSON
|
||||||
|
pub fn json(self: ErrorLabel, alloc: std.mem.Allocator) ![]u8 {
|
||||||
|
return try std.json.stringifyAlloc(alloc, .{
|
||||||
|
.message = self.message,
|
||||||
|
.start = self.start,
|
||||||
|
.end = self.end,
|
||||||
|
}, .{});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
test "should serialize" {
|
||||||
|
const label = ErrorLabel{
|
||||||
|
.message = "Error",
|
||||||
|
.start = 5,
|
||||||
|
.end = 6,
|
||||||
|
};
|
||||||
|
const json_str = try label.json(std.testing.allocator);
|
||||||
|
defer std.testing.allocator.free(json_str);
|
||||||
|
|
||||||
|
const expected =
|
||||||
|
\\{"message":"Error","start":5,"end":6}
|
||||||
|
;
|
||||||
|
try std.testing.expectEqualStrings(expected, json_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "should handle special characters" {
|
||||||
|
const label = ErrorLabel{
|
||||||
|
.message = "Error\"with\"quotes",
|
||||||
|
.start = 0,
|
||||||
|
.end = 1,
|
||||||
|
};
|
||||||
|
const json_str = try label.json(std.testing.allocator);
|
||||||
|
defer std.testing.allocator.free(json_str);
|
||||||
|
|
||||||
|
const expected =
|
||||||
|
\\{"message":"Error\"with\"quotes","start":0,"end":1}
|
||||||
|
;
|
||||||
|
try std.testing.expectEqualStrings(expected, json_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "should serialize empty message" {
|
||||||
|
const label = ErrorLabel{
|
||||||
|
.message = "",
|
||||||
|
.start = 0,
|
||||||
|
.end = 0,
|
||||||
|
};
|
||||||
|
const json_str = try label.json(std.testing.allocator);
|
||||||
|
defer std.testing.allocator.free(json_str);
|
||||||
|
|
||||||
|
const expected =
|
||||||
|
\\{"message":"","start":0,"end":0}
|
||||||
|
;
|
||||||
|
try std.testing.expectEqualStrings(expected, json_str);
|
||||||
|
}
|
@ -1,10 +1,5 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
pub const ErrorLabel = @import("./error_label.zig").ErrorLabel;
|
||||||
pub const ErrorLabel = struct {
|
|
||||||
message: []const u8,
|
|
||||||
start: usize,
|
|
||||||
end: usize,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Holds information about errors generated during the compilation,
|
/// Holds information about errors generated during the compilation,
|
||||||
/// and pretty prints them.
|
/// and pretty prints them.
|
||||||
@ -158,6 +153,12 @@ pub const ErrorData = struct {
|
|||||||
// - Get previous, current and next line
|
// - Get previous, current and next line
|
||||||
// - Display message
|
// - Display message
|
||||||
|
|
||||||
|
/// Transform this error into a JSON
|
||||||
|
pub fn json(alloc: std.mem.Allocator) void {
|
||||||
|
_ = alloc;
|
||||||
|
std.debug.panic(":c");
|
||||||
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *@This()) void {
|
pub fn deinit(self: *@This()) void {
|
||||||
self.labels.deinit();
|
self.labels.deinit();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user