draconisplusplus/src/config/config.h

207 lines
4.9 KiB
C
Raw Normal View History

2024-06-07 04:23:11 -04:00
#pragma once
2024-06-01 06:59:01 -04:00
#include <fmt/core.h>
#include <rfl.hpp>
2024-06-05 19:04:53 -04:00
#include <rfl/toml.hpp>
2024-05-31 22:59:00 -04:00
#include <string>
2024-06-01 06:59:01 -04:00
#include <variant>
2024-05-31 22:59:00 -04:00
2024-06-07 04:23:11 -04:00
#include "util/numtypes.h"
2024-05-31 22:59:00 -04:00
2024-06-02 06:03:21 -04:00
class Weather {
public:
2024-06-07 04:23:11 -04:00
using degrees = rfl::Validator<u16, rfl::Minimum<0>, rfl::Maximum<360>>;
2024-06-09 20:14:51 -04:00
using percentage = rfl::Validator<i8, rfl::Minimum<0>, rfl::Maximum<100>>;
2024-06-07 04:23:11 -04:00
struct Condition {
std::string description;
2024-06-09 20:14:51 -04:00
std::string icon;
std::string main;
usize id;
2024-06-07 04:23:11 -04:00
};
struct Main {
2024-06-09 20:14:51 -04:00
f64 feels_like;
f64 temp;
f64 temp_max;
f64 temp_min;
isize pressure;
percentage humidity;
2024-06-07 04:23:11 -04:00
std::optional<isize> grnd_level;
2024-06-09 20:14:51 -04:00
std::optional<isize> sea_level;
2024-06-07 04:23:11 -04:00
};
struct Wind {
2024-06-09 20:14:51 -04:00
degrees deg;
f64 speed;
2024-06-07 04:23:11 -04:00
std::optional<f64> gust;
};
struct Precipitation {
rfl::Rename<"1h", f64> one_hour;
rfl::Rename<"3h", f64> three_hours;
};
struct Sys {
std::string country;
2024-06-09 20:14:51 -04:00
usize id;
usize sunrise;
usize sunset;
usize type;
2024-06-07 04:23:11 -04:00
};
struct Clouds {
percentage all;
};
2024-06-02 06:03:21 -04:00
struct Coords {
double lat;
double lon;
};
2024-06-01 06:59:01 -04:00
2024-06-07 04:23:11 -04:00
struct WeatherOutput {
2024-06-09 20:14:51 -04:00
Clouds clouds;
Main main;
Sys sys;
Wind wind;
isize timezone;
isize visibility;
2024-06-07 04:23:11 -04:00
rfl::Rename<"coord", Coords> coords;
std::optional<Precipitation> rain;
std::optional<Precipitation> snow;
2024-06-09 20:14:51 -04:00
std::string base;
std::string name;
std::vector<Condition> weather;
usize cod;
usize dt;
usize id;
2024-06-07 04:23:11 -04:00
};
using Location = std::variant<std::string, Coords>;
2024-05-31 22:59:00 -04:00
private:
2024-06-09 20:14:51 -04:00
Location m_Location;
2024-06-07 04:23:11 -04:00
std::string m_ApiKey;
std::string m_Units;
2024-05-31 22:59:00 -04:00
public:
2024-06-07 04:23:11 -04:00
Weather(Location location, std::string api_key, std::string units);
2024-06-01 06:59:01 -04:00
2024-06-09 20:14:51 -04:00
[[nodiscard]] fn getWeatherInfo() const -> WeatherOutput;
[[nodiscard]] fn getLocation() const -> const Location;
[[nodiscard]] fn getApiKey() const -> const std::string;
[[nodiscard]] fn getUnits() const -> const std::string;
2024-06-01 06:59:01 -04:00
};
struct WeatherImpl {
2024-06-02 06:03:21 -04:00
Weather::Location location;
2024-06-09 20:14:51 -04:00
std::string api_key;
std::string units;
2024-05-31 22:59:00 -04:00
2024-06-09 20:14:51 -04:00
static fn from_class(const Weather& weather) noexcept -> WeatherImpl;
2024-05-31 22:59:00 -04:00
2024-06-09 20:14:51 -04:00
[[nodiscard]] fn to_class() const -> Weather;
2024-05-31 22:59:00 -04:00
};
class General {
private:
2024-06-07 04:23:11 -04:00
std::string m_Name;
2024-05-31 22:59:00 -04:00
public:
2024-06-07 04:23:11 -04:00
General(std::string name);
2024-05-31 22:59:00 -04:00
2024-06-09 20:14:51 -04:00
[[nodiscard]] fn getName() const -> const std::string;
2024-06-01 06:59:01 -04:00
};
struct GeneralImpl {
2024-06-07 04:23:11 -04:00
std::string name;
2024-06-01 06:59:01 -04:00
2024-06-09 20:14:51 -04:00
static fn from_class(const General& general) noexcept -> GeneralImpl;
2024-06-01 06:59:01 -04:00
2024-06-09 20:14:51 -04:00
[[nodiscard]] fn to_class() const -> General;
2024-05-31 22:59:00 -04:00
};
class NowPlaying {
private:
2024-06-01 06:59:01 -04:00
bool m_Enabled;
2024-05-31 22:59:00 -04:00
public:
2024-06-02 06:03:21 -04:00
NowPlaying(bool enabled);
2024-05-31 22:59:00 -04:00
2024-06-09 20:14:51 -04:00
[[nodiscard]] fn getEnabled() const -> bool;
2024-06-01 06:59:01 -04:00
};
struct NowPlayingImpl {
bool enabled;
2024-06-09 20:14:51 -04:00
static fn from_class(const NowPlaying& now_playing
) noexcept -> NowPlayingImpl;
2024-06-01 06:59:01 -04:00
2024-06-09 20:14:51 -04:00
[[nodiscard]] fn to_class() const -> NowPlaying;
2024-05-31 22:59:00 -04:00
};
class Config {
private:
2024-06-09 20:14:51 -04:00
General m_General;
2024-06-01 06:59:01 -04:00
NowPlaying m_NowPlaying;
2024-06-09 20:14:51 -04:00
Weather m_Weather;
2024-05-31 22:59:00 -04:00
2024-06-01 06:59:01 -04:00
public:
Config(General general, NowPlaying now_playing, Weather weather);
2024-06-09 20:14:51 -04:00
static fn getInstance() -> const Config&;
2024-05-31 22:59:00 -04:00
2024-06-09 20:14:51 -04:00
[[nodiscard]] fn getWeather() const -> const Weather;
[[nodiscard]] fn getGeneral() const -> const General;
[[nodiscard]] fn getNowPlaying() const -> const NowPlaying;
2024-05-31 22:59:00 -04:00
};
2024-06-01 06:59:01 -04:00
struct ConfigImpl {
2024-06-09 20:14:51 -04:00
General general;
2024-06-01 06:59:01 -04:00
NowPlaying now_playing;
2024-06-09 20:14:51 -04:00
Weather weather;
2024-06-01 06:59:01 -04:00
2024-06-09 20:14:51 -04:00
static fn from_class(const Config& config) noexcept -> ConfigImpl;
2024-06-01 06:59:01 -04:00
2024-06-09 20:14:51 -04:00
[[nodiscard]] fn to_class() const -> Config;
2024-06-01 06:59:01 -04:00
};
2024-06-02 06:03:21 -04:00
// Parsers for Config classes
2024-06-01 06:59:01 -04:00
namespace rfl::parsing {
template <class ReaderType, class WriterType, class ProcessorsType>
struct Parser<ReaderType, WriterType, Weather, ProcessorsType>
2024-06-09 20:14:51 -04:00
: public CustomParser<
ReaderType,
WriterType,
ProcessorsType,
Weather,
WeatherImpl> {};
2024-06-01 06:59:01 -04:00
template <class ReaderType, class WriterType, class ProcessorsType>
struct Parser<ReaderType, WriterType, General, ProcessorsType>
2024-06-09 20:14:51 -04:00
: public CustomParser<
ReaderType,
WriterType,
ProcessorsType,
General,
GeneralImpl> {};
2024-06-01 06:59:01 -04:00
template <class ReaderType, class WriterType, class ProcessorsType>
struct Parser<ReaderType, WriterType, NowPlaying, ProcessorsType>
2024-06-09 20:14:51 -04:00
: public CustomParser<
ReaderType,
WriterType,
ProcessorsType,
NowPlaying,
NowPlayingImpl> {};
2024-06-01 06:59:01 -04:00
template <class ReaderType, class WriterType, class ProcessorsType>
struct Parser<ReaderType, WriterType, Config, ProcessorsType>
2024-06-09 20:14:51 -04:00
: public CustomParser<
ReaderType,
WriterType,
ProcessorsType,
Config,
ConfigImpl> {};
2024-06-01 06:59:01 -04:00
} // namespace rfl::parsing