This commit is contained in:
Mars 2024-06-16 00:13:15 -04:00
parent f668a2eb4c
commit 50083503cf
Signed by: pupbrained
GPG key ID: 0FF5B8826803F895
224 changed files with 16150 additions and 19488 deletions

View file

@ -1,26 +1,5 @@
#include "config.h"
// -------------
// -- Weather --
// -------------
DEFINE_GETTER(Weather, const Weather::Location, Location)
DEFINE_GETTER(Weather, const std::string, ApiKey)
DEFINE_GETTER(Weather, const std::string, Units)
Weather::Weather(Location location, std::string api_key, std::string units)
: m_Location(std::move(location)), m_ApiKey(std::move(api_key)), m_Units(std::move(units)) {}
fn WeatherImpl::from_class(const Weather& weather) noexcept -> WeatherImpl {
return {
.location = weather.getLocation(),
.api_key = weather.getApiKey(),
.units = weather.getUnits(),
};
}
fn WeatherImpl::to_class() const -> Weather { return {location, api_key, units}; }
// ------------
// -------------
// -- General --
// -------------
@ -28,9 +7,11 @@ DEFINE_GETTER(General, const std::string, Name)
General::General(std::string name) : m_Name(std::move(name)) {}
fn GeneralImpl::from_class(const General& general) -> GeneralImpl { return {general.getName()}; }
fn GeneralImpl::from_class(const General& instance) noexcept -> GeneralImpl {
return { instance.getName() };
}
fn GeneralImpl::to_class() const -> General { return {name}; }
fn GeneralImpl::to_class() const -> General { return General { name }; }
// -------------
// ----------------
@ -40,11 +21,11 @@ DEFINE_GETTER(NowPlaying, bool, Enabled)
NowPlaying::NowPlaying(bool enabled) : m_Enabled(enabled) {}
fn NowPlayingImpl::from_class(const NowPlaying& now_playing) -> NowPlayingImpl {
return {.enabled = now_playing.getEnabled()};
fn NowPlayingImpl::from_class(const NowPlaying& instance) noexcept -> NowPlayingImpl {
return { .enabled = instance.getEnabled() };
}
fn NowPlayingImpl::to_class() const -> NowPlaying { return {enabled.value_or(false)}; }
fn NowPlayingImpl::to_class() const -> NowPlaying { return NowPlaying { enabled.value_or(false) }; }
// ----------------
// ------------
@ -55,20 +36,20 @@ DEFINE_GETTER(Config, const NowPlaying, NowPlaying)
DEFINE_GETTER(Config, const Weather, Weather)
Config::Config(General general, NowPlaying now_playing, Weather weather)
: m_General(std::move(general)), m_NowPlaying(now_playing), m_Weather(std::move(weather)) {}
: m_General(std::move(general)), m_NowPlaying(now_playing), m_Weather(std::move(weather)) {}
fn Config::getInstance() -> const Config& {
static const auto* INSTANCE = new Config(rfl::toml::load<Config>("./config.toml").value());
return *INSTANCE;
}
fn ConfigImpl::from_class(const Config& config) -> ConfigImpl {
fn ConfigImpl::from_class(const Config& instance) noexcept -> ConfigImpl {
return {
.general = config.getGeneral(),
.now_playing = config.getNowPlaying(),
.weather = config.getWeather(),
instance.getGeneral(),
instance.getNowPlaying(),
instance.getWeather(),
};
}
fn ConfigImpl::to_class() const -> Config { return {general, now_playing, weather}; }
fn ConfigImpl::to_class() const -> Config { return { general, now_playing, weather }; }
// ------------

View file

@ -1,8 +1,6 @@
#pragma once
#include <fmt/core.h>
#include <rfl.hpp>
#include <rfl/toml.hpp>
#include <string>
#include "util/macros.h"
@ -15,7 +13,7 @@ class General {
std::string m_Name;
public:
General(std::string name);
explicit General(std::string name);
[[nodiscard]] fn getName() const -> const std::string;
};
@ -25,7 +23,7 @@ class NowPlaying {
bool m_Enabled;
public:
NowPlaying(bool enabled);
explicit NowPlaying(bool enabled);
[[nodiscard]] fn getEnabled() const -> bool;
};
@ -37,8 +35,20 @@ class Config {
Weather m_Weather;
public:
/**
* @brief Creates a new Config instance.
*
* @param general The general section of the configuration.
* @param now_playing The now playing section of the configuration.
* @param weather The weather section of the configuration.
*/
Config(General general, NowPlaying now_playing, Weather weather);
/**
* @brief Gets the current (read-only) configuration.
*
* @return The current Config instance.
*/
static fn getInstance() -> const Config&;
[[nodiscard]] fn getWeather() const -> const Weather;
@ -47,20 +57,20 @@ class Config {
};
// reflect-cpp Stuff
DEF_IMPL(General, general, std::string name)
DEF_IMPL(NowPlaying, now_playing, std::optional<bool> enabled)
DEF_IMPL(Config, config, General general; NowPlaying now_playing; Weather weather)
DEF_IMPL(General, std::string name)
DEF_IMPL(NowPlaying, std::optional<bool> enabled)
DEF_IMPL(Config, General general; NowPlaying now_playing; Weather weather)
namespace rfl::parsing {
template <class ReaderType, class WriterType, class ProcessorsType>
struct Parser<ReaderType, WriterType, General, ProcessorsType>
: public CustomParser<ReaderType, WriterType, ProcessorsType, General, GeneralImpl> {};
: 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> {};
: 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> {};
: CustomParser<ReaderType, WriterType, ProcessorsType, Config, ConfigImpl> {};
}

View file

@ -7,6 +7,23 @@
#include "util/result.h"
DEFINE_GETTER(Weather, const Weather::Location, Location)
DEFINE_GETTER(Weather, const std::string, ApiKey)
DEFINE_GETTER(Weather, const std::string, Units)
Weather::Weather(Location location, std::string api_key, std::string units)
: m_Location(std::move(location)), m_ApiKey(std::move(api_key)), m_Units(std::move(units)) {}
fn WeatherImpl::from_class(const Weather& weather) noexcept -> WeatherImpl {
return {
weather.getLocation(),
weather.getApiKey(),
weather.getUnits(),
};
}
fn WeatherImpl::to_class() const -> Weather { return { location, api_key, units }; }
using WeatherOutput = Weather::WeatherOutput;
// Function to read cache from file
@ -49,8 +66,7 @@ fn WriteCacheToFile(const WeatherOutput& data) -> Result<> {
return Ok();
}
fn WriteCallback(void* contents, size_t size, size_t nmemb, std::string* str)
-> size_t {
fn WriteCallback(void* contents, size_t size, size_t nmemb, std::string* str) -> size_t {
size_t totalSize = size * nmemb;
str->append(static_cast<char*>(contents), totalSize);
return totalSize;
@ -71,17 +87,12 @@ fn MakeApiRequest(const std::string& url) -> Result<WeatherOutput> {
curl_easy_cleanup(curl);
if (res != CURLE_OK) {
return Error(fmt::format(
"Failed to perform cURL request: {}", curl_easy_strerror(res)
));
return Error(fmt::format("Failed to perform cURL request: {}", curl_easy_strerror(res)));
}
fmt::println(
"Received response from API. Response size: {}", responseBuffer.size()
);
fmt::println("Received response from API. Response size: {}", responseBuffer.size());
WeatherOutput output =
rfl::json::read<WeatherOutput>(responseBuffer).value();
WeatherOutput output = rfl::json::read<WeatherOutput>(responseBuffer).value();
return Ok(output); // Return an empty result for now
}
@ -118,18 +129,16 @@ fn Weather::getWeatherInfo() const -> WeatherOutput {
if (holds_alternative<std::string>(loc)) {
const std::string city = get<std::string>(loc);
const char* location = curl_easy_escape(
nullptr, city.c_str(), static_cast<int>(city.length())
);
const char* location = curl_easy_escape(nullptr, city.c_str(), static_cast<int>(city.length()));
fmt::println("City: {}", location);
const std::string apiUrl = fmt::format(
"https://api.openweathermap.org/data/2.5/"
"weather?q={}&appid={}&units={}",
location,
apiKey,
units
"https://api.openweathermap.org/data/2.5/"
"weather?q={}&appid={}&units={}",
location,
apiKey,
units
);
result = MakeApiRequest(apiUrl).value();
@ -139,12 +148,12 @@ fn Weather::getWeatherInfo() const -> WeatherOutput {
fmt::println("Coordinates: lat = {:.3f}, lon = {:.3f}", lat, lon);
const std::string apiUrl = fmt::format(
"https://api.openweathermap.org/data/2.5/"
"weather?lat={:.3f}&lon={:.3f}&appid={}&units={}",
lat,
lon,
apiKey,
units
"https://api.openweathermap.org/data/2.5/"
"weather?lat={:.3f}&lon={:.3f}&appid={}&units={}",
lat,
lon,
apiKey,
units
);
result = MakeApiRequest(apiUrl).value();

View file

@ -94,23 +94,10 @@ class Weather {
[[nodiscard]] fn getUnits() const -> const std::string;
};
struct WeatherImpl {
Weather::Location location;
std::string api_key;
std::string units;
static fn from_class(const Weather& weather) noexcept -> WeatherImpl;
[[nodiscard]] fn to_class() const -> Weather;
};
DEF_IMPL(Weather, Weather::Location location; std::string api_key; std::string units)
namespace rfl::parsing {
template <class ReaderType, class WriterType, class ProcessorsType>
struct Parser<ReaderType, WriterType, Weather, ProcessorsType>
: public CustomParser<
ReaderType,
WriterType,
ProcessorsType,
Weather,
WeatherImpl> {};
: CustomParser<ReaderType, WriterType, ProcessorsType, Weather, WeatherImpl> {};
}