summaryrefslogtreecommitdiffstats
path: root/src/item.hpp
blob: 0fe27a852b6fcce087488e070ac3bee037ae0ba8 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#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