i should sleep
This commit is contained in:
parent
791e237470
commit
b366a7ee63
10 changed files with 341 additions and 109 deletions
|
@ -3,19 +3,93 @@
|
|||
#include <toml++/toml.h>
|
||||
#include <unistd.h>
|
||||
|
||||
Weather make_weather(toml::node_view<toml::node> location,
|
||||
const char* api_key,
|
||||
const char* units) {
|
||||
return location.is_string()
|
||||
? Weather(location.value_or(""), api_key, units)
|
||||
: Weather(std::make_tuple(location["lat"].value_or(0.0),
|
||||
location["lon"].value_or(0.0)),
|
||||
api_key, units);
|
||||
#define DEFINE_GETTER(class_name, type, name) \
|
||||
type class_name::get##name() const { \
|
||||
return m_##name; \
|
||||
}
|
||||
|
||||
DEFINE_GETTER(Config, const General, General)
|
||||
DEFINE_GETTER(Config, const NowPlaying, NowPlaying)
|
||||
DEFINE_GETTER(Config, const Weather, Weather)
|
||||
DEFINE_GETTER(General, const string, Name)
|
||||
DEFINE_GETTER(NowPlaying, bool, Enabled)
|
||||
DEFINE_GETTER(Weather, const Location, Location)
|
||||
DEFINE_GETTER(Weather, const string, ApiKey)
|
||||
DEFINE_GETTER(Weather, const string, Units)
|
||||
|
||||
const Config& Config::getInstance() {
|
||||
static const Config& INSTANCE =
|
||||
*new Config(toml::parse_file("./config.toml"));
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
Config::Config(toml::table toml)
|
||||
: m_general(toml["general"]["name"].value_or(getlogin())),
|
||||
m_now_playing(toml["now_playing"]["enable"].value_or(false)),
|
||||
m_weather(make_weather(toml["weather"]["location"],
|
||||
toml["weather"]["api_key"].value_or(""),
|
||||
toml["weather"]["units"].value_or("metric"))) {}
|
||||
: m_General(toml["general"]["name"].value_or(getlogin())),
|
||||
m_NowPlaying(toml["now_playing"]["enable"].value_or(false)),
|
||||
m_Weather([location = toml["weather"]["location"],
|
||||
apiKey = toml["weather"]["api_key"].value_or(""),
|
||||
units = toml["weather"]["units"].value_or("metric")] {
|
||||
return location.is_string()
|
||||
? Weather(location.value_or(""), apiKey, units)
|
||||
: Weather(
|
||||
Coords {
|
||||
.lat = location["lat"].value_or(0.0),
|
||||
.lon = location["lon"].value_or(0.0),
|
||||
},
|
||||
apiKey, units);
|
||||
}()) {}
|
||||
|
||||
Config::Config(General general, NowPlaying now_playing, Weather weather)
|
||||
: m_General(std::move(general)),
|
||||
m_NowPlaying(now_playing),
|
||||
m_Weather(std::move(weather)) {}
|
||||
|
||||
General::General(string name) : m_Name(std::move(name)) {}
|
||||
|
||||
NowPlaying::NowPlaying(bool enable) : m_Enabled(enable) {}
|
||||
|
||||
Weather::Weather(Location location, string api_key, string units)
|
||||
: m_Location(std::move(location)),
|
||||
m_ApiKey(std::move(api_key)),
|
||||
m_Units(std::move(units)) {}
|
||||
|
||||
WeatherImpl WeatherImpl::from_class(const Weather& weather) noexcept {
|
||||
return {
|
||||
.location = weather.getLocation(),
|
||||
.api_key = weather.getApiKey(),
|
||||
.units = weather.getUnits(),
|
||||
};
|
||||
}
|
||||
|
||||
Weather WeatherImpl::to_class() const {
|
||||
return {location, api_key, units};
|
||||
}
|
||||
|
||||
GeneralImpl GeneralImpl::from_class(const General& general) noexcept {
|
||||
return {general.getName()};
|
||||
}
|
||||
|
||||
General GeneralImpl::to_class() const {
|
||||
return {name};
|
||||
}
|
||||
|
||||
NowPlayingImpl NowPlayingImpl::from_class(
|
||||
const NowPlaying& now_playing) noexcept {
|
||||
return {.enabled = now_playing.getEnabled()};
|
||||
}
|
||||
|
||||
NowPlaying NowPlayingImpl::to_class() const {
|
||||
return {enabled};
|
||||
}
|
||||
|
||||
ConfigImpl ConfigImpl::from_class(const Config& config) noexcept {
|
||||
return {
|
||||
.general = config.getGeneral(),
|
||||
.now_playing = config.getNowPlaying(),
|
||||
.weather = config.getWeather(),
|
||||
};
|
||||
}
|
||||
|
||||
Config ConfigImpl::to_class() const {
|
||||
return {general, now_playing, weather};
|
||||
}
|
||||
|
|
|
@ -1,63 +1,140 @@
|
|||
#pragma once
|
||||
|
||||
#include <fmt/core.h>
|
||||
#include <toml++/toml.h>
|
||||
#include <unistd.h>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <toml++/impl/parser.hpp>
|
||||
#include <variant>
|
||||
|
||||
using std::string;
|
||||
|
||||
typedef std::tuple<double, double> Coords;
|
||||
typedef std::variant<string, Coords> Location;
|
||||
struct Coords {
|
||||
double lat;
|
||||
double lon;
|
||||
};
|
||||
|
||||
using Location = std::variant<string, Coords>;
|
||||
|
||||
class Weather {
|
||||
private:
|
||||
Location m_location;
|
||||
string m_api_key;
|
||||
string m_units;
|
||||
Location m_Location;
|
||||
string m_ApiKey;
|
||||
string m_Units;
|
||||
|
||||
public:
|
||||
Weather(string city, string api_key, string units)
|
||||
: m_location(city), m_api_key(api_key), m_units(units) {}
|
||||
Weather(Location location, string api_key, string units);
|
||||
|
||||
Weather(Coords coords, string api_key, string units)
|
||||
: m_location(coords), m_api_key(api_key), m_units(units) {}
|
||||
[[nodiscard]] const Location getLocation() const;
|
||||
[[nodiscard]] const string getApiKey() const;
|
||||
[[nodiscard]] const string getUnits() const;
|
||||
};
|
||||
|
||||
inline Location get_location() { return m_location; }
|
||||
inline string get_api_key() { return m_api_key; }
|
||||
inline string get_units() { return m_units; }
|
||||
struct WeatherImpl {
|
||||
Location location;
|
||||
string api_key;
|
||||
string units;
|
||||
|
||||
static WeatherImpl from_class(const Weather& weather) noexcept;
|
||||
|
||||
[[nodiscard]] Weather to_class() const;
|
||||
};
|
||||
|
||||
class General {
|
||||
private:
|
||||
string m_name;
|
||||
string m_Name;
|
||||
|
||||
public:
|
||||
General(string name) { this->m_name = name; }
|
||||
General(string name);
|
||||
|
||||
inline string get_name() { return m_name; }
|
||||
[[nodiscard]] const string getName() const;
|
||||
};
|
||||
|
||||
struct GeneralImpl {
|
||||
string name;
|
||||
|
||||
static GeneralImpl from_class(const General& general) noexcept;
|
||||
|
||||
[[nodiscard]] General to_class() const;
|
||||
};
|
||||
|
||||
class NowPlaying {
|
||||
private:
|
||||
bool m_enable;
|
||||
bool m_Enabled;
|
||||
|
||||
public:
|
||||
NowPlaying(bool enable) { this->m_enable = enable; }
|
||||
NowPlaying(bool enable);
|
||||
|
||||
inline bool get_enabled() { return m_enable; }
|
||||
[[nodiscard]] bool getEnabled() const;
|
||||
};
|
||||
|
||||
struct NowPlayingImpl {
|
||||
bool enabled;
|
||||
|
||||
static NowPlayingImpl from_class(const NowPlaying& now_playing) noexcept;
|
||||
|
||||
[[nodiscard]] NowPlaying to_class() const;
|
||||
};
|
||||
|
||||
class Config {
|
||||
private:
|
||||
General m_general;
|
||||
NowPlaying m_now_playing;
|
||||
Weather m_weather;
|
||||
General m_General;
|
||||
NowPlaying m_NowPlaying;
|
||||
Weather m_Weather;
|
||||
|
||||
public:
|
||||
Config(toml::table toml);
|
||||
|
||||
~Config();
|
||||
public:
|
||||
Config(General general, NowPlaying now_playing, Weather weather);
|
||||
|
||||
inline Weather get_weather() { return m_weather; }
|
||||
inline General get_general() { return m_general; }
|
||||
inline NowPlaying get_now_playing() { return m_now_playing; }
|
||||
static const Config& getInstance();
|
||||
|
||||
[[nodiscard]] const Weather getWeather() const;
|
||||
[[nodiscard]] const General getGeneral() const;
|
||||
[[nodiscard]] const NowPlaying getNowPlaying() const;
|
||||
};
|
||||
|
||||
struct ConfigImpl {
|
||||
General general;
|
||||
NowPlaying now_playing;
|
||||
Weather weather;
|
||||
|
||||
static ConfigImpl from_class(const Config& config) noexcept;
|
||||
|
||||
[[nodiscard]] Config to_class() const;
|
||||
};
|
||||
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue