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>
|
2023-03-06 12:52:49 -05:00
|
|
|
#include <optional>
|
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>
|
2023-01-31 07:48:34 -05:00
|
|
|
requires(std::is_constructible_v<T, T &&>)
|
|
|
|
[[nodiscard]] Result<T, std::string> expect(std::string const& format, Args&&... args) {
|
|
|
|
if (this->isErr()) {
|
|
|
|
return impl::Failure<std::string>(fmt::format(
|
|
|
|
fmt::runtime(format), std::forward<Args>(args)...,
|
|
|
|
fmt::arg("error", this->unwrapErr())
|
|
|
|
));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return std::move(*this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... Args>
|
|
|
|
requires(std::is_constructible_v<T, T const&>)
|
2022-12-02 10:40:51 -05:00
|
|
|
[[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
|
|
|
}
|
2023-02-27 11:36:36 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the result into an optional containing the value if Ok, and
|
|
|
|
* nullopt if Err
|
|
|
|
*/
|
2023-03-07 01:50:20 -05:00
|
|
|
[[nodiscard]] constexpr std::optional<T> ok() const& {
|
2023-02-27 11:36:36 -05:00
|
|
|
if (this->isOk()) {
|
2023-03-07 01:50:20 -05:00
|
|
|
return this->unwrap();
|
2023-02-27 11:36:36 -05:00
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the result into an optional containing the value if Ok, and
|
|
|
|
* nullopt if Err
|
|
|
|
*/
|
2023-03-07 01:50:20 -05:00
|
|
|
[[nodiscard]] constexpr std::optional<T> ok() && {
|
2023-02-27 11:36:36 -05:00
|
|
|
if (this->isOk()) {
|
2023-03-07 01:50:20 -05:00
|
|
|
return this->unwrap();
|
2023-02-27 11:36:36 -05:00
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the result into an optional containing the error if Err, and
|
|
|
|
* nullopt if Ok
|
|
|
|
*/
|
2023-03-07 01:50:20 -05:00
|
|
|
[[nodiscard]] constexpr std::optional<E> err() const& {
|
2023-02-27 11:36:36 -05:00
|
|
|
if (this->isErr()) {
|
2023-03-07 01:50:20 -05:00
|
|
|
return this->unwrapErr();
|
2023-02-27 11:36:36 -05:00
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the result into an optional containing the error if Err, and
|
|
|
|
* nullopt if Ok
|
|
|
|
*/
|
2023-03-07 01:50:20 -05:00
|
|
|
[[nodiscard]] constexpr std::optional<E> err() && {
|
2023-02-27 11:36:36 -05:00
|
|
|
if (this->isErr()) {
|
2023-03-07 01:50:20 -05:00
|
|
|
return this->unwrapErr();
|
2023-02-27 11:36:36 -05:00
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
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>
|
2024-04-22 06:34:14 -04:00
|
|
|
constexpr impl::Success<T> Ok(T value) {
|
|
|
|
// DO NOT MAKE THE PARAMETER T&&!!!! THAT WILL CAUSE C++ TO DO UNEXPECTED
|
|
|
|
// IMPLICIT MOVES FOR EXAMPLE WHEN DOING `Ok(unordered_map.at(value))`
|
2022-12-02 10:40:51 -05:00
|
|
|
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>
|
2024-04-22 06:34:14 -04:00
|
|
|
constexpr impl::Failure<E> Err(E error) {
|
2022-12-02 10:40:51 -05:00
|
|
|
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()) { \
|
2023-06-04 10:23:13 -04:00
|
|
|
return geode::Err(std::move(GEODE_CONCAT(unwrap_res_, __LINE__).unwrapErr())); \
|
2022-12-02 10:40:51 -05:00
|
|
|
} \
|
|
|
|
into = std::move(GEODE_CONCAT(unwrap_res_, __LINE__).unwrap())
|
|
|
|
|
|
|
|
#define GEODE_UNWRAP(...) \
|
2023-08-17 18:38:09 -04:00
|
|
|
do { \
|
2022-11-28 10:42:19 -05:00
|
|
|
auto GEODE_CONCAT(unwrap_res_, __LINE__) = (__VA_ARGS__); \
|
|
|
|
if (GEODE_CONCAT(unwrap_res_, __LINE__).isErr()) { \
|
2023-06-04 10:23:13 -04:00
|
|
|
return geode::Err(std::move(GEODE_CONCAT(unwrap_res_, __LINE__).unwrapErr())); \
|
2022-11-28 10:42:19 -05:00
|
|
|
} \
|
2023-08-17 18:38:09 -04:00
|
|
|
} while(false)
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|