2022-07-30 12:24:03 -04:00
|
|
|
#pragma once
|
|
|
|
|
2022-11-28 10:42:19 -05:00
|
|
|
#include "../DefaultInclude.hpp"
|
2022-12-02 10:40:51 -05:00
|
|
|
#include "../external/result/result.hpp"
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
#include <fmt/format.h>
|
2022-11-28 10:42:19 -05:00
|
|
|
#include <string>
|
2022-07-30 12:24:03 -04:00
|
|
|
#include <string_view>
|
2022-12-02 10:40:51 -05:00
|
|
|
#include <type_traits>
|
2022-11-28 10:42:19 -05:00
|
|
|
#include <variant>
|
2022-07-30 12:24:03 -04:00
|
|
|
|
|
|
|
namespace geode {
|
2022-12-02 10:40:51 -05:00
|
|
|
namespace impl {
|
2022-11-28 10:42:19 -05:00
|
|
|
using DefaultValue = std::monostate;
|
|
|
|
using DefaultError = std::string;
|
2022-12-02 10:40:51 -05:00
|
|
|
template <class T>
|
|
|
|
using WrappedResult = std::conditional_t<
|
|
|
|
std::is_lvalue_reference_v<T>, std::reference_wrapper<std::remove_reference_t<T>>,
|
|
|
|
std::remove_const_t<T>>;
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
template <class E = impl::DefaultError>
|
|
|
|
class [[nodiscard]] Failure {
|
|
|
|
public:
|
|
|
|
WrappedResult<E> m_error;
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
Failure() = default;
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
template <class E2>
|
|
|
|
requires(std::is_constructible_v<E, E2 const&>)
|
|
|
|
explicit constexpr Failure(E2 const& e) : m_error(e) {}
|
|
|
|
|
|
|
|
template <class E2>
|
|
|
|
requires(std::is_constructible_v<E, E2 &&>)
|
2022-12-03 06:35:28 -05:00
|
|
|
explicit constexpr Failure(E2&& e) : m_error(std::move(e)) {}
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
E& error() & noexcept {
|
|
|
|
return m_error;
|
2022-11-30 10:07:05 -05:00
|
|
|
}
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
E const& error() const& noexcept {
|
|
|
|
return m_error;
|
2022-11-30 10:07:05 -05:00
|
|
|
}
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
E&& error() && noexcept {
|
|
|
|
return static_cast<E&&>(m_error);
|
2022-11-30 10:07:05 -05:00
|
|
|
}
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
E const&& error() const&& noexcept {
|
|
|
|
return static_cast<E&&>(m_error);
|
2022-11-30 10:07:05 -05:00
|
|
|
}
|
|
|
|
};
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
template <class T = impl::DefaultValue>
|
|
|
|
class [[nodiscard]] Success {
|
|
|
|
public:
|
|
|
|
WrappedResult<T> m_value;
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
Success() = default;
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
template <class T2>
|
|
|
|
requires(std::is_constructible_v<T, T2 const&>)
|
|
|
|
explicit constexpr Success(T2 const& v) : m_value(v) {}
|
|
|
|
|
|
|
|
template <class T2>
|
|
|
|
requires(std::is_constructible_v<T, T2 &&>)
|
2022-12-03 06:35:28 -05:00
|
|
|
explicit constexpr Success(T2&& v) : m_value(std::forward<T2>(v)) {}
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
T& value() & noexcept {
|
|
|
|
return m_value;
|
2022-11-30 10:07:05 -05:00
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
T const& value() const& noexcept {
|
2022-11-30 10:07:05 -05:00
|
|
|
return m_value;
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
T&& value() && noexcept {
|
|
|
|
return static_cast<T&&>(m_value);
|
2022-11-30 10:07:05 -05:00
|
|
|
}
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
T const&& value() const&& noexcept {
|
|
|
|
return static_cast<T&&>(m_value);
|
|
|
|
}
|
2022-11-30 10:07:05 -05:00
|
|
|
};
|
|
|
|
}
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
template <class T = impl::DefaultValue, class E = impl::DefaultError>
|
|
|
|
class [[nodiscard]] Result : public cpp::result<T, E> {
|
2022-11-28 10:42:19 -05:00
|
|
|
public:
|
2022-12-02 10:40:51 -05:00
|
|
|
using Base = cpp::result<T, E>;
|
|
|
|
using ValueType = typename Base::value_type;
|
|
|
|
using ErrorType = typename Base::error_type;
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
using Base::result;
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
template <class U>
|
|
|
|
requires(cpp::detail::result_is_implicit_value_convertible<T, U>::value)
|
|
|
|
constexpr Result(U&& value) = delete;
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
template <class E2>
|
|
|
|
requires(std::is_constructible_v<E, E2 const&>)
|
|
|
|
constexpr Result(impl::Failure<E2> const& e) : Base(cpp::failure<E>(e.error())) {}
|
|
|
|
|
|
|
|
template <class E2>
|
|
|
|
requires(std::is_constructible_v<E, E2 &&>)
|
2022-12-03 06:35:28 -05:00
|
|
|
constexpr Result(impl::Failure<E2>&& e) : Base(cpp::failure<E>(std::move(e.error()))) {}
|
2022-12-02 10:40:51 -05:00
|
|
|
|
|
|
|
template <class T2>
|
|
|
|
requires(std::is_constructible_v<T, T2 const&>)
|
|
|
|
constexpr Result(impl::Success<T2> const& s) : Base(s.value()) {}
|
|
|
|
|
|
|
|
template <class T2>
|
|
|
|
requires(std::is_constructible_v<T, T2 &&>)
|
2022-12-03 06:35:28 -05:00
|
|
|
constexpr Result(impl::Success<T2>&& s) : Base(std::move(s.value())) {}
|
2022-12-02 10:40:51 -05:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr explicit operator bool() const noexcept {
|
2022-12-03 09:41:51 -05:00
|
|
|
return this->Base::operator bool();
|
2022-11-30 10:07:05 -05:00
|
|
|
}
|
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
[[nodiscard]] constexpr bool isOk() const noexcept {
|
2022-12-03 09:41:51 -05:00
|
|
|
return this->Base::has_value();
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
[[nodiscard]] constexpr bool isErr() const noexcept {
|
2022-12-03 09:41:51 -05:00
|
|
|
return this->Base::has_error();
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
[[nodiscard]] constexpr decltype(auto) unwrap() & {
|
2022-12-03 09:41:51 -05:00
|
|
|
return this->Base::value();
|
2022-11-28 10:42:19 -05:00
|
|
|
}
|
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
[[nodiscard]] constexpr decltype(auto) unwrap() const& {
|
2022-12-03 09:41:51 -05:00
|
|
|
return this->Base::value();
|
2022-12-02 10:40:51 -05:00
|
|
|
}
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
[[nodiscard]] constexpr decltype(auto) unwrap() && {
|
2022-12-03 09:41:51 -05:00
|
|
|
return this->Base::value();
|
2022-12-02 10:40:51 -05:00
|
|
|
}
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
[[nodiscard]] constexpr decltype(auto) unwrap() const&& {
|
2022-12-03 09:41:51 -05:00
|
|
|
return this->Base::value();
|
2022-12-02 10:40:51 -05:00
|
|
|
}
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
[[nodiscard]] constexpr decltype(auto) unwrapErr() & {
|
2022-12-03 09:41:51 -05:00
|
|
|
return this->Base::error();
|
2022-12-02 10:40:51 -05:00
|
|
|
}
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
[[nodiscard]] constexpr decltype(auto) unwrapErr() const& {
|
2022-12-03 09:41:51 -05:00
|
|
|
return this->Base::error();
|
2022-12-02 10:40:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr decltype(auto) unwrapErr() && {
|
2022-12-03 09:41:51 -05:00
|
|
|
return this->Base::error();
|
2022-12-02 10:40:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr decltype(auto) unwrapErr() const&& {
|
2022-12-03 09:41:51 -05:00
|
|
|
return this->Base::error();
|
2022-12-02 10:40:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class... Args>
|
|
|
|
[[nodiscard]] Result<T, std::string> expect(std::string const& format, Args&&... args)
|
|
|
|
const {
|
|
|
|
if (this->isErr()) {
|
|
|
|
return impl::Failure<std::string>(fmt::format(
|
|
|
|
fmt::runtime(format), std::forward<Args>(args)...,
|
|
|
|
fmt::arg("error", this->unwrapErr())
|
|
|
|
));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return *this;
|
|
|
|
}
|
2022-11-28 10:42:19 -05:00
|
|
|
}
|
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
template <class U>
|
|
|
|
[[nodiscard]] constexpr decltype(auto) unwrapOr(U&& val) && {
|
2022-12-03 09:41:51 -05:00
|
|
|
return this->Base::value_or(std::forward<U>(val));
|
2022-11-28 10:42:19 -05:00
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
template <class U>
|
|
|
|
[[nodiscard]] constexpr decltype(auto) unwrapOr(U&& val) const& {
|
2022-12-03 09:41:51 -05:00
|
|
|
return this->Base::value_or(std::forward<U>(val));
|
2022-11-28 10:42:19 -05:00
|
|
|
}
|
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
template <class U>
|
|
|
|
[[nodiscard]] constexpr decltype(auto) errorOr(U&& val) && {
|
2022-12-03 09:41:51 -05:00
|
|
|
return this->Base::error_or(std::forward<U>(val));
|
2022-11-28 10:42:19 -05:00
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
template <class U>
|
|
|
|
[[nodiscard]] constexpr decltype(auto) errorOr(U&& val) const& {
|
2022-12-03 09:41:51 -05:00
|
|
|
return this->Base::error_or(std::forward<U>(val));
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
|
|
|
};
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
template <class T = impl::DefaultValue>
|
|
|
|
constexpr impl::Success<T> Ok() {
|
|
|
|
return impl::Success<T>();
|
2022-11-28 10:42:19 -05:00
|
|
|
}
|
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
template <class T>
|
|
|
|
constexpr impl::Success<T> Ok(T&& value) {
|
|
|
|
return impl::Success<T>(std::forward<T>(value));
|
2022-11-28 10:42:19 -05:00
|
|
|
}
|
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
template <class E>
|
|
|
|
constexpr impl::Failure<E> Err(E&& error) {
|
|
|
|
return impl::Failure<E>(std::forward<E>(error));
|
2022-11-28 10:42:19 -05:00
|
|
|
}
|
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
template <class... Args>
|
|
|
|
inline impl::Failure<std::string> Err(std::string const& format, Args&&... args) {
|
|
|
|
return impl::Failure<std::string>(
|
|
|
|
fmt::format(fmt::runtime(format), std::forward<Args>(args)...)
|
|
|
|
);
|
2022-11-28 10:42:19 -05:00
|
|
|
}
|
|
|
|
|
2022-12-02 10:40:51 -05:00
|
|
|
#define GEODE_UNWRAP_INTO(into, ...) \
|
|
|
|
auto GEODE_CONCAT(unwrap_res_, __LINE__) = (__VA_ARGS__); \
|
|
|
|
if (GEODE_CONCAT(unwrap_res_, __LINE__).isErr()) { \
|
|
|
|
return Err(std::move(GEODE_CONCAT(unwrap_res_, __LINE__).unwrapErr())); \
|
|
|
|
} \
|
|
|
|
into = std::move(GEODE_CONCAT(unwrap_res_, __LINE__).unwrap())
|
|
|
|
|
|
|
|
#define GEODE_UNWRAP(...) \
|
|
|
|
{ \
|
2022-11-28 10:42:19 -05:00
|
|
|
auto GEODE_CONCAT(unwrap_res_, __LINE__) = (__VA_ARGS__); \
|
|
|
|
if (GEODE_CONCAT(unwrap_res_, __LINE__).isErr()) { \
|
|
|
|
return Err(std::move(GEODE_CONCAT(unwrap_res_, __LINE__).unwrapErr())); \
|
|
|
|
} \
|
2022-12-02 10:40:51 -05:00
|
|
|
}
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|