wttr/src/weather/types.zig

138 lines
3.2 KiB
Zig

const std = @import("std");
/// Weather condition codes based on OpenWeatherMap standard
/// https://openweathermap.org/weather-conditions
pub const WeatherCode = enum(u16) {
// Thunderstorm group (2xx)
thunderstorm_light_rain = 200,
thunderstorm_rain = 201,
thunderstorm_heavy_rain = 202,
thunderstorm_light = 210,
thunderstorm = 211,
thunderstorm_heavy = 212,
thunderstorm_ragged = 221,
thunderstorm_light_drizzle = 230,
thunderstorm_drizzle = 231,
thunderstorm_heavy_drizzle = 232,
// Drizzle group (3xx)
drizzle_light = 300,
drizzle = 301,
drizzle_heavy = 302,
drizzle_rain_light = 310,
drizzle_rain = 311,
drizzle_rain_heavy = 312,
drizzle_shower_light = 313,
drizzle_shower = 314,
drizzle_shower_heavy = 321,
// Rain group (5xx)
rain_light = 500,
rain_moderate = 501,
rain_heavy = 502,
rain_very_heavy = 503,
rain_extreme = 504,
rain_freezing = 511,
rain_shower_light = 520,
rain_shower = 521,
rain_shower_heavy = 522,
rain_shower_ragged = 531,
// Snow group (6xx)
snow_light = 600,
snow = 601,
snow_heavy = 602,
sleet = 611,
sleet_shower_light = 612,
sleet_shower = 613,
rain_snow_light = 615,
rain_snow = 616,
snow_shower_light = 620,
snow_shower = 621,
snow_shower_heavy = 622,
// Atmosphere group (7xx)
mist = 701,
smoke = 711,
haze = 721,
dust_whirls = 731,
fog = 741,
sand = 751,
dust = 761,
ash = 762,
squall = 771,
tornado = 781,
// Clear group (800)
clear = 800,
// Clouds group (80x)
clouds_few = 801, // 11-25%
clouds_scattered = 802, // 25-50%
clouds_broken = 803, // 51-84%
clouds_overcast = 804, // 85-100%
unknown = 999,
};
pub const WeatherError = error{
LocationNotFound,
ApiError,
NetworkError,
};
pub const WeatherData = struct {
location: []const u8,
current: CurrentCondition,
forecast: []ForecastDay,
allocator: std.mem.Allocator,
pub fn deinit(self: WeatherData) void {
self.allocator.free(self.location);
self.allocator.free(self.current.condition);
self.allocator.free(self.current.wind_dir);
for (self.forecast) |day| {
self.allocator.free(day.date);
self.allocator.free(day.condition);
for (day.hourly) |hour| {
self.allocator.free(hour.time);
self.allocator.free(hour.condition);
}
self.allocator.free(day.hourly);
}
self.allocator.free(self.forecast);
}
};
pub const CurrentCondition = struct {
temp_c: f32,
temp_f: f32,
feels_like_c: f32,
feels_like_f: f32,
condition: []const u8,
weather_code: WeatherCode,
humidity: u8,
wind_kph: f32,
wind_dir: []const u8,
pressure_mb: f32,
precip_mm: f32,
};
pub const ForecastDay = struct {
date: []const u8,
max_temp_c: f32,
min_temp_c: f32,
condition: []const u8,
weather_code: WeatherCode,
hourly: []HourlyForecast,
};
pub const HourlyForecast = struct {
time: []const u8,
temp_c: f32,
feels_like_c: f32,
condition: []const u8,
weather_code: WeatherCode,
wind_kph: f32,
precip_mm: f32,
};