Skip to content

Commit

Permalink
Remove core::except completely
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-matsui committed Jan 4, 2021
1 parent cd56d7b commit 48a3e1e
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 187 deletions.
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.

38 changes: 16 additions & 22 deletions tests/standard.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#define BOOST_TEST_MAIN
#include <boost/test/included/unit_test.hpp>
#include <poac/core/builder/standard.hpp>
#include <poac/core/except.hpp>

// inline std::string version_prefix(const bool& enable_gnu) noexcept
BOOST_AUTO_TEST_CASE( poac_core_builder_field_standard_version_prefix_test )
Expand All @@ -15,7 +14,6 @@ BOOST_AUTO_TEST_CASE( poac_core_builder_field_standard_version_prefix_test )
BOOST_AUTO_TEST_CASE( poac_core_builder_field_standard_apple_llvm_convert_test )
{
using poac::core::builder::standard::apple_llvm_convert;
using poac::core::except::error;

BOOST_CHECK( apple_llvm_convert(98, false) == "-std=c++98" );
BOOST_CHECK( apple_llvm_convert(98, true) == "-std=gnu++98" );
Expand All @@ -32,53 +30,51 @@ BOOST_AUTO_TEST_CASE( poac_core_builder_field_standard_apple_llvm_convert_test )
BOOST_CHECK( apple_llvm_convert(17, false) == "-std=c++17" );
BOOST_CHECK( apple_llvm_convert(17, true) == "-std=gnu++17" );

BOOST_CHECK_THROW( apple_llvm_convert(20, false), error );
BOOST_CHECK_THROW( apple_llvm_convert(6, true), error );
BOOST_CHECK( apple_llvm_convert(20, false).is_err() );
BOOST_CHECK( apple_llvm_convert(6, true).is_err() );
}

// std::string gcc_convert(const std::uint_fast8_t& cpp_version, const std::string& compiler_version, const bool& enable_gnu)
BOOST_AUTO_TEST_CASE( poac_core_builder_field_standard_gcc_convert_test )
{
using poac::core::builder::standard::gcc_convert;
using poac::core::except::error;

BOOST_CHECK( gcc_convert(98, "1.0.0", false) == "" );
BOOST_CHECK( gcc_convert(98, "1.0.0", true) == "" );

BOOST_CHECK_THROW( gcc_convert(3, "4.2", false), error );
BOOST_CHECK( gcc_convert(3, "4.2", false).is_err() );
BOOST_CHECK( gcc_convert(3, "4.3", false) == "-std=c++0x" );
BOOST_CHECK( gcc_convert(3, "4.5", true) == "-std=gnu++0x" );
BOOST_CHECK( gcc_convert(3, "4.7", false) == "-std=c++11" );
BOOST_CHECK( gcc_convert(3, "8.0", true) == "-std=gnu++11" );

BOOST_CHECK_THROW( gcc_convert(11, "4.2", false), error );
BOOST_CHECK( gcc_convert(11, "4.2", false).is_err() );
BOOST_CHECK( gcc_convert(11, "4.3", false) == "-std=c++0x" );
BOOST_CHECK( gcc_convert(11, "4.5", true) == "-std=gnu++0x" );
BOOST_CHECK( gcc_convert(11, "4.7", false) == "-std=c++11" );
BOOST_CHECK( gcc_convert(11, "8.0", true) == "-std=gnu++11" );

BOOST_CHECK_THROW( gcc_convert(14, "4.7", false), error );
BOOST_CHECK( gcc_convert(14, "4.7", false).is_err() );
BOOST_CHECK( gcc_convert(14, "4.8", false) == "-std=c++1y" );
BOOST_CHECK( gcc_convert(14, "4.8.3", true) == "-std=gnu++1y" );
BOOST_CHECK( gcc_convert(14, "4.9", false) == "-std=c++14" );
BOOST_CHECK( gcc_convert(14, "8.0", true) == "-std=gnu++14" );

BOOST_CHECK_THROW( gcc_convert(17, "4.9", false), error );
BOOST_CHECK( gcc_convert(17, "4.9", false).is_err() );
BOOST_CHECK( gcc_convert(17, "5.0", false) == "-std=c++17" );
BOOST_CHECK( gcc_convert(17, "8.0", true) == "-std=gnu++17" );

BOOST_CHECK_THROW( gcc_convert(20, "7", false), error );
BOOST_CHECK( gcc_convert(20, "7", false).is_err() );
BOOST_CHECK( gcc_convert(20, "8.0", false) == "-std=c++2a" );
BOOST_CHECK( gcc_convert(20, "10.0", true) == "-std=gnu++2a" );

BOOST_CHECK_THROW( gcc_convert(9, "1.0.0", false), error );
BOOST_CHECK( gcc_convert(9, "1.0.0", false).is_err() );
}

