diff options
Diffstat (limited to 'src/Config.zig')
| -rw-r--r-- | src/Config.zig | 68 |
1 files changed, 40 insertions, 28 deletions
diff --git a/src/Config.zig b/src/Config.zig index 4eb58cc..e559cad 100644 --- a/src/Config.zig +++ b/src/Config.zig | |||
| @@ -3,6 +3,7 @@ const std = @import("std"); | |||
| 3 | const xdg = @import("xdg"); | 3 | const xdg = @import("xdg"); |
| 4 | 4 | ||
| 5 | const Allocator = std.mem.Allocator; | 5 | const Allocator = std.mem.Allocator; |
| 6 | const ArenaAllocator = std.heap.ArenaAllocator; | ||
| 6 | const ArrayList = std.ArrayList; | 7 | const ArrayList = std.ArrayList; |
| 7 | const Config = @This(); | 8 | const Config = @This(); |
| 8 | const CrossTarget = std.zig.CrossTarget; | 9 | const CrossTarget = std.zig.CrossTarget; |
| @@ -55,38 +56,49 @@ pub fn deinit(self: *Config) void { | |||
| 55 | } | 56 | } |
| 56 | 57 | ||
| 57 | fn readConfig(self: *Config, file: File) !void { | 58 | fn readConfig(self: *Config, file: File) !void { |
| 58 | const allocator = self.allocator; | 59 | var arena = ArenaAllocator.init(self.allocator); |
| 59 | const reader = file.reader(); | 60 | defer arena.deinit(); |
| 60 | 61 | ||
| 61 | var json_str = try reader.readAllAlloc(allocator, std.math.maxInt(u64)); | 62 | const allocator = arena.allocator(); |
| 62 | defer allocator.free(json_str); | ||
| 63 | 63 | ||
| 64 | var parser = std.json.Parser.init(allocator, false); | 64 | const ConfigJson = struct { |
| 65 | defer parser.deinit(); | 65 | supported_targets: [][]u8, |
| 66 | 66 | }; | |
| 67 | var vt = try parser.parse(json_str); | ||
| 68 | defer vt.deinit(); | ||
| 69 | |||
| 70 | if (vt.root != .Object) { | ||
| 71 | std.log.warn("Failed at reading config, root isn't object.", .{}); | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | 67 | ||
| 75 | if (vt.root.Object.get("supported_targets")) |supported_targets| blk: { | 68 | var reader = std.json.reader(allocator, file.reader()); |
| 76 | if (supported_targets != .Array) { | 69 | defer reader.deinit(); |
| 77 | break :blk; | 70 | |
| 78 | } | 71 | const parsed = try std.json.parseFromTokenSourceLeaky( |
| 72 | ConfigJson, | ||
| 73 | allocator, | ||
| 74 | &reader, | ||
| 75 | .{ | ||
| 76 | .duplicate_field_behavior = .use_last, | ||
| 77 | .ignore_unknown_fields = true, | ||
| 78 | }, | ||
| 79 | ); | ||
| 80 | |||
| 81 | try self.supported_targets.ensureUnusedCapacity(parsed.supported_targets.len); | ||
| 82 | |||
| 83 | for (parsed.supported_targets) |target| { | ||
| 84 | const ct = CrossTarget.parse(.{ | ||
| 85 | .arch_os_abi = target, | ||
| 86 | }) catch |e| { | ||
| 87 | std.log.warn( | ||
| 88 | "Failed to parse '{s}' as a target string: {}", | ||
| 89 | .{ target, e }, | ||
| 90 | ); | ||
| 91 | continue; | ||
| 92 | }; | ||
| 79 | 93 | ||
| 80 | for (supported_targets.Array.items) |supported_target| { | 94 | const nti = NativeTargetInfo.detect(ct) catch |e| { |
| 81 | if (supported_target != .String) { | 95 | std.log.warn( |
| 82 | continue; | 96 | "Failed to detect NativeTargetInfo from '{s}': {}", |
| 83 | } | 97 | .{ target, e }, |
| 98 | ); | ||
| 99 | continue; | ||
| 100 | }; | ||
| 84 | 101 | ||
| 85 | const cross_target = try CrossTarget.parse(.{ | 102 | self.supported_targets.appendAssumeCapacity(nti.target); |
| 86 | .arch_os_abi = supported_target.String, | ||
| 87 | }); | ||
| 88 | const native_target_info = try NativeTargetInfo.detect(cross_target); | ||
| 89 | try self.supported_targets.append(native_target_info.target); | ||
| 90 | } | ||
| 91 | } | 103 | } |
| 92 | } | 104 | } |