draconisplusplus/src/config/config.h

202 lines
4.6 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 percentage = rfl::Validator<i8, rfl::Minimum<0>, rfl::Maximum<100>>;
using degrees = rfl::Validator<u16, rfl::Minimum<0>, rfl::Maximum<360>>;
struct Condition {
usize id;
rfl::Rename<"main", std::string> group;
std::string description;
rfl::Rename<"icon", std::string> icon_id;
};
struct Main {
f64 temp;
f64 temp_max;
f64 temp_min;
f64 feels_like;
isize pressure;
std::optional<isize> sea_level;
std::optional<isize> grnd_level;
percentage humidity;
};
struct Wind {
f64 speed;
degrees deg;
std::optional<f64> gust;
};
struct Precipitation {
rfl::Rename<"1h", f64> one_hour;
rfl::Rename<"3h", f64> three_hours;
};
struct Sys {
std::string country;
usize id;
usize sunrise;
usize sunset;
usize type;
};
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 {
isize timezone;
isize visibility;
Main main;
Clouds clouds;
rfl::Rename<"coord", Coords> coords;
std::optional<Precipitation> rain;
std::optional<Precipitation> snow;
std::vector<Condition> weather;
std::string base;
std::string name;
Sys sys;
usize cod;
usize dt;
usize id;
Wind wind;
};
using Location = std::variant<std::string, Coords>;
2024-05-31 22:59:00 -04:00
private:
2024-06-01 06:59:01 -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-07 04:23:11 -04:00
[[nodiscard]] WeatherOutput getWeatherInfo() const;
2024-06-01 06:59:01 -04:00
[[nodiscard]] const Location getLocation() const;
2024-06-07 04:23:11 -04:00
[[nodiscard]] const std::string getApiKey() const;
[[nodiscard]] const std::string getUnits() const;
2024-06-01 06:59:01 -04:00
};
struct WeatherImpl {
2024-06-02 06:03:21 -04:00
Weather::Location location;
2024-06-07 04:23:11 -04:00
std::string api_key;
std::string units;
2024-05-31 22:59:00 -04:00
2024-06-01 06:59:01 -04:00
static WeatherImpl from_class(const Weather& weather) noexcept;
2024-05-31 22:59:00 -04:00
2024-06-01 06:59:01 -04:00
[[nodiscard]] Weather to_class() const;
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-07 04:23:11 -04:00
[[nodiscard]] const std::string getName() const;
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
static GeneralImpl from_class(const General& general) noexcept;
[[nodiscard]] General to_class() const;
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-01 06:59:01 -04:00
[[nodiscard]] bool getEnabled() const;
};
struct NowPlayingImpl {
bool enabled;
static NowPlayingImpl from_class(const NowPlaying& now_playing) noexcept;
[[nodiscard]] NowPlaying to_class() const;
2024-05-31 22:59:00 -04:00
};
class Config {
private:
2024-06-01 06:59:01 -04:00
General m_General;
NowPlaying m_NowPlaying;
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);
static const Config& getInstance();
2024-05-31 22:59:00 -04:00
2024-06-01 06:59:01 -04:00
[[nodiscard]] const Weather getWeather() const;
[[nodiscard]] const General getGeneral() const;
[[nodiscard]] const NowPlaying getNowPlaying() const;
2024-05-31 22:59:00 -04:00
};
2024-06-01 06:59:01 -04:00
struct ConfigImpl {
General general;
NowPlaying now_playing;
Weather weather;
static ConfigImpl from_class(const Config& config) noexcept;
[[nodiscard]] Config to_class() const;
};
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>
: public CustomParser<ReaderType,
WriterType,
ProcessorsType,
Weather,
WeatherImpl> {};
template <class ReaderType, class WriterType, class ProcessorsType>
struct Parser<ReaderType, WriterType, General, ProcessorsType>
: public CustomParser<ReaderType,
WriterType,
ProcessorsType,
General,
GeneralImpl> {};
template <class ReaderType, class WriterType, class ProcessorsType>
struct Parser<ReaderType, WriterType, NowPlaying, ProcessorsType>
: public CustomParser<ReaderType,
WriterType,
ProcessorsType,
NowPlaying,
NowPlayingImpl> {};
template <class ReaderType, class WriterType, class ProcessorsType>
struct Parser<ReaderType, WriterType, Config, ProcessorsType>
: public CustomParser<ReaderType,
WriterType,
ProcessorsType,
Config,
ConfigImpl> {};
} // namespace rfl::parsing