Missed some items during the rename from print to write

This commit is contained in:
Cameron Reed 2024-11-04 15:44:56 -07:00
parent b31820d771
commit 310d40be12
2 changed files with 23 additions and 29 deletions

View File

@ -40,7 +40,7 @@ pub const JSONFormat = struct {
} }
}; };
fn printIndent(writer: std.fs.File.Writer, format: JSONFormat) !void { fn writeIndent(writer: std.fs.File.Writer, format: JSONFormat) !void {
if (!format.one_line) { if (!format.one_line) {
try writer.writeByte('\n'); try writer.writeByte('\n');
} }
@ -92,14 +92,14 @@ pub const JSONObject = struct {
try writer.writeAll(", "); try writer.writeAll(", ");
} }
try printIndent(writer, format.indent()); try writeIndent(writer, format.indent());
try writer.print("\"{s}\": ", .{key.*}); try writer.print("\"{s}\": ", .{key.*});
try self.children.get(key.*).?.print(writer, format.indent()); try self.children.get(key.*).?.write(writer, format.indent());
first = false; first = false;
} }
try printIndent(writer, format); try writeIndent(writer, format);
try writer.writeByte('}'); try writer.writeByte('}');
} }
@ -162,7 +162,7 @@ pub const JSONValue = struct {
deinit: *const fn (self: *JSONValue) void, deinit: *const fn (self: *JSONValue) void,
print: *const fn (self: *const JSONValue, writer: std.fs.File.Writer, format: JSONFormat) std.fs.File.WriteError!void, write: *const fn (self: *const JSONValue, writer: std.fs.File.Writer, format: JSONFormat) std.fs.File.WriteError!void,
toString: *const fn (self: *const JSONValue, allocator: std.mem.Allocator, format: JSONFormat) std.mem.Allocator.Error![]const u8, toString: *const fn (self: *const JSONValue, allocator: std.mem.Allocator, format: JSONFormat) std.mem.Allocator.Error![]const u8,
}; };
@ -198,8 +198,8 @@ pub const JSONValue = struct {
return self.vtable.getObject(self); return self.vtable.getObject(self);
} }
pub fn print(self: *const JSONValue, writer: std.fs.File.Writer, format: JSONFormat) std.fs.File.WriteError!void { pub fn write(self: *const JSONValue, writer: std.fs.File.Writer, format: JSONFormat) std.fs.File.WriteError!void {
return self.vtable.print(self, writer, format); return self.vtable.write(self, writer, format);
} }
pub fn toString(self: *const JSONValue, allocator: std.mem.Allocator, format: JSONFormat) std.mem.Allocator.Error![]const u8 { pub fn toString(self: *const JSONValue, allocator: std.mem.Allocator, format: JSONFormat) std.mem.Allocator.Error![]const u8 {
@ -213,13 +213,13 @@ pub const JSONValue = struct {
pub fn createNull(allocator: std.mem.Allocator) JSONError!*JSONValue { pub fn createNull(allocator: std.mem.Allocator) JSONError!*JSONValue {
var self = try allocator.create(JSONValue); var self = try allocator.create(JSONValue);
self.type = .Null; self.type = .Null;
self.vtable = .{ .deinit = basicDeinit, .print = printNull, .toString = toStringNull }; self.vtable = .{ .deinit = basicDeinit, .write = writeNull, .toString = toStringNull };
self.allocator = allocator; self.allocator = allocator;
return self; return self;
} }
fn printNull(_: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat) std.fs.File.WriteError!void { fn writeNull(_: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat) std.fs.File.WriteError!void {
return writer.writeAll("null"); return writer.writeAll("null");
} }
@ -241,7 +241,7 @@ pub const JSONArrayValue = struct {
self.array = array; self.array = array;
self.value = .{ self.value = .{
.type = .Array, .type = .Array,
.vtable = .{ .getArray = getValue, .print = print, .toString = toString, .deinit = deinit }, .vtable = .{ .getArray = getValue, .write = write, .toString = toString, .deinit = deinit },
.allocator = allocator, .allocator = allocator,
}; };
@ -253,7 +253,7 @@ pub const JSONArrayValue = struct {
return self.array; return self.array;
} }
fn print(value: *const JSONValue, writer: std.fs.File.Writer, format: JSONFormat) std.fs.File.WriteError!void { fn write(value: *const JSONValue, writer: std.fs.File.Writer, format: JSONFormat) std.fs.File.WriteError!void {
const self: *const JSONArrayValue = @fieldParentPtr("value", value); const self: *const JSONArrayValue = @fieldParentPtr("value", value);
try writer.writeAll("[ "); try writer.writeAll("[ ");
@ -264,7 +264,7 @@ pub const JSONArrayValue = struct {
try writer.writeAll(", "); try writer.writeAll(", ");
} }
try val.print(writer, format); try val.write(writer, format);
first = false; first = false;
} }
@ -313,7 +313,7 @@ pub const JSONObjectValue = struct {
self.object = object; self.object = object;
self.value = .{ self.value = .{
.type = .Object, .type = .Object,
.vtable = .{ .getObject = getValue, .print = print, .toString = toString, .deinit = deinit }, .vtable = .{ .getObject = getValue, .write = write, .toString = toString, .deinit = deinit },
.allocator = allocator, .allocator = allocator,
}; };
@ -325,7 +325,7 @@ pub const JSONObjectValue = struct {
return self.object; return self.object;
} }
fn print(value: *const JSONValue, writer: std.fs.File.Writer, format: JSONFormat) std.fs.File.WriteError!void { fn write(value: *const JSONValue, writer: std.fs.File.Writer, format: JSONFormat) std.fs.File.WriteError!void {
const self: *const JSONObjectValue = @fieldParentPtr("value", value); const self: *const JSONObjectValue = @fieldParentPtr("value", value);
try self.object.write(writer, format); try self.object.write(writer, format);
} }
@ -351,7 +351,7 @@ pub const JSONIntValue = struct {
self.int = value; self.int = value;
self.value = .{ self.value = .{
.type = .Int, .type = .Int,
.vtable = .{ .getInt = getValue, .print = print, .toString = toString, .deinit = deinit }, .vtable = .{ .getInt = getValue, .write = write, .toString = toString, .deinit = deinit },
.allocator = allocator, .allocator = allocator,
}; };
@ -363,7 +363,7 @@ pub const JSONIntValue = struct {
return self.int; return self.int;
} }
fn print(value: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat) std.fs.File.WriteError!void { fn write(value: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat) std.fs.File.WriteError!void {
const self: *const JSONIntValue = @fieldParentPtr("value", value); const self: *const JSONIntValue = @fieldParentPtr("value", value);
try writer.print("{d}", .{self.int}); try writer.print("{d}", .{self.int});
} }
@ -388,7 +388,7 @@ pub const JSONFloatValue = struct {
self.float = value; self.float = value;
self.value = .{ self.value = .{
.type = .Float, .type = .Float,
.vtable = .{ .getFloat = getValue, .print = print, .toString = toString, .deinit = deinit }, .vtable = .{ .getFloat = getValue, .write = write, .toString = toString, .deinit = deinit },
.allocator = allocator, .allocator = allocator,
}; };
@ -400,7 +400,7 @@ pub const JSONFloatValue = struct {
return self.float; return self.float;
} }
fn print(value: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat) std.fs.File.WriteError!void { fn write(value: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat) std.fs.File.WriteError!void {
const self: *const JSONFloatValue = @fieldParentPtr("value", value); const self: *const JSONFloatValue = @fieldParentPtr("value", value);
try writer.print("{d}", .{self.float}); try writer.print("{d}", .{self.float});
} }
@ -425,7 +425,7 @@ pub const JSONBoolValue = struct {
self.boolean = value; self.boolean = value;
self.value = .{ self.value = .{
.type = .Bool, .type = .Bool,
.vtable = .{ .getBool = getValue, .print = print, .toString = toString, .deinit = deinit }, .vtable = .{ .getBool = getValue, .write = write, .toString = toString, .deinit = deinit },
.allocator = allocator, .allocator = allocator,
}; };
@ -437,7 +437,7 @@ pub const JSONBoolValue = struct {
return self.boolean; return self.boolean;
} }
fn print(value: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat) std.fs.File.WriteError!void { fn write(value: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat) std.fs.File.WriteError!void {
const self: *const JSONBoolValue = @fieldParentPtr("value", value); const self: *const JSONBoolValue = @fieldParentPtr("value", value);
if (self.boolean) { if (self.boolean) {
try writer.writeAll("true"); try writer.writeAll("true");
@ -470,7 +470,7 @@ pub const JSONStringValue = struct {
self.string = string; self.string = string;
self.value = .{ self.value = .{
.type = .String, .type = .String,
.vtable = .{ .getString = getValue, .print = print, .toString = toString, .deinit = deinit }, .vtable = .{ .getString = getValue, .write = write, .toString = toString, .deinit = deinit },
.allocator = allocator, .allocator = allocator,
}; };
@ -482,7 +482,7 @@ pub const JSONStringValue = struct {
return self.string; return self.string;
} }
fn print(value: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat) std.fs.File.WriteError!void { fn write(value: *const JSONValue, writer: std.fs.File.Writer, _: JSONFormat) std.fs.File.WriteError!void {
const self: *const JSONStringValue = @fieldParentPtr("value", value); const self: *const JSONStringValue = @fieldParentPtr("value", value);
try writer.print("\"{s}\"", .{self.string}); try writer.print("\"{s}\"", .{self.string});
} }
@ -529,7 +529,7 @@ pub fn writeToFile(fileName: []const u8, json: *JSONObject, format: JSONFormat)
defer file.close(); defer file.close();
const writer = file.writer(); const writer = file.writer();
return json.write(writer, format, 0); return json.write(writer, format);
} }
const JSONParser = struct { const JSONParser = struct {

View File

@ -68,10 +68,4 @@ pub fn main() void {
}; };
back2JSON.write(writer, .{}) catch return; back2JSON.write(writer, .{}) catch return;
std.debug.print("\n", .{}); std.debug.print("\n", .{});
const rootJSON = json.structToJSON(allocator, json.JSONFormat{}) catch |err| {
std.debug.print("Failed to convert root to JSON: {any}\n", .{err});
return;
};
rootJSON.write(writer, .{}) catch return;
} }