diff --git a/src/http/handler.zig b/src/http/handler.zig index 016b5ec..9567844 100644 --- a/src/http/handler.zig +++ b/src/http/handler.zig @@ -1,7 +1,7 @@ const std = @import("std"); const httpz = @import("httpz"); const Cache = @import("../cache/Cache.zig"); -const WeatherProvider = @import("../weather/provider.zig").WeatherProvider; +const WeatherProvider = @import("../weather/Provider.zig"); const Resolver = @import("../location/resolver.zig").Resolver; const Location = @import("../location/resolver.zig").Location; const QueryParams = @import("query.zig").QueryParams; diff --git a/src/weather/MetNo.zig b/src/weather/MetNo.zig index d60a096..7abee31 100644 --- a/src/weather/MetNo.zig +++ b/src/weather/MetNo.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const weather_provider = @import("provider.zig"); +const WeatherProvider = @import("Provider.zig"); const types = @import("types.zig"); const MetNo = @This(); @@ -12,7 +12,7 @@ pub fn init(allocator: std.mem.Allocator) !MetNo { }; } -pub fn provider(self: *MetNo) weather_provider.WeatherProvider { +pub fn provider(self: *MetNo) WeatherProvider { return .{ .ptr = self, .vtable = &.{ @@ -107,11 +107,11 @@ fn parseMetNoResponse(allocator: std.mem.Allocator, location: []const u8, json: const instant = data.object.get("instant") orelse return error.InvalidResponse; const details = instant.object.get("details") orelse return error.InvalidResponse; - const temp_c = @as(f32, @floatCast(details.object.get("air_temperature").?.float)); - const humidity = @as(u8, @intFromFloat(details.object.get("relative_humidity").?.float)); + const temp_c: f32 = @floatCast(details.object.get("air_temperature").?.float); + const humidity: u8 = @intFromFloat(details.object.get("relative_humidity").?.float); const wind_speed_ms = details.object.get("wind_speed").?.float; - const wind_kph = @as(f32, @floatCast(wind_speed_ms * 3.6)); - const pressure_mb = @as(f32, @floatCast(details.object.get("air_pressure_at_sea_level").?.float)); + const wind_kph: f32 = @floatCast(wind_speed_ms * 3.6); + const pressure_mb: f32 = @floatCast(details.object.get("air_pressure_at_sea_level").?.float); // Get weather symbol const next_1h = data.object.get("next_1_hours"); @@ -173,7 +173,7 @@ fn symbolCodeToCondition(symbol: []const u8) []const u8 { fn degreeToDirection(deg: f32) []const u8 { const normalized = @mod(deg + 22.5, 360.0); - const idx = @as(usize, @intFromFloat(normalized / 45.0)); + const idx: usize = @intFromFloat(normalized / 45.0); const directions = [_][]const u8{ "N", "NE", "E", "SE", "S", "SW", "W", "NW" }; return directions[@min(idx, 7)]; } diff --git a/src/weather/Mock.zig b/src/weather/Mock.zig index 87566af..fcf48f4 100644 --- a/src/weather/Mock.zig +++ b/src/weather/Mock.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const weather_provider = @import("provider.zig"); +const WeatherProvider = @import("Provider.zig"); const types = @import("types.zig"); const Mock = @This(); @@ -14,7 +14,7 @@ pub fn init(allocator: std.mem.Allocator) !Mock { }; } -pub fn provider(self: *Mock) weather_provider.WeatherProvider { +pub fn provider(self: *Mock) WeatherProvider { return .{ .ptr = self, .vtable = &.{ diff --git a/src/weather/Provider.zig b/src/weather/Provider.zig new file mode 100644 index 0000000..fcd700f --- /dev/null +++ b/src/weather/Provider.zig @@ -0,0 +1,20 @@ +const std = @import("std"); +const types = @import("types.zig"); + +const WeatherProvider = @This(); + +ptr: *anyopaque, +vtable: *const VTable, + +pub const VTable = struct { + fetch: *const fn (ptr: *anyopaque, allocator: std.mem.Allocator, location: []const u8) anyerror!types.WeatherData, + deinit: *const fn (ptr: *anyopaque) void, +}; + +pub fn fetch(self: WeatherProvider, allocator: std.mem.Allocator, location: []const u8) !types.WeatherData { + return self.vtable.fetch(self.ptr, allocator, location); +} + +pub fn deinit(self: WeatherProvider) void { + self.vtable.deinit(self.ptr); +} diff --git a/src/weather/provider.zig b/src/weather/provider.zig deleted file mode 100644 index fc7a041..0000000 --- a/src/weather/provider.zig +++ /dev/null @@ -1,20 +0,0 @@ -const std = @import("std"); -const types = @import("types.zig"); - -pub const WeatherProvider = struct { - ptr: *anyopaque, - vtable: *const VTable, - - pub const VTable = struct { - fetch: *const fn (ptr: *anyopaque, allocator: std.mem.Allocator, location: []const u8) anyerror!types.WeatherData, - deinit: *const fn (ptr: *anyopaque) void, - }; - - pub fn fetch(self: WeatherProvider, allocator: std.mem.Allocator, location: []const u8) !types.WeatherData { - return self.vtable.fetch(self.ptr, allocator, location); - } - - pub fn deinit(self: WeatherProvider) void { - self.vtable.deinit(self.ptr); - } -};