Skip to content

Commit

Permalink
result: define Bail
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-matsui committed Jan 7, 2025
1 parent a3a51d9 commit 1ccbc10
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
18 changes: 8 additions & 10 deletions src/Manifest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Edition::tryFromString(std::string str) noexcept {
} else if (str == "2c") {
return Ok(Edition(Edition::Cpp26, std::move(str)));
}
return Err(anyhow::anyhow("invalid edition"));
Bail("invalid edition");
}

Result<Package>
Expand All @@ -79,7 +79,7 @@ static Result<std::size_t>
validateOptLevel(const std::size_t optLevel) noexcept {
if (optLevel > 3) {
// TODO: use toml::format_error for better diagnostics.
return Err(anyhow::anyhow("opt_level must be between 0 and 3"));
Bail("opt_level must be between 0 and 3");
}
return Ok(optLevel);
}
Expand All @@ -88,18 +88,18 @@ static Result<void>
validateCxxflag(const std::string_view cxxflag) noexcept {
// cxxflag must start with `-`
if (cxxflag.empty() || cxxflag[0] != '-') {
return Err(anyhow::anyhow("cxxflag must start with `-`"));
Bail("cxxflag must start with `-`");
}

// cxxflag only contains alphanumeric characters, `-`, `_`, `=`, `+`, `:`,
// or `.`.
for (const char c : cxxflag) {
if (!std::isalnum(c) && c != '-' && c != '_' && c != '=' && c != '+'
&& c != ':' && c != '.') {
return Err(anyhow::anyhow(
Bail(
"cxxflag must only contain alphanumeric characters, `-`, `_`, `=`, "
"`+`, `:`, or `.`"
));
);
}
}

Expand Down Expand Up @@ -322,11 +322,11 @@ parseDependencies(const toml::value& val, const char* key) noexcept {
}
}

return Err(anyhow::anyhow(fmt::format(
Bail(
"Only Git dependency, path dependency, and system dependency are "
"supported for now: {}",
dep.first
)));
);
}
return Ok(deps);
}
Expand Down Expand Up @@ -428,9 +428,7 @@ findManifest(fs::path candidate) noexcept {
}
}

return Err(
anyhow::anyhow("could not find `cabin.toml` here and in its parents")
);
Bail("could not find `cabin.toml` here and in its parents");
}

Result<Manifest>
Expand Down
25 changes: 19 additions & 6 deletions src/Rustify/Result.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <exception>
#include <fmt/core.h>
#include <mitama/anyhow/anyhow.hpp>
#include <mitama/result/result.hpp>
#include <mitama/thiserror/thiserror.hpp>
Expand All @@ -11,23 +12,25 @@
namespace anyhow = mitama::anyhow;
namespace thiserror = mitama::thiserror;

// NOLINTNEXTLINE
// NOLINTBEGIN(readability-identifier-naming,cppcoreguidelines-macro-usage)

#define Try(...) MITAMA_TRY(__VA_ARGS__)
#define Bail(...) return Err(Anyhow(__VA_ARGS__))

template <typename T, typename E = void>
using Result = std::conditional_t<
std::is_void_v<E>, anyhow::result<T>, mitama::result<T, E>>;

template <typename... Args>
inline auto
Ok(Args&&... args) // NOLINT(readability-identifier-naming)
Ok(Args&&... args)
-> decltype(mitama::success(std::forward<Args>(args)...)) {
return mitama::success(std::forward<Args>(args)...);
}

template <typename E = void, typename... Args>
inline auto
Err(Args&&... args) { // NOLINT(readability-identifier-naming)
Err(Args&&... args) {
if constexpr (std::is_void_v<E>) {
return mitama::failure(std::forward<Args>(args)...);
} else {
Expand All @@ -38,7 +41,17 @@ Err(Args&&... args) { // NOLINT(readability-identifier-naming)
template <thiserror::fixed_string S, typename... T>
using Error = thiserror::error<S, T...>;

// NOLINTNEXTLINE(readability-identifier-naming)
template <typename... T>
inline auto
Anyhow(fmt::format_string<T...> f, T&&... args) {
return anyhow::anyhow(fmt::format(f, std::forward<T>(args)...));
}
template <typename T>
inline auto
Anyhow(T&& arg) {
return anyhow::anyhow(std::forward<T>(arg));
}

inline constexpr auto to_anyhow = [](std::string e) {
return anyhow::anyhow(std::move(e));
};
Expand All @@ -48,7 +61,6 @@ inline constexpr auto to_anyhow = [](std::string e) {
# include <toml.hpp>

namespace toml {
// NOLINTBEGIN(readability-identifier-naming)

template <typename T, typename... U>
inline auto
Expand All @@ -61,7 +73,8 @@ try_find(const toml::value& v, const U&... u) noexcept
}
}

// NOLINTEND(readability-identifier-naming)
} // namespace toml

#endif

// NOLINTEND(readability-identifier-naming,cppcoreguidelines-macro-usage)

0 comments on commit 1ccbc10

Please sign in to comment.