// std::string clang_convert(const std::uint_fast8_t& cpp_version, const std::string& compiler_version, const bool& enable_gnu)
BOOST_AUTO_TEST_CASE( poac_core_builder_field_standard_clang_convert_test )
{
using poac::core::builder::standard::clang_convert;
using poac::core::except::error;

BOOST_CHECK( clang_convert(98, "1.0.0", false) == "" );
BOOST_CHECK( clang_convert(98, "1.0.0", true) == "" );
Expand All @@ -93,30 +89,29 @@ BOOST_AUTO_TEST_CASE( poac_core_builder_field_standard_clang_convert_test )
BOOST_CHECK( clang_convert(11, "7.0", false) == "-std=c++11" );
BOOST_CHECK( clang_convert(11, "7.0", true) == "-std=gnu++11" );

BOOST_CHECK_THROW( clang_convert(14, "3.1", false), error );
BOOST_CHECK( clang_convert(14, "3.1", false).is_err() );
BOOST_CHECK( clang_convert(14, "3.2", false) == "-std=c++1y" );
BOOST_CHECK( clang_convert(14, "3.4", true) == "-std=gnu++1y" );
BOOST_CHECK( clang_convert(14, "3.5", false) == "-std=c++14" );
BOOST_CHECK( clang_convert(14, "7.0", true) == "-std=gnu++14" );

BOOST_CHECK_THROW( clang_convert(17, "3.4.0", false), error );
BOOST_CHECK( clang_convert(17, "3.4.0", false).is_err() );
BOOST_CHECK( clang_convert(17, "3.5.0", false) == "-std=c++1z" );
BOOST_CHECK( clang_convert(17, "4.9.0", true) == "-std=gnu++1z" );
BOOST_CHECK( clang_convert(17, "5.0", false) == "-std=c++17" );
BOOST_CHECK( clang_convert(17, "7.0", true) == "-std=gnu++17" );

BOOST_CHECK_THROW( clang_convert(20, "5.9.0", false), error );
BOOST_CHECK( clang_convert(20, "5.9.0", false).is_err() );
BOOST_CHECK( clang_convert(20, "6.0", false) == "-std=c++2a" );
BOOST_CHECK( clang_convert(20, "7.0", true) == "-std=gnu++2a" );

BOOST_CHECK_THROW( clang_convert(12, "1.0.0", false), error );
BOOST_CHECK( clang_convert(12, "1.0.0", false).is_err() );
}

// std::string icc_convert(const std::uint_fast8_t& cpp_version)
BOOST_AUTO_TEST_CASE( poac_core_builder_field_standard_icc_convert_test )
{
using poac::core::builder::standard::icc_convert;
using poac::core::except::error;

BOOST_CHECK( icc_convert(98) == "" );
#ifndef _WIN32
Expand All @@ -130,21 +125,20 @@ BOOST_AUTO_TEST_CASE( poac_core_builder_field_standard_icc_convert_test )
BOOST_CHECK( icc_convert(14) == "/Qstd:c++14" );
BOOST_CHECK( icc_convert(17) == "/Qstd:c++17" );
#endif
BOOST_CHECK_THROW( icc_convert(20), error );
BOOST_CHECK_THROW( icc_convert(15), error );
BOOST_CHECK( icc_convert(20).is_err() );
BOOST_CHECK( icc_convert(15).is_err() );
}

// std::string msvc_convert(const std::uint_fast8_t& cpp_version)
BOOST_AUTO_TEST_CASE( poac_core_builder_field_standard_msvc_convert_test )
{
using poac::core::builder::standard::msvc_convert;
using poac::core::except::error;

BOOST_CHECK( msvc_convert(98) == "" );
BOOST_CHECK( msvc_convert(3) == "" );
BOOST_CHECK( msvc_convert(11) == "" );
BOOST_CHECK( msvc_convert(14) == "/std:c++14" );
BOOST_CHECK( msvc_convert(17) == "/std:c++17" );
BOOST_CHECK_THROW( msvc_convert(20), error );
BOOST_CHECK_THROW( msvc_convert(18), error );
BOOST_CHECK( msvc_convert(20).is_err() );
BOOST_CHECK( msvc_convert(18).is_err() );
}

0 comments on commit 48a3e1e

Please sign in to comment.