Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove core::except completely #360

Merged
merged 3 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ jobs:
sudo apt-get install -y -qq pkg-config cmake ${{ matrix.compiler.pacakge }}

- name: Restore & Cache CMake build results
if: matrix.build_type != 'Coverage'
uses: actions/[email protected]
with:
path: build
Expand Down
3 changes: 2 additions & 1 deletion include/poac/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ namespace poac {
} // end namespace

namespace poac::config::path {
inline const std::filesystem::path root(util::misc::expand_user() / std::filesystem::path(".poac"));
inline const std::filesystem::path user = util::misc::expand_user().unwrap();
inline const std::filesystem::path root(user / ".poac");
inline const std::filesystem::path cache_dir(root / "cache");
inline const std::filesystem::path archive_dir(cache_dir / "archive");
inline const std::filesystem::path extract_dir(cache_dir / "extract");
Expand Down
1 change: 0 additions & 1 deletion include/poac/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define POAC_CORE_HPP

#include <poac/core/builder.hpp>
#include <poac/core/except.hpp>
#include <poac/core/resolver.hpp>
#include <poac/core/validator.hpp>

Expand Down
188 changes: 106 additions & 82 deletions include/poac/core/builder/standard.hpp

Large diffs are not rendered by default.

22 changes: 0 additions & 22 deletions include/poac/core/except.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion include/poac/core/resolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ namespace poac::core::resolver {
std::ofstream archive(archive_path);
const auto [host, target] = util::net::parse_url(download_link);
const util::net::requests requests{ host };
requests.get(target, {}, std::move(archive));
static_cast<void>(requests.get(target, {}, std::move(archive)));

return mitama::success(archive_path);
} catch (...) {
Expand Down
15 changes: 6 additions & 9 deletions include/poac/util/misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
// external
#include <boost/algorithm/string.hpp>
#include <boost/predef.h>

// internal
#include <poac/core/except.hpp>
#include <mitama/result/result.hpp>

namespace poac::util::misc {
inline namespace path_literals {
Expand Down Expand Up @@ -58,19 +56,18 @@ namespace poac::util::misc {

// Inspired by https://stackoverflow.com/q/4891006
// Expand ~ to user home directory.
std::string expand_user() {
[[nodiscard]] mitama::result<std::filesystem::path, std::string>
expand_user() {
auto home = dupenv("HOME");
if (home || (home = dupenv("USERPROFILE"))) {
return home.value();
return mitama::success(home.value());
} else {
const auto home_drive = dupenv("HOMEDRIVE");
const auto home_path = dupenv("HOMEPATH");
if (home_drive && home_path) {
return home_drive.value() + home_path.value();
return mitama::success(home_drive.value() + home_path.value());
}
throw core::except::error(
"could not read environment variable"
);
return mitama::failure("could not get home directory");
}
}
} // end namespace
Expand Down
60 changes: 28 additions & 32 deletions include/poac/util/net.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@

// internal
#include <poac/config.hpp>
#include <poac/core/except.hpp>
#include <poac/util/meta.hpp>
#include <poac/util/misc.hpp>
#include <poac/util/pretty.hpp>
Expand Down Expand Up @@ -268,7 +267,7 @@ namespace poac::util::net {
typename ResponseBody,
typename Request,
typename Ofstream>
typename ResponseBody::value_type
[[nodiscard]] mitama::result<typename ResponseBody::value_type, std::string>
request(Request&& req, Ofstream&& ofs) const {
ssl_prepare();
write_request(req);
Expand All @@ -288,7 +287,7 @@ namespace poac::util::net {
std::ofstream>,
http::vector_body<unsigned char>,
http::string_body>>
typename ResponseBody::value_type
[[nodiscard]] mitama::result<typename ResponseBody::value_type, std::string>
get(
const std::string_view target,
const headers_t& headers={},
Expand Down Expand Up @@ -320,7 +319,7 @@ namespace poac::util::net {
std::ofstream>,
http::vector_body<unsigned char>,
http::string_body>>
typename ResponseBody::value_type
[[nodiscard]] mitama::result<typename ResponseBody::value_type, std::string>
post(
const std::string_view target,
BodyType&& body,
Expand Down Expand Up @@ -429,7 +428,7 @@ namespace poac::util::net {
typename ResponseBody,
typename Request,
typename Ofstream>
typename ResponseBody::value_type
[[nodiscard]] mitama::result<typename ResponseBody::value_type, std::string>
read_response(Request&& old_req, Ofstream&& ofs) const {
// This buffer is used for reading and must be persisted
boost::beast::flat_buffer buffer;
Expand All @@ -439,9 +438,10 @@ namespace poac::util::net {
http::read(*stream, buffer, res);
// Handle HTTP status code
return handle_status<method>(
std::forward<Request>(old_req),
std::move(res),
std::forward<Ofstream>(ofs));
std::forward<Request>(old_req),
std::move(res),
std::forward<Ofstream>(ofs)
);
}

template <
Expand All @@ -450,35 +450,33 @@ namespace poac::util::net {
typename Response,
typename Ofstream,
typename ResponseBody = typename Response::body_type>
typename ResponseBody::value_type
[[nodiscard]] mitama::result<typename ResponseBody::value_type, std::string>
handle_status(Request&& old_req, Response&& res, Ofstream&& ofs) const
{
close_stream();
switch (res.base().result_int() / 100) {
case 2:
return parse_response(
std::forward<Response>(res),
std::forward<Ofstream>(ofs));
return mitama::success(parse_response(
std::forward<Response>(res),
std::forward<Ofstream>(ofs)
));
case 3:
return redirect<method>(
std::forward<Request>(old_req),
std::forward<Response>(res),
std::forward<Ofstream>(ofs));
std::forward<Request>(old_req),
std::forward<Response>(res),
std::forward<Ofstream>(ofs)
);
default:
if constexpr (!std::is_same_v<util::meta::remove_cvref_t<Ofstream>, std::ofstream>) {
throw core::except::error(
fmt::format(
"util::net received a bad response code: {}\n{}",
res.base().result_int(), res.body()
)
);
return mitama::failure(fmt::format(
"util::net received a bad response code: {}\n{}",
res.base().result_int(), res.body()
));
} else {
throw core::except::error(
fmt::format(
"util::net received a bad response code: {}",
res.base().result_int()
)
);
throw mitama::failure(fmt::format(
"util::net received a bad response code: {}",
res.base().result_int()
));
}
}
}
Expand Down Expand Up @@ -524,7 +522,7 @@ namespace poac::util::net {
typename Response,
typename Ofstream,
typename ResponseBody = typename Response::body_type>
typename ResponseBody::value_type
[[nodiscard]] mitama::result<typename ResponseBody::value_type, std::string>
redirect(Request&& old_req, Response&& res, Ofstream&& ofs) const {
const std::string new_location(res.base()["Location"]);
const auto [new_host, new_target] = parse_url(new_location);
Expand All @@ -537,7 +535,7 @@ namespace poac::util::net {
} else if (method == http::verb::post) {
return req.post(new_target, old_req.body(), {}, std::forward<Ofstream>(ofs));
} else { // verb error
return {};
return mitama::failure("[util::net::requests] unknown verb used");
}
}

Expand Down Expand Up @@ -593,15 +591,13 @@ namespace poac::util::net::api {
headers.emplace("X-Algolia-API-Key", ALGOLIA_SEARCH_ONLY_KEY);
headers.emplace("X-Algolia-Application-Id", ALGOLIA_APPLICATION_ID);

const auto response = request.post(ALGOLIA_SEARCH_INDEX_API, body, headers);
const auto response = MITAMA_TRY(request.post(ALGOLIA_SEARCH_INDEX_API, body, headers));
std::stringstream response_body;
response_body << response.data();

boost::property_tree::ptree pt;
boost::property_tree::json_parser::read_json(response_body, pt);
return mitama::success(pt);
} catch (const core::except::error& e) {
return mitama::failure(e.what());
} catch (...) {
return mitama::failure("unknown error caused when calling search api");
}
Expand Down
1 change: 0 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
set(TEST_NAMES
cfg
except
meta
misc
net
Expand Down
16 changes: 0 additions & 16 deletions tests/except.cpp

This file was deleted.

Loading