From bedf45b831baf2ded33d08c653f1ea18e2833777 Mon Sep 17 00:00:00 2001 From: pupbrained Date: Fri, 21 Jun 2024 03:03:42 -0400 Subject: [PATCH] :3333 --- src/config/config.cpp | 53 +------------- src/config/config.h | 80 ++++++---------------- src/config/weather.cpp | 41 +++-------- src/config/weather.h | 152 +++++++++++++++++------------------------ src/main.cpp | 11 ++- 5 files changed, 101 insertions(+), 236 deletions(-) diff --git a/src/config/config.cpp b/src/config/config.cpp index 8acaea9..9c7c2d7 100644 --- a/src/config/config.cpp +++ b/src/config/config.cpp @@ -1,55 +1,8 @@ +#include + #include "config.h" -// ------------- -// -- General -- -// ------------- -DEFINE_GETTER(General, const std::string, Name) - -General::General(std::string name) : m_Name(std::move(name)) {} - -fn GeneralImpl::from_class(const General& instance) noexcept -> GeneralImpl { - return { instance.getName() }; -} - -fn GeneralImpl::to_class() const -> General { return General { name }; } -// ------------- - -// ---------------- -// -- NowPlaying -- -// ---------------- -DEFINE_GETTER(NowPlaying, bool, Enabled) - -NowPlaying::NowPlaying(bool enabled) : m_Enabled(enabled) {} - -fn NowPlayingImpl::from_class(const NowPlaying& instance) noexcept -> NowPlayingImpl { - return { .enabled = instance.getEnabled() }; -} - -fn NowPlayingImpl::to_class() const -> NowPlaying { return NowPlaying { enabled.value_or(false) }; } -// ---------------- - -// ------------ -// -- Config -- -// ------------ -DEFINE_GETTER(Config, const General, General) -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)) {} - fn Config::getInstance() -> const Config& { - static const auto* INSTANCE = new Config(rfl::toml::load("./config.toml").value()); + static const Config* INSTANCE = new Config(rfl::toml::load("./config.toml").value()); return *INSTANCE; } - -fn ConfigImpl::from_class(const Config& instance) noexcept -> ConfigImpl { - return { - instance.getGeneral(), - instance.getNowPlaying(), - instance.getWeather(), - }; -} - -fn ConfigImpl::to_class() const -> Config { return { general, now_playing, weather }; } -// ------------ diff --git a/src/config/config.h b/src/config/config.h index 343827a..b44a25b 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -1,76 +1,36 @@ #pragma once +#include #include +#include +#include #include +#include -#include "util/macros.h" #include "weather.h" -// TODO: Make config values optional and supply defaults +using Location = std::variant; -class General { - private: - std::string m_Name; - - public: - explicit General(std::string name); - - [[nodiscard]] fn getName() const -> const std::string; +struct General { + rfl::Field<"name", std::string> name = "user"; }; -class NowPlaying { - private: - bool m_Enabled; - - public: - explicit NowPlaying(bool enabled); - - [[nodiscard]] fn getEnabled() const -> bool; +struct NowPlaying { + bool enabled = false; }; -class Config { - private: - General m_General; - NowPlaying m_NowPlaying; - Weather m_Weather; +struct Weather { + Location location; + std::string api_key; + std::string units; - 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); + fn getWeatherInfo() const -> WeatherOutput; +}; + +struct Config { + rfl::Field<"general", General> general = General { .name = "user" }; + rfl::Field<"now_playing", NowPlaying> now_playing = NowPlaying(); + rfl::Field<"weather", Weather> 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; - [[nodiscard]] fn getGeneral() const -> const General; - [[nodiscard]] fn getNowPlaying() const -> const NowPlaying; }; - -// reflect-cpp Stuff -DEF_IMPL(General, std::string name) -DEF_IMPL(NowPlaying, std::optional enabled) -DEF_IMPL(Config, General general; NowPlaying now_playing; Weather weather) - -namespace rfl::parsing { - template - struct Parser - : CustomParser {}; - - template - struct Parser - : CustomParser {}; - - template - struct Parser - : CustomParser {}; -} diff --git a/src/config/weather.cpp b/src/config/weather.cpp index 86ecdda..907abc2 100644 --- a/src/config/weather.cpp +++ b/src/config/weather.cpp @@ -5,27 +5,9 @@ #include "weather.h" +#include "config.h" #include "util/result.h" -using WeatherOutput = Weather::WeatherOutput; - -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 }; } - // Function to read cache from file fn ReadCacheFromFile() -> Result { std::ifstream ifs("/tmp/weather_cache.json"); @@ -91,6 +73,7 @@ fn MakeApiRequest(const std::string& url) -> Result { } fmt::println("Received response from API. Response size: {}", responseBuffer.size()); + fmt::println("Response: {}", responseBuffer); WeatherOutput output = rfl::json::read(responseBuffer).value(); @@ -104,10 +87,6 @@ fn MakeApiRequest(const std::string& url) -> Result { fn Weather::getWeatherInfo() const -> WeatherOutput { using namespace std::chrono; - const Location loc = m_Location; - const std::string apiKey = m_ApiKey; - const std::string units = m_Units; - // Check if cache is valid if (Result data = ReadCacheFromFile(); data.isOk()) { WeatherOutput dataVal = data.value(); @@ -126,24 +105,24 @@ fn Weather::getWeatherInfo() const -> WeatherOutput { WeatherOutput result; - if (holds_alternative(loc)) { - const std::string city = get(loc); + if (holds_alternative(location)) { + const std::string city = get(location); - const char* location = curl_easy_escape(nullptr, city.c_str(), static_cast(city.length())); + const char* loc = curl_easy_escape(nullptr, city.c_str(), static_cast(city.length())); - fmt::println("City: {}", location); + fmt::println("City: {}", loc); const std::string apiUrl = fmt::format( "https://api.openweathermap.org/data/2.5/" "weather?q={}&appid={}&units={}", - location, - apiKey, + loc, + api_key, units ); result = MakeApiRequest(apiUrl).value(); } else { - const auto [lat, lon] = get(loc); + const auto [lat, lon] = get(location); fmt::println("Coordinates: lat = {:.3f}, lon = {:.3f}", lat, lon); @@ -152,7 +131,7 @@ fn Weather::getWeatherInfo() const -> WeatherOutput { "weather?lat={:.3f}&lon={:.3f}&appid={}&units={}", lat, lon, - apiKey, + api_key, units ); diff --git a/src/config/weather.h b/src/config/weather.h index 60cfac3..d22cec2 100644 --- a/src/config/weather.h +++ b/src/config/weather.h @@ -4,99 +4,73 @@ #include #include #include -#include +#include -#include "util/macros.h" #include "util/numtypes.h" -class Weather { - public: - using degrees = rfl::Validator, rfl::Maximum<360>>; - using percentage = rfl::Validator, rfl::Maximum<100>>; +using degrees = rfl::Validator, rfl::Maximum<360>>; +using percentage = rfl::Validator, rfl::Maximum<100>>; - struct Condition { - std::string description; - std::string icon; - std::string main; - usize id; - }; - - struct Main { - f64 feels_like; - f64 temp; - f64 temp_max; - f64 temp_min; - isize pressure; - percentage humidity; - std::optional grnd_level; - std::optional sea_level; - }; - - struct Wind { - degrees deg; - f64 speed; - std::optional 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; - }; - - struct Coords { - double lat; - double lon; - }; - - struct WeatherOutput { - Clouds clouds; - isize timezone; - isize visibility; - Main main; - rfl::Rename<"coord", Coords> coords; - std::optional rain; - std::optional snow; - std::string base; - std::string name; - std::vector weather; - Sys sys; - usize cod; - usize dt; - usize id; - Wind wind; - }; - - using Location = std::variant; - - Weather(Location location, std::string api_key, std::string units); - - [[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; - - private: - Location m_Location; - std::string m_ApiKey; - std::string m_Units; +struct Condition { + std::string description; + std::string icon; + std::string main; + usize id; }; -DEF_IMPL(Weather, Weather::Location location; std::string api_key; std::string units) +struct Main { + f64 feels_like; + f64 temp; + f64 temp_max; + f64 temp_min; + isize pressure; + percentage humidity; + std::optional grnd_level; + std::optional sea_level; +}; -namespace rfl::parsing { - template - struct Parser - : CustomParser {}; -} +struct Wind { + degrees deg; + f64 speed; + std::optional 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; +}; + +struct Coords { + double lat; + double lon; +}; + +struct WeatherOutput { + Clouds clouds; + isize timezone; + isize visibility; + Main main; + rfl::Rename<"coord", Coords> coords; + std::optional rain; + std::optional snow; + std::string base; + std::string name; + std::vector weather; + Sys sys; + usize cod; + usize dt; + usize id; + Wind wind; +}; diff --git a/src/main.cpp b/src/main.cpp index a477347..12588ae 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -48,16 +48,14 @@ fn GetDate() -> string { } fn main() -> int { - using WeatherOutput = Weather::WeatherOutput; - const Config& config = Config::getInstance(); auto weatherFuture = - std::async(std::launch::async, [&config]() { return config.getWeather().getWeatherInfo(); }); + std::async(std::launch::async, [&config]() { return config.weather.get().getWeatherInfo(); }); auto osVersionFuture = std::async(std::launch::async, GetOSVersion); auto nowPlayingEnabledFuture = - std::async(std::launch::async, [&config]() { return config.getNowPlaying().getEnabled(); }); + std::async(std::launch::async, [&config]() { return config.now_playing.get().enabled; }); auto dateFuture = std::async(std::launch::async, GetDate); auto memInfoFuture = std::async(std::launch::async, GetMemInfo); @@ -66,8 +64,9 @@ fn main() -> int { const long temp = std::lround(json.main.temp); const std::string townName = json.name; - const char* version = osVersionFuture.get(); - const std::string name = config.getGeneral().getName(); + const char* version = osVersionFuture.get(); + + const std::string name = config.general.value().name.get(); const bool nowPlayingEnabled = nowPlayingEnabledFuture.get(); fmt::println("Hello {}!", name);