summaryrefslogtreecommitdiffstats
path: root/src/item.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/item.hpp')
-rw-r--r--src/item.hpp250
1 files changed, 0 insertions, 250 deletions
diff --git a/src/item.hpp b/src/item.hpp
deleted file mode 100644
index 0fe27a8..0000000
--- a/src/item.hpp
+++ /dev/null
@@ -1,250 +0,0 @@
-#pragma once
-
-#include <cassert>
-#include <optional>
-#include <string>
-#include <variant>
-#include <vector>
-
-namespace rttt {
-
-namespace hackernews {
-using ItemId = int;
-using ItemIds = std::vector<ItemId>;
-
-struct Story {
- std::string by = "";
- int descendants = 0;
- ItemId id = 0;
- ItemIds kids;
- int score = 0;
- uint64_t time = 0;
- std::string text = "";
- std::string title = "";
- std::string url = "";
- std::string domain = "";
-};
-
-struct Comment {
- std::string by = "";
- ItemId id = 0;
- ItemIds kids;
- ItemId parent = 0;
- std::string text = "";
- uint64_t time = 0;
-};
-} // namespace hackernews
-
-namespace rss {
-struct Story {
- std::string by = "";
- uint64_t time = 0;
- std::string text = "";
- std::string title = "";
- std::string domain = "";
- std::string url = "";
-
- int id;
- std::string key = "";
-};
-} // namespace rss
-
-// Forward declarations
-namespace reddit {
-struct Story;
-struct Comment;
-} // namespace reddit
-
-struct unknown_type {};
-
-using ItemVariant =
- std::variant<reddit::Comment, reddit::Story, hackernews::Comment,
- hackernews::Story, rss::Story, rttt::unknown_type>;
-
-namespace reddit {
-struct Story {
- std::string by = "";
- int descendants = 0;
- int score = 0;
- int vote = 0;
- uint64_t time = 0;
- std::string text = "";
- std::string title = "";
- std::string domain = "";
- std::string url = "";
- std::string fullname = "";
-
- std::vector<ItemVariant> kids;
-
- std::string id = "";
-};
-
-struct Comment {
- std::string by = "";
- std::string text = "";
- std::string id = "";
- std::string fullname = "";
- uint64_t time = 0;
- int score = 0;
- int vote = 0;
-
- std::vector<ItemVariant> kids;
-};
-
-} // namespace reddit
-
-inline std::optional<std::string> try_id_string(const ItemVariant &item) {
- if (std::holds_alternative<reddit::Story>(item)) {
- auto value = std::get<reddit::Story>(item);
- if (value.id.empty())
- return std::nullopt;
- return value.id;
- }
- if (std::holds_alternative<reddit::Comment>(item)) {
- auto value = std::get<reddit::Comment>(item);
- if (value.id.empty())
- return std::nullopt;
- return value.id;
- }
- if (std::holds_alternative<hackernews::Story>(item)) {
- auto value = std::get<hackernews::Story>(item);
- if (value.id == 0)
- return std::nullopt;
- return std::to_string(value.id);
- }
- if (std::holds_alternative<hackernews::Comment>(item)) {
- auto value = std::get<hackernews::Comment>(item);
- if (value.id == 0)
- return std::nullopt;
- return std::to_string(value.id);
- }
- if (std::holds_alternative<rss::Story>(item)) {
- auto value = std::get<rss::Story>(item);
- if (value.id == 0)
- return std::nullopt;
- return std::to_string(value.id);
- }
- return std::nullopt;
-}
-
-inline std::optional<uint64_t> try_time(const ItemVariant &item) {
- std::optional<uint64_t> opt;
- std::visit(
- [&opt](const auto &data) {
- if constexpr (!std::is_same<const rttt::unknown_type &,
- decltype(data)>::value) {
- opt = data.time;
- }
- },
- item);
- return opt;
-}
-
-std::optional<int> try_vote(const ItemVariant &item) {
- std::optional<int> opt;
- std::visit(
- [&opt](const auto &data) {
- // TODO: make a constexpr is_reddit template function
- if constexpr (std::is_same<const rttt::reddit::Story &,
- decltype(data)>::value ||
- std::is_same<const rttt::reddit::Comment &,
- decltype(data)>::value) {
- opt = data.vote;
- }
- },
- item);
- return opt;
-}
-
-std::optional<std::string> try_fullname(const ItemVariant &item) {
- std::optional<std::string> opt;
- std::visit(
- [&opt](const auto &data) {
- // TODO: make a constexpr is_reddit template function
- if constexpr (std::is_same<const rttt::reddit::Story &,
- decltype(data)>::value ||
- std::is_same<const rttt::reddit::Comment &,
- decltype(data)>::value) {
- opt = data.fullname;
- }
- },
- item);
- return opt;
-}
-
-template <typename C, typename = void> struct has_url : std::false_type {};
-
-template <typename C>
-struct has_url<
- C, typename std::enable_if<std::is_same<decltype(std::declval<C>().url),
- std::string>::value>::type>
- : std::true_type {};
-
-inline std::optional<std::string> try_url(const ItemVariant &item) {
- std::optional<std::string> opt;
- std::visit(
- [&opt](const auto &data) {
- if constexpr (has_url<decltype(data)>::value)
- opt = data.url;
- },
- item);
- return opt;
-}
-
-template <typename C, typename = void> struct has_title : std::false_type {};
-
-template <typename C>
-struct has_title<
- C, typename std::enable_if<std::is_same<decltype(std::declval<C>().title),
- std::string>::value>::type>
- : std::true_type {};
-
-inline std::optional<std::string> try_title(const ItemVariant &item) {
- std::optional<std::string> opt;
- std::visit(
- [&opt](const auto &data) {
- if constexpr (has_title<decltype(data)>::value)
- opt = data.title;
- },
- item);
- return opt;
-}
-
-inline std::optional<std::string> try_text(const ItemVariant &item) {
- std::optional<std::string> opt;
- std::visit(
- [&opt](const auto &data) {
- if constexpr (!std::is_same<const rttt::unknown_type &,
- decltype(data)>::value) {
- opt = data.text;
- }
- },
- item);
- return opt;
-}
-
-inline std::optional<std::vector<ItemVariant>>
-try_kids(const ItemVariant &item) {
- if (std::holds_alternative<reddit::Story>(item)) {
- return std::get<reddit::Story>(item).kids;
- }
- if (std::holds_alternative<reddit::Comment>(item)) {
- return std::get<reddit::Comment>(item).kids;
- }
- if (std::holds_alternative<hackernews::Comment>(item) ||
- std::holds_alternative<hackernews::Story>(item)) {
- assert(false); // kids work different here, so not supported (yet?)
- }
-
- return std::nullopt;
-}
-
-template <typename C, typename = void> struct has_text : std::false_type {};
-
-template <typename C>
-struct has_text<
- C, typename std::enable_if<std::is_same<decltype(std::declval<C>().text),
- std::string>::value>::type>
- : std::true_type {};
-
-} // namespace rttt