Simplify file creation branch of process_rule.zig
Still not quite where I want it, but definitely better than what it was
This commit is contained in:
parent
5383b69eb6
commit
baf9ef38a4
1 changed files with 21 additions and 11 deletions
|
|
@ -20,18 +20,25 @@ pub fn run(allocator: std.mem.Allocator, params: *jetzig.data.Value, env: jetzig
|
||||||
};
|
};
|
||||||
|
|
||||||
const Rules = struct {
|
const Rules = struct {
|
||||||
rules: []Rule,
|
rules: []const Rule,
|
||||||
};
|
};
|
||||||
|
|
||||||
const rule = try std.json.parseFromSliceLeaky(Rule, allocator, try params.toJson(), .{ .ignore_unknown_fields = true });
|
const rule = try std.json.parseFromSliceLeaky(Rule, allocator, try params.toJson(), .{ .ignore_unknown_fields = true });
|
||||||
|
|
||||||
const file_read: std.fs.File = std.fs.cwd().openFile("rules.json", .{}) catch |read_err| switch (read_err) {
|
const file_read: std.fs.File = std.fs.cwd().openFile("rules.json", .{}) catch |read_err| switch (read_err) {
|
||||||
error.FileNotFound => std.fs.cwd().createFile("rules.json", .{ .read = true, .exclusive = true }) catch |write_err| switch (write_err) {
|
error.FileNotFound => {
|
||||||
|
const file = std.fs.cwd().createFile("rules.json", .{ .read = true, .exclusive = true }) catch |write_err| switch (write_err) {
|
||||||
error.PathAlreadyExists => unreachable,
|
error.PathAlreadyExists => unreachable,
|
||||||
else => {
|
else => {
|
||||||
std.log.debug("{any} while writing file", .{write_err});
|
std.log.debug("{any} while writing file", .{write_err});
|
||||||
return;
|
return;
|
||||||
},
|
},
|
||||||
|
};
|
||||||
|
const out_rules = Rules{ .rules = &[_]Rule{rule} };
|
||||||
|
const out = try std.json.stringifyAlloc(allocator, out_rules, .{});
|
||||||
|
try file.writeAll(out);
|
||||||
|
file.close();
|
||||||
|
return;
|
||||||
},
|
},
|
||||||
else => {
|
else => {
|
||||||
std.log.debug("{any} while reading file", .{read_err});
|
std.log.debug("{any} while reading file", .{read_err});
|
||||||
|
|
@ -40,16 +47,19 @@ pub fn run(allocator: std.mem.Allocator, params: *jetzig.data.Value, env: jetzig
|
||||||
};
|
};
|
||||||
|
|
||||||
var rules = std.ArrayList(Rule).init(allocator);
|
var rules = std.ArrayList(Rule).init(allocator);
|
||||||
|
defer rules.deinit();
|
||||||
|
|
||||||
const file_content = try file_read.readToEndAlloc(allocator, 16_000_000);
|
const file_content = try file_read.readToEndAlloc(allocator, 16_000_000);
|
||||||
if (file_content.len != 0) {
|
|
||||||
const content: Rules = try std.json.parseFromSliceLeaky(Rules, allocator, file_content, .{});
|
const content: Rules = try std.json.parseFromSliceLeaky(Rules, allocator, file_content, .{});
|
||||||
try rules.appendSlice(content.rules);
|
try rules.appendSlice(content.rules);
|
||||||
}
|
|
||||||
try rules.append(rule);
|
try rules.append(rule);
|
||||||
file_read.close();
|
file_read.close();
|
||||||
|
|
||||||
const file_write: std.fs.File = try std.fs.cwd().openFile("rules.json", .{ .mode = .write_only });
|
const file_write: std.fs.File = try std.fs.cwd().openFile("rules.json", .{ .mode = .write_only });
|
||||||
const out_rules = Rules{ .rules = rules.items };
|
const out_rules = Rules{ .rules = rules.items };
|
||||||
const out = try std.json.stringifyAlloc(allocator, out_rules, .{});
|
const out = try std.json.stringifyAlloc(allocator, out_rules, .{});
|
||||||
|
|
||||||
try file_write.writeAll(out);
|
try file_write.writeAll(out);
|
||||||
|
file_write.close();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue