summaryrefslogtreecommitdiffstats
path: root/src/item.hpp
blob: ae54ed26831fbecce216d73dad82618257164f78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#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 {};
using ItemVariant =
    std::variant<reddit::Comment, reddit::Story, hackernews::Comment,
                 hackernews::Story, rss::Story, Unknown>;

namespace reddit {
struct Story {
  std::string by = "";
  int descendants = 0;
  int score = 0;
  uint64_t time = 0;
  std::string text = "";
  std::string title = "";
  std::string domain = "";
  std::string url = "";

  std::vector<ItemVariant> kids;

  std::string id = "";
};

struct Comment {
  std::string by = "";
  std::string text = "";
  std::string id = "";
  uint64_t time = 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 &,
                                    decltype(data)>::value) {
          opt = data.time;
        }
      },
      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;
}

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 &,
                                    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