// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT const std = @import("std"); pub const LatLonDir = enum { neg, pos, pub fn init(latlon: i64) LatLonDir { return if (latlon < 0) .neg else .pos; } }; pub const LatLonType = enum { latitude, longitude, pub inline fn toChar(self: @This(), dir: LatLonDir) u8 { return switch (comptime self) { .latitude => switch (dir) { .neg => 'S', .pos => 'N', }, .longitude => switch (dir) { .neg => 'W', .pos => 'E', }, }; } }; pub fn LatLon(comptime T: LatLonType) type { return struct { latlon: i64, offset: i64, granularity: i64, pub fn init(latlon: i64, offset: i64, granularity: i64) @This() { return .{ .latlon = latlon, .offset = offset, .granularity = granularity, }; } pub fn format(self: *const @This(), writer: *std.Io.Writer) std.Io.Writer.Error!void { const latlon_ = (self.granularity * self.latlon) + self.offset; const dir: LatLonDir = .init(latlon_); const latlon = @abs(latlon_); const d = @divFloor(latlon, 1000000000); const f = @mod(latlon, 1000000000); try writer.print("{d}.{d:0>9}°{c}", .{ d, f, T.toChar(dir), }); } pub fn float(self: *const @This(), comptime F: type) F { return @as(T, 0.000000001) * @as(F, @floatFromInt(self.offset + (self.granularity * self.latlon))); } }; }