diff options
author | Edwin van Leeuwen <edwinvanl@tuta.io> | 2022-10-01 13:14:51 +0100 |
---|---|---|
committer | Edwin van Leeuwen <edwinvanl@tuta.io> | 2022-11-05 19:41:47 +0000 |
commit | 1fb9837d9bc1e3b8beccb0dfb6b01824dda0d68b (patch) | |
tree | 149130f95eaeb65cbc84c0661d4200c7c0534ec1 | |
parent | fb1eb2d10eb68364174b9758d1741807e8bf0dd5 (diff) |
refactor: Remove the deprecated SiteType enum
-rw-r--r-- | src/rttt.hpp | 16 | ||||
-rw-r--r-- | src/rttt/hackernews.hpp | 1 | ||||
-rw-r--r-- | src/rttt/reddit.hpp | 27 | ||||
-rw-r--r-- | src/rttt/rss.hpp | 1 | ||||
-rw-r--r-- | src/rttt/thing.hpp | 6 | ||||
-rw-r--r-- | src/rttt/twitter.hpp | 1 | ||||
-rw-r--r-- | test/catch_hackernews.cpp | 1 | ||||
-rw-r--r-- | test/catch_rss.cpp | 21 | ||||
-rw-r--r-- | test/catch_rttt.cpp | 25 | ||||
-rw-r--r-- | test/catch_twitter.cpp | 1 |
10 files changed, 28 insertions, 72 deletions
diff --git a/src/rttt.hpp b/src/rttt.hpp index efdd20b..62afd27 100644 --- a/src/rttt.hpp +++ b/src/rttt.hpp @@ -18,15 +18,6 @@ namespace rttt { -// TODO: move to thing::type -enum class SiteType : int { - Unknown, - HN, - Reddit, - RSS, - Twitter, -}; - // Consider moving this to view.hpp enum class list_mode : int { story, @@ -141,7 +132,6 @@ inline std::vector<std::string> extractURL(std::string text) { } struct Path { - SiteType type = SiteType::Unknown; std::string name; std::string basename; list_mode mode = list_mode::story; @@ -155,7 +145,6 @@ inline Path parse_path(std::string_view path_name) { auto &v = path.parts; if (v.size() == 1) { if (v[0] == "rss") { - path.type = SiteType::RSS; path.basename = "/rss"; if (v.size() == 2 && v[1] != "front") { path.mode = list_mode::feed; @@ -172,11 +161,6 @@ inline Path parse_path(std::string_view path_name) { return path; } } - if (v[0] == "hn") { - path.type = SiteType::HN; - } else if (v[0] == "r") { - path.type = SiteType::Reddit; - } if (v.size() == 4) { if (v[2] == "comments") { path.mode = list_mode::comment; diff --git a/src/rttt/hackernews.hpp b/src/rttt/hackernews.hpp index 7b2a0c1..a1e4b91 100644 --- a/src/rttt/hackernews.hpp +++ b/src/rttt/hackernews.hpp @@ -373,7 +373,6 @@ bool is_valid_path(const rttt::Path &path) { Path parse_path(std::string_view name) { auto path = rttt::parse_path(name); - path.type = SiteType::HN; return path; } diff --git a/src/rttt/reddit.hpp b/src/rttt/reddit.hpp index db06273..76a93bd 100644 --- a/src/rttt/reddit.hpp +++ b/src/rttt/reddit.hpp @@ -26,6 +26,18 @@ namespace rttt { namespace reddit { +bool is_valid_path(std::string_view name) { + // TODO: Ideally we should not split it twice (here and in parse_path) + auto v = rttt::text::split(name, "/"); + return (v.size() > 0 && v[0] == "r"); +} + +bool is_valid_path(const rttt::Path &path) { + // TODO: should we rely on path.type if it is present? + return (path.parts.size() == 2 || path.parts.size() == 4) && + path.parts[0] == "r"; +} + struct credentials { cpr::Header header = cpr::Header{{"User-Agent", "rttt/1.1.1"}}; std::string client_id; @@ -385,7 +397,7 @@ bool update(state &state) { } inline rttt::Path updatePath(rttt::Path &&path) { - logger::log_ifnot(path.type == rttt::SiteType::Reddit); + logger::log_ifnot(reddit::is_valid_path(path)); if (!items.contains(path.name)) { items.insert({path.name, {}}); } @@ -474,21 +486,8 @@ bool catch_ui_event(state &state, ui::state &ui_state, const rttt::Path &path, return false; } -bool is_valid_path(std::string_view name) { - // TODO: Ideally we should not split it twice (here and in parse_path) - auto v = rttt::text::split(name, "/"); - return (v.size() > 0 && v[0] == "r"); -} - -bool is_valid_path(const rttt::Path &path) { - // TODO: should we rely on path.type if it is present? - return (path.parts.size() == 2 || path.parts.size() == 4) && - path.parts[0] == "r"; -} - Path parse_path(std::string_view name) { auto path = rttt::parse_path(name); - path.type = SiteType::Reddit; return path; } diff --git a/src/rttt/rss.hpp b/src/rttt/rss.hpp index e171833..9903d44 100644 --- a/src/rttt/rss.hpp +++ b/src/rttt/rss.hpp @@ -364,7 +364,6 @@ bool is_valid_path(const rttt::Path &path) { Path parse_path(std::string_view name) { auto path = rttt::parse_path(name); - path.type = SiteType::RSS; path.basename = "/rss"; if (path.parts.size() > 1) path.mode = list_mode::feed; diff --git a/src/rttt/thing.hpp b/src/rttt/thing.hpp index 7fa90ae..eaf6e08 100644 --- a/src/rttt/thing.hpp +++ b/src/rttt/thing.hpp @@ -64,9 +64,9 @@ std::optional<rttt::Path> unfocus_path(rttt::Path &&path) { } std::string path_url(const rttt::Path &path) { - if (path.type == rttt::SiteType::HN) { + if (hackernews::is_valid_path(path)) { return std::string("https://news.ycombinator.com/item?id=") + path.id; - } else if (path.type == rttt::SiteType::Reddit) { + } else if (reddit::is_valid_path(path)) { auto uri = reddit::getURIFromPath(path); rttt::replaceAll(uri, "oauth", "www"); return uri; @@ -97,7 +97,7 @@ bool catch_ui_event(state &state, ui::state &ui_state, const rttt::Path &path, std::string window_header(const state &state, const rttt::Path &path) { // TODO: Header should be responsibility of sources. Then we also don't have // to pass username anymore - if (path.type == rttt::SiteType::Reddit) { + if (reddit::is_valid_path(path)) { if (!state.reddit_state.username.empty()) return fmt::format("[R] Reddit ({} on {})", state.reddit_state.username, path.name); diff --git a/src/rttt/twitter.hpp b/src/rttt/twitter.hpp index 43b870b..0e173d8 100644 --- a/src/rttt/twitter.hpp +++ b/src/rttt/twitter.hpp @@ -387,7 +387,6 @@ bool is_valid_path(const rttt::Path &path) { Path parse_path(std::string_view name) { auto path = rttt::parse_path(name); - path.type = SiteType::Twitter; return path; } diff --git a/test/catch_hackernews.cpp b/test/catch_hackernews.cpp index eae4718..46e8b16 100644 --- a/test/catch_hackernews.cpp +++ b/test/catch_hackernews.cpp @@ -62,7 +62,6 @@ SCENARIO("We can use the path API", "[.,api]") { auto path = hackernews::parse_path(pth); REQUIRE(path.parts[1] == "top"); - REQUIRE(path.type == SiteType::HN); size_t count = 5000; rttt::active_storage<std::string, view::item_state> dummy; diff --git a/test/catch_rss.cpp b/test/catch_rss.cpp index 718eb5e..25fa840 100644 --- a/test/catch_rss.cpp +++ b/test/catch_rss.cpp @@ -14,16 +14,17 @@ using namespace rttt; -// https://github.com/zeux/pugixml - -/* - * Design notes: - * use /rss/front as the main path - * - * Either have /rss/feedname as sub paths, or /rss/front/comments/feedname? - * We want two views. one with the title etc, other with the full text included - * (if available) - */ +SCENARIO("Parse path works as expected") { + auto p = rss::parse_path("/rss"); + REQUIRE(p.mode == rttt::list_mode::story); + REQUIRE(p.name == "/rss"); + REQUIRE(p.basename == "/rss"); + + p = rss::parse_path("/rss/x1"); + REQUIRE(p.mode == rttt::list_mode::feed); + REQUIRE(p.name == "/rss/x1"); + REQUIRE(p.basename == "/rss"); +} // rename to parse_opml? std::map<std::string, std::string> parse_opml(const std::string &filename) { diff --git a/test/catch_rttt.cpp b/test/catch_rttt.cpp index 15be3f5..7cca304 100644 --- a/test/catch_rttt.cpp +++ b/test/catch_rttt.cpp @@ -6,59 +6,36 @@ #include "rttt/config.hpp" SCENARIO("We can parse a path correctly") { + // TODO: Move this to the source specific tests auto p = rttt::parse_path("/hn/news"); REQUIRE(p.mode == rttt::list_mode::story); - REQUIRE(p.type == rttt::SiteType::HN); REQUIRE(p.name == "/hn/news"); p = rttt::parse_path("/hn/top"); REQUIRE(p.mode == rttt::list_mode::story); - REQUIRE(p.type == rttt::SiteType::HN); REQUIRE(p.basename == "/hn/top"); REQUIRE(p.name == "/hn/top"); p = rttt::parse_path("/hn/news/comments/x1"); REQUIRE(p.mode == rttt::list_mode::comment); - REQUIRE(p.type == rttt::SiteType::HN); REQUIRE(p.basename == "/hn/news"); REQUIRE(p.name == "/hn/news/comments/x1"); REQUIRE(p.id == "x1"); p = rttt::parse_path("/r/news"); REQUIRE(p.mode == rttt::list_mode::story); - REQUIRE(p.type == rttt::SiteType::Reddit); REQUIRE(p.name == "/r/news"); p = rttt::parse_path("r/news"); REQUIRE(p.mode == rttt::list_mode::story); - REQUIRE(p.type == rttt::SiteType::Reddit); REQUIRE(p.name == "/r/news"); p = rttt::parse_path("/r/news/comments/x1"); REQUIRE(p.mode == rttt::list_mode::comment); - REQUIRE(p.type == rttt::SiteType::Reddit); REQUIRE(p.name == "/r/news/comments/x1"); REQUIRE(p.basename == "/r/news"); REQUIRE(p.id == "x1"); - p = rttt::parse_path("/rss/front"); - REQUIRE(p.mode == rttt::list_mode::story); - REQUIRE(p.type == rttt::SiteType::RSS); - REQUIRE(p.name == "/rss"); - REQUIRE(p.basename == "/rss"); - - p = rttt::parse_path("/rss"); - REQUIRE(p.mode == rttt::list_mode::story); - REQUIRE(p.type == rttt::SiteType::RSS); - REQUIRE(p.name == "/rss"); - REQUIRE(p.basename == "/rss"); - - p = rttt::parse_path("/rss/x1"); - REQUIRE(p.mode == rttt::list_mode::feed); - REQUIRE(p.type == rttt::SiteType::RSS); - REQUIRE(p.name == "/rss/x1"); - REQUIRE(p.basename == "/rss"); - REQUIRE(p.id == "x1"); } SCENARIO("We can extract urls from text") { diff --git a/test/catch_twitter.cpp b/test/catch_twitter.cpp index 637baad..774a3e1 100644 --- a/test/catch_twitter.cpp +++ b/test/catch_twitter.cpp @@ -102,7 +102,6 @@ SCENARIO("We can use the path API", "[.api]") { auto path = twitter::parse_path(pth); REQUIRE(path.parts[1] == "awesomekling"); - REQUIRE(path.type == SiteType::Twitter); size_t count = 5000; rttt::active_storage<std::string, view::item_state> dummy; |