diff --git a/src/Config.zig b/src/Config.zig index 47e9dde..399f47d 100644 --- a/src/Config.zig +++ b/src/Config.zig @@ -8,7 +8,7 @@ const Config = @This(); var global_internal: Config = undefined; pub const global: *const Config = &global_internal; -pub fn load(allocator: std.mem.Allocator, reader: anytype) !void { +pub fn load(allocator: std.mem.Allocator, reader: anytype, err_writer: anytype) !void { var jread = std.json.Reader(1024, @TypeOf(reader)).init(allocator, reader); defer jread.deinit(); @@ -18,6 +18,8 @@ pub fn load(allocator: std.mem.Allocator, reader: anytype) !void { &jread, .{}, ); + + try global_internal.validate(err_writer); } pub fn loadDefault(allocator: std.mem.Allocator) void { @@ -30,6 +32,57 @@ pub fn destroy(allocator: std.mem.Allocator) void { _ = allocator; } +pub fn validate(self: Config, err_writer: anytype) !void { + var valid: bool = true; + + // zig fmt: off + if ( + self.controller.parking_posture.azimuth < ( + self.labjack.feedback_calibration.azimuth.minimum.angle + + self.controller.angle_offset.azimuth + ) or self.controller.parking_posture.azimuth > ( + self.labjack.feedback_calibration.azimuth.maximum.angle + + self.controller.angle_offset.azimuth + ) + ) { + // zig fmt: on + valid = false; + try err_writer.print( + "Config validation failed: Parking azimuth {d:.1} is outside of the valid azimuth range {d:.1} - {d:.1}\n", + .{ + self.controller.parking_posture.azimuth, + self.labjack.feedback_calibration.azimuth.minimum.angle + self.controller.angle_offset.azimuth, + self.labjack.feedback_calibration.azimuth.maximum.angle + self.controller.angle_offset.azimuth, + }, + ); + } + + // zig fmt: off + if ( + self.controller.parking_posture.elevation < ( + self.labjack.feedback_calibration.elevation.minimum.angle + + self.controller.angle_offset.elevation + ) or self.controller.parking_posture.elevation > ( + self.labjack.feedback_calibration.elevation.maximum.angle + + self.controller.angle_offset.elevation + ) + ) { + // zig fmt: on + valid = false; + try err_writer.print( + "Config validation failed: Parking elevation {d:.1} is outside of the valid elevation range {d:.1} - {d:.1}\n", + .{ + self.controller.parking_posture.elevation, + self.labjack.feedback_calibration.elevation.minimum.angle + self.controller.angle_offset.elevation, + self.labjack.feedback_calibration.elevation.maximum.angle + self.controller.angle_offset.elevation, + }, + ); + } + + if (!valid) + return error.InvalidConfig; +} + rotctl: RotControlConfig = .{ .listen_address = "127.0.0.1", .listen_port = 4533, diff --git a/src/main.zig b/src/main.zig index a7a40de..52e7e25 100644 --- a/src/main.zig +++ b/src/main.zig @@ -69,7 +69,7 @@ pub fn main() !u8 { }; defer conf_file.close(); - Config.load(allocator, conf_file.reader()) catch |err| { + Config.load(allocator, conf_file.reader(), std.io.getStdErr().writer()) catch |err| { log.err("Could not parse config file '{s}': {s}.", .{ confpath, @errorName(err) }); return 1; };