Added support for parsing results

This commit is contained in:
Patrick Urbanke
2023-11-05 11:38:33 +01:00
parent a759c25485
commit f9a16641c9
4 changed files with 1118 additions and 1072 deletions
+273 -273
View File
@@ -13,17 +13,17 @@ namespace rfl {
/// To be returned
class Error {
public:
Error(const std::string& _what) : what_(_what) {}
public:
Error(const std::string& _what) : what_(_what) {}
~Error() = default;
~Error() = default;
/// Returns the error message, equivalent to .what() in std::exception.
const std::string& what() const { return what_; }
/// Returns the error message, equivalent to .what() in std::exception.
const std::string& what() const { return what_; }
private:
/// Documents what went wrong
std::string what_;
private:
/// Documents what went wrong
std::string what_;
};
/// Can be used when we are simply interested in whether an operation was
@@ -33,310 +33,310 @@ struct Nothing {};
/// The Result class is used for monadic error handling.
template <class T>
class Result {
static_assert(!std::is_same<T, Error>(),
"The result type cannot be Error.");
static_assert(!std::is_same<T, Error>(), "The result type cannot be Error.");
public:
Result(const T& _val) : t_or_err_(_val) {}
public:
Result(const T& _val) : t_or_err_(_val) {}
Result(T&& _val) noexcept : t_or_err_(std::move(_val)) {}
Result(T&& _val) noexcept : t_or_err_(std::move(_val)) {}
Result(const Error& _err) : t_or_err_(_err) {}
Result(const Error& _err) : t_or_err_(_err) {}
Result(Error&& _err) noexcept : t_or_err_(std::move(_err)) {}
Result(Error&& _err) noexcept : t_or_err_(std::move(_err)) {}
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
bool>::type = true>
Result(Result<U>&& _other)
: t_or_err_(
std::forward<Result<U>>(_other)
.transform([](U&& _u) { return T(std::forward<U>(_u)); })
.t_or_err_) {}
Result(Result<T>&& _other) noexcept = default;
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
bool>::type = true>
Result(const Result<U>& _other)
: t_or_err_(
_other.transform([](const U& _u) { return T(_u); }).t_or_err_) {}
Result(const Result<T>& _other) = default;
~Result() = default;
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
bool>::type = true>
Result(Result<U>&& _other)
: t_or_err_(std::forward<Result<U>>(_other)
.transform([](U&& _u) { return T(std::forward<U>(_u)); })
.t_or_err_) {}
/// Returns Result<U>, if successful and error otherwise.
/// Inspired by .and(...) in the Rust std::result type.
template <class U>
Result<U> and_other(const Result<U>& _r) const noexcept {
const auto f = [&](const auto& _) { return _r; };
return and_then(f);
}
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
bool>::type = true>
Result(const Result<U>& _other)
: t_or_err_(
_other.transform([](const U& _u) { return T(_u); }).t_or_err_) {}
/// Monadic operation - F must be a function of type T -> Result<U>.
template <class F>
auto and_then(const F& _f) {
/// Result_U is expected to be of type Result<U>.
using Result_U = typename std::invoke_result<F, T>::type;
~Result() = default;
const auto handle_variant =
[&]<class TOrError>(TOrError&& _t_or_err) -> Result_U {
if constexpr (!std::is_same<std::decay_t<TOrError>, Error>()) {
return _f(std::forward<TOrError>(_t_or_err));
} else {
return std::forward<TOrError>(_t_or_err);
}
};
/// Returns Result<U>, if successful and error otherwise.
/// Inspired by .and(...) in the Rust std::result type.
template <class U>
Result<U> and_other(const Result<U>& _r) const noexcept {
const auto f = [&](const auto& _) { return _r; };
return and_then(f);
}
return std::visit(handle_variant,
std::forward<std::variant<T, Error>>(t_or_err_));
}
/// Monadic operation - F must be a function of type T -> Result<U>.
template <class F>
auto and_then(const F& _f) {
/// Result_U is expected to be of type Result<U>.
using Result_U = typename std::invoke_result<F, T>::type;
/// Monadic operation - F must be a function of type T -> Result<U>.
template <class F>
auto and_then(const F& _f) const {
/// Result_U is expected to be of type Result<U>.
using Result_U = typename std::invoke_result<F, T>::type;
const auto handle_variant =
[&]<class TOrError>(TOrError&& _t_or_err) -> Result_U {
if constexpr (!std::is_same<std::decay_t<TOrError>, Error>()) {
return _f(std::forward<TOrError>(_t_or_err));
} else {
return std::forward<TOrError>(_t_or_err);
}
};
const auto handle_variant =
[&]<class TOrError>(const TOrError& _t_or_err) -> Result_U {
if constexpr (!std::is_same<TOrError, Error>()) {
return _f(_t_or_err);
} else {
return _t_or_err;
}
};
return std::visit(handle_variant,
std::forward<std::variant<T, Error>>(t_or_err_));
}
return std::visit(handle_variant, t_or_err_);
}
/// Monadic operation - F must be a function of type T -> Result<U>.
template <class F>
auto and_then(const F& _f) const {
/// Result_U is expected to be of type Result<U>.
using Result_U = typename std::invoke_result<F, T>::type;
/// Results types can be iterated over, which even make it possible to use
/// them within a std::range.
T* begin() noexcept {
const auto get_ptr =
[this]<class TOrError>(const TOrError& _t_or_err) -> T* {
if constexpr (!std::is_same<TOrError, Error>()) {
return &_t_or_err;
} else {
return nullptr;
}
};
return std::visit(get_ptr, t_or_err_);
}
const auto handle_variant =
[&]<class TOrError>(const TOrError& _t_or_err) -> Result_U {
if constexpr (!std::is_same<TOrError, Error>()) {
return _f(_t_or_err);
} else {
return _t_or_err;
}
};
/// Results types can be iterated over, which even make it possible to use
/// them within a std::range.
const T* begin() const noexcept {
const auto get_ptr =
[this]<class TOrError>(const TOrError& _t_or_err) -> const T* {
if constexpr (!std::is_same<TOrError, Error>()) {
return &_t_or_err;
} else {
return nullptr;
}
};
return std::visit(get_ptr, t_or_err_);
}
return std::visit(handle_variant, t_or_err_);
}
/// Results types can be iterated over, which even make it possible to use
/// them within a std::range.
T* end() noexcept {
const auto get_ptr =
[this]<class TOrError>(const TOrError& _t_or_err) -> T* {
if constexpr (!std::is_same<TOrError, Error>()) {
return &_t_or_err + 1;
} else {
return nullptr;
}
};
return std::visit(get_ptr, t_or_err_);
}
/// Results types can be iterated over, which even make it possible to use
/// them within a std::range.
T* begin() noexcept {
const auto get_ptr =
[this]<class TOrError>(const TOrError& _t_or_err) -> T* {
if constexpr (!std::is_same<TOrError, Error>()) {
return &_t_or_err;
} else {
return nullptr;
}
};
return std::visit(get_ptr, t_or_err_);
}
/// Results types can be iterated over, which even make it possible to use
/// them within a std::range.
const T* end() const noexcept {
const auto get_ptr =
[]<class TOrError>(const TOrError& _t_or_err) -> const T* {
if constexpr (!std::is_same<TOrError, Error>()) {
return &_t_or_err + 1;
} else {
return nullptr;
}
};
return std::visit(get_ptr, t_or_err_);
}
/// Results types can be iterated over, which even make it possible to use
/// them within a std::range.
const T* begin() const noexcept {
const auto get_ptr =
[this]<class TOrError>(const TOrError& _t_or_err) -> const T* {
if constexpr (!std::is_same<TOrError, Error>()) {
return &_t_or_err;
} else {
return nullptr;
}
};
return std::visit(get_ptr, t_or_err_);
}
/// Returns an std::optional<error> if this does in fact contain an error
/// or std::nullopt otherwise.
std::optional<Error> error() const noexcept {
const auto get_err =
[]<class TOrError>(
const TOrError& _t_or_err) -> std::optional<Error> {
if constexpr (!std::is_same<TOrError, Error>()) {
return std::nullopt;
} else {
return _t_or_err;
}
};
return std::visit(get_err, t_or_err_);
}
/// Results types can be iterated over, which even make it possible to use
/// them within a std::range.
T* end() noexcept {
const auto get_ptr =
[this]<class TOrError>(const TOrError& _t_or_err) -> T* {
if constexpr (!std::is_same<TOrError, Error>()) {
return &_t_or_err + 1;
} else {
return nullptr;
}
};
return std::visit(get_ptr, t_or_err_);
}
/// Returns true if the result contains a value, false otherwise.
operator bool() const noexcept {
return !std::holds_alternative<Error>(t_or_err_);
}
/// Results types can be iterated over, which even make it possible to use
/// them within a std::range.
const T* end() const noexcept {
const auto get_ptr =
[]<class TOrError>(const TOrError& _t_or_err) -> const T* {
if constexpr (!std::is_same<TOrError, Error>()) {
return &_t_or_err + 1;
} else {
return nullptr;
}
};
return std::visit(get_ptr, t_or_err_);
}
/// Allows access to the underlying value. Careful: Will result in undefined
/// behavior, if the result contains an error.
T& operator*() { return *std::get_if<T>(&t_or_err_); }
/// Returns an std::optional<error> if this does in fact contain an error
/// or std::nullopt otherwise.
std::optional<Error> error() const noexcept {
const auto get_err =
[]<class TOrError>(const TOrError& _t_or_err) -> std::optional<Error> {
if constexpr (!std::is_same<TOrError, Error>()) {
return std::nullopt;
} else {
return _t_or_err;
}
};
return std::visit(get_err, t_or_err_);
}
/// Allows read access to the underlying value. Careful: Will result in
/// undefined behavior, if the result contains an error.
const T& operator*() const { return *std::get_if<T>(&t_or_err_); }
/// Returns true if the result contains a value, false otherwise.
operator bool() const noexcept {
return !std::holds_alternative<Error>(t_or_err_);
}
/// Assigns the underlying object.
Result<T>& operator=(const Result<T>& _other) = default;
/// Allows access to the underlying value. Careful: Will result in undefined
/// behavior, if the result contains an error.
T& operator*() { return *std::get_if<T>(&t_or_err_); }
/// Assigns the underlying object.
Result<T>& operator=(Result<T>&& _other) = default;
/// Allows read access to the underlying value. Careful: Will result in
/// undefined behavior, if the result contains an error.
const T& operator*() const { return *std::get_if<T>(&t_or_err_); }
/// Assigns the underlying object.
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
bool>::type = true>
auto& operator=(const Result<U>& _other) {
const auto to_t = [](const U& _u) -> T { return _u; };
t_or_err_ = _other.transform(to_t).t_or_err_;
return *this;
}
/// Assigns the underlying object.
Result<T>& operator=(const Result<T>& _other) = default;
/// Expects a function that takes of type Error -> Result<T> and returns
/// Result<T>.
template <class F>
Result<T> or_else(const F& _f) {
const auto handle_variant =
[&]<class TOrError>(TOrError&& _t_or_err) -> Result<T> {
if constexpr (std::is_same<std::decay_t<TOrError>, Error>()) {
return _f(std::forward<Error>(_t_or_err));
} else {
return std::forward<T>(_t_or_err);
}
};
return std::visit(handle_variant,
std::forward<std::variant<T, Error>>(t_or_err_));
}
/// Assigns the underlying object.
Result<T>& operator=(Result<T>&& _other) = default;
/// Expects a function that takes of type Error -> Result<T> and returns
/// Result<T>.
template <class F>
Result<T> or_else(const F& _f) const {
const auto handle_variant =
[&_f]<class TOrError>(const TOrError& _t_or_err) -> Result<T> {
if constexpr (std::is_same<TOrError, Error>()) {
return _f(_t_or_err);
} else {
return _t_or_err;
}
};
return std::visit(handle_variant, t_or_err_);
}
/// Assigns the underlying object.
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
bool>::type = true>
auto& operator=(const Result<U>& _other) {
const auto to_t = [](const U& _u) -> T { return _u; };
t_or_err_ = _other.transform(to_t).t_or_err_;
return *this;
}
/// Returns the value contained if successful or the provided result r if
/// not.
Result<T> or_other(const Result<T>& _r) const noexcept {
const auto f = [&](const auto& _) { return _r; };
return or_else(f);
}
/// Expects a function that takes of type Error -> Result<T> and returns
/// Result<T>.
template <class F>
Result<T> or_else(const F& _f) {
const auto handle_variant =
[&]<class TOrError>(TOrError&& _t_or_err) -> Result<T> {
if constexpr (std::is_same<std::decay_t<TOrError>, Error>()) {
return _f(std::forward<Error>(_t_or_err));
} else {
return std::forward<T>(_t_or_err);
}
};
return std::visit(handle_variant,
std::forward<std::variant<T, Error>>(t_or_err_));
}
/// Functor operation - F must be a function of type T -> U.
template <class F>
auto transform(const F& _f) {
/// Result_U is expected to be of type Result<U>.
using U = typename std::invoke_result<F, T>::type;
/// Expects a function that takes of type Error -> Result<T> and returns
/// Result<T>.
template <class F>
Result<T> or_else(const F& _f) const {
const auto handle_variant =
[&_f]<class TOrError>(const TOrError& _t_or_err) -> Result<T> {
if constexpr (std::is_same<TOrError, Error>()) {
return _f(_t_or_err);
} else {
return _t_or_err;
}
};
return std::visit(handle_variant, t_or_err_);
}
const auto handle_variant =
[&]<class TOrError>(TOrError&& _t_or_err) -> rfl::Result<U> {
if constexpr (!std::is_same<std::decay_t<TOrError>, Error>()) {
return _f(std::forward<TOrError>(_t_or_err));
} else {
return std::forward<TOrError>(_t_or_err);
}
};
/// Returns the value contained if successful or the provided result r if
/// not.
Result<T> or_other(const Result<T>& _r) const noexcept {
const auto f = [&](const auto& _) { return _r; };
return or_else(f);
}
return std::visit(handle_variant,
std::forward<std::variant<T, Error>>(t_or_err_));
}
/// Functor operation - F must be a function of type T -> U.
template <class F>
auto transform(const F& _f) {
/// Result_U is expected to be of type Result<U>.
using U = typename std::invoke_result<F, T>::type;
/// Functor operation - F must be a function of type T -> U.
template <class F>
auto transform(const F& _f) const {
/// Result_U is expected to be of type Result<U>.
using U = typename std::invoke_result<F, T>::type;
const auto handle_variant =
[&]<class TOrError>(TOrError&& _t_or_err) -> rfl::Result<U> {
if constexpr (!std::is_same<std::decay_t<TOrError>, Error>()) {
return _f(std::forward<TOrError>(_t_or_err));
} else {
return std::forward<TOrError>(_t_or_err);
}
};
const auto handle_variant =
[&]<class TOrError>(const TOrError& _t_or_err) -> rfl::Result<U> {
if constexpr (!std::is_same<TOrError, Error>()) {
return _f(_t_or_err);
} else {
return _t_or_err;
}
};
return std::visit(handle_variant,
std::forward<std::variant<T, Error>>(t_or_err_));
}
return std::visit(handle_variant, t_or_err_);
}
/// Functor operation - F must be a function of type T -> U.
template <class F>
auto transform(const F& _f) const {
/// Result_U is expected to be of type Result<U>.
using U = typename std::invoke_result<F, T>::type;
/// Returns the value if the result does not contain an error, throws an
/// exceptions if not. Similar to .unwrap() in Rust.
T value() {
const auto handle_variant =
[&]<class TOrError>(TOrError& _t_or_err) -> T {
if constexpr (!std::is_same<TOrError, Error>()) {
return std::forward<T>(_t_or_err);
} else {
throw std::runtime_error(_t_or_err.what());
}
};
return std::visit(handle_variant, t_or_err_);
}
const auto handle_variant =
[&]<class TOrError>(const TOrError& _t_or_err) -> rfl::Result<U> {
if constexpr (!std::is_same<TOrError, Error>()) {
return _f(_t_or_err);
} else {
return _t_or_err;
}
};
/// Returns the value if the result does not contain an error, throws an
/// exceptions if not. Similar to .unwrap() in Rust.
const T& value() const {
const auto handle_variant =
[&]<class TOrError>(const TOrError& _t_or_err) -> const T& {
if constexpr (!std::is_same<TOrError, Error>()) {
return _t_or_err;
} else {
throw std::runtime_error(_t_or_err.what());
}
};
return std::visit(handle_variant, t_or_err_);
}
return std::visit(handle_variant, t_or_err_);
}
/// Returns the value or a default.
T value_or(const T& _default) noexcept {
const auto handle_variant =
[&]<class TOrError>(const TOrError& _t_or_err) -> T {
if constexpr (!std::is_same<TOrError, Error>()) {
return std::forward<T>(_t_or_err);
} else {
return _default;
}
};
return std::visit(handle_variant, t_or_err_);
}
/// Returns the value if the result does not contain an error, throws an
/// exceptions if not. Similar to .unwrap() in Rust.
T value() {
const auto handle_variant = [&]<class TOrError>(TOrError& _t_or_err) -> T {
if constexpr (!std::is_same<TOrError, Error>()) {
return std::forward<T>(_t_or_err);
} else {
throw std::runtime_error(_t_or_err.what());
}
};
return std::visit(handle_variant, t_or_err_);
}
/// Returns the value or a default.
T value_or(const T& _default) const noexcept {
const auto handle_variant =
[&]<class TOrError>(const TOrError& _t_or_err) -> T {
if constexpr (!std::is_same<TOrError, Error>()) {
return _t_or_err;
} else {
return _default;
}
};
return std::visit(handle_variant, t_or_err_);
}
/// Returns the value if the result does not contain an error, throws an
/// exceptions if not. Similar to .unwrap() in Rust.
const T& value() const {
const auto handle_variant =
[&]<class TOrError>(const TOrError& _t_or_err) -> const T& {
if constexpr (!std::is_same<TOrError, Error>()) {
return _t_or_err;
} else {
throw std::runtime_error(_t_or_err.what());
}
};
return std::visit(handle_variant, t_or_err_);
}
private:
/// The underlying variant, can either be T or Error.
std::variant<T, Error> t_or_err_;
/// Returns the value or a default.
T value_or(const T& _default) noexcept {
const auto handle_variant =
[&]<class TOrError>(const TOrError& _t_or_err) -> T {
if constexpr (!std::is_same<TOrError, Error>()) {
return std::forward<T>(_t_or_err);
} else {
return _default;
}
};
return std::visit(handle_variant, t_or_err_);
}
/// Returns the value or a default.
T value_or(const T& _default) const noexcept {
const auto handle_variant =
[&]<class TOrError>(const TOrError& _t_or_err) -> T {
if constexpr (!std::is_same<TOrError, Error>()) {
return _t_or_err;
} else {
return _default;
}
};
return std::visit(handle_variant, t_or_err_);
}
private:
/// The underlying variant, can either be T or Error.
std::variant<T, Error> t_or_err_;
};
} // namespace rfl
File diff suppressed because it is too large Load Diff
+32
View File
@@ -0,0 +1,32 @@
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include <vector>
#include "write_and_read.hpp"
void test_result() {
std::cout << "test_result" << std::endl;
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name = "Simpson";
rfl::Field<"children", std::vector<rfl::Result<Person>>> children =
rfl::default_value;
};
const rfl::Result<Person> bart = Person{.first_name = "Bart"};
const rfl::Result<Person> lisa = Person{.first_name = "Lisa"};
const rfl::Result<Person> maggie = rfl::Error("Some error occurred.");
const auto homer = Person{
.first_name = "Homer",
.children = std::vector<rfl::Result<Person>>({bart, lisa, maggie})};
write_and_read(
homer,
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson","children":[]},{"firstName":"Lisa","lastName":"Simpson","children":[]},{"error":"Some error occurred."}]})");
}
+2
View File
@@ -28,6 +28,7 @@
#include "test_replace.hpp"
#include "test_replace_flatten.hpp"
#include "test_replace_with_other_struct.hpp"
#include "test_result.hpp"
#include "test_set.hpp"
#include "test_size.hpp"
#include "test_string_map.hpp"
@@ -71,6 +72,7 @@ int main() {
test_string_map();
test_string_unordered_map();
test_error_messages();
test_result();
test_custom_class1();
test_custom_class2();