From 86c6058c56a0f02ad8cb6190e6d762c5b588c992 Mon Sep 17 00:00:00 2001 From: WillisMedwell Date: Fri, 8 Mar 2024 22:31:08 +1100 Subject: [PATCH] Update Result.hpp --- include/Utily/Result.hpp | 41 ++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/include/Utily/Result.hpp b/include/Utily/Result.hpp index 9e74ea9..01b4782 100644 --- a/include/Utily/Result.hpp +++ b/include/Utily/Result.hpp @@ -12,7 +12,9 @@ namespace Utily { class Result { private: - using InternalResult = std::variant; + using V = std::optional; + using E = std::optional; + using InternalResult = std::variant; InternalResult _result; public: @@ -59,30 +61,30 @@ namespace Utily { } [[nodiscard]] constexpr bool has_value() const noexcept { - return std::holds_alternative(_result); + return std::holds_alternative(_result); } [[nodiscard]] constexpr bool has_error() const noexcept { - return std::holds_alternative(_result); + return std::holds_alternative(_result); } [[nodiscard]] constexpr auto value() -> Value& { - return std::get(_result); + return std::get(_result).value(); } [[nodiscard]] constexpr auto value() const -> const Value& { - return std::get(_result); + return std::get(_result).value(); } [[nodiscard]] constexpr auto error() -> Error& { - return std::get(_result); + return std::get(_result).value(); } [[nodiscard]] constexpr auto error() const -> const Error& { - return std::get(_result); + return std::get(_result).value(); } template requires Utily::Concepts::IsConstCallableWith constexpr auto on_value(Pred pred) const noexcept -> const Result& { if (has_value()) { - pred(std::get(_result)); + pred(std::get(_result).value()); } return *this; } @@ -91,7 +93,7 @@ namespace Utily { requires Utily::Concepts::IsCallableWith constexpr auto on_value(Pred pred) noexcept -> Result& { if (has_value()) { - pred(std::get(_result)); + pred(std::get(_result).value()); } return *this; } @@ -100,7 +102,7 @@ namespace Utily { requires Utily::Concepts::IsConstCallableWith constexpr auto on_error(Pred pred) const noexcept -> const Result& { if (has_error()) { - pred(std::get(_result)); + pred(std::get(_result).value()); } return *this; } @@ -109,7 +111,7 @@ namespace Utily { requires Utily::Concepts::IsCallableWith constexpr auto on_error(Pred pred) noexcept -> Result& { if (has_error()) { - pred(std::get(_result)); + pred(std::get(_result).value()); } return *this; } @@ -118,10 +120,10 @@ namespace Utily { requires Utily::Concepts::IsCallableWith && Utily::Concepts::IsCallableWith constexpr auto on_either(ValuePred value_pred, ErrorPred error_pred) -> Result& { - if (std::holds_alternative(_result)) { - value_pred(std::get(_result)); + if (std::holds_alternative(_result)) { + value_pred(std::get(_result).value()); } else { - error_pred(std::get(_result)); + error_pred(std::get(_result).value()); } return *this; } @@ -131,9 +133,16 @@ namespace Utily { && Utily::Concepts::IsConstCallableWith constexpr auto on_either(ValuePred value_pred, ErrorPred error_pred) const noexcept -> const Result& { if (std::holds_alternative(_result)) { - value_pred(std::get(_result)); + value_pred(std::get(_result).value()); } else { - error_pred(std::get(_result)); + error_pred(std::get(_result).value()); + } + return *this; + } + + auto on_error_panic() -> Result& { + if(std::holds_alternative(_result)) { + throw std::runtime_error("Error encountered, panicking."); } return *this; }