mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-18 10:28:09 +08:00
add map_err_or_else to result
This commit is contained in:
@@ -259,6 +259,49 @@ BOOST_AUTO_TEST_CASE(test_map_or_else)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(test_map_err_or_else)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
const toml::result<int, std::string> result(toml::ok(42));
|
||||||
|
const auto mapped = result.map_err_or_else(
|
||||||
|
[](const std::string i) -> std::string {
|
||||||
|
return i + i;
|
||||||
|
}, "foobar");
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(mapped, "foobar");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
toml::result<std::unique_ptr<int>, std::string>
|
||||||
|
result(toml::ok(std::unique_ptr<int>(new int(42))));
|
||||||
|
const auto mapped = std::move(result).map_err_or_else(
|
||||||
|
[](const std::string i) -> std::string {
|
||||||
|
return i + i;
|
||||||
|
}, "foobar");
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(mapped, "foobar");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const toml::result<int, std::string> result(toml::err<std::string>("hoge"));
|
||||||
|
const auto mapped = result.map_err_or_else(
|
||||||
|
[](const std::string i) -> std::string {
|
||||||
|
return i + i;
|
||||||
|
}, "foobar");
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(mapped, "hogehoge");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
toml::result<std::unique_ptr<int>, std::string>
|
||||||
|
result(toml::err<std::string>("hoge"));
|
||||||
|
const auto mapped = result.map_err_or_else(
|
||||||
|
[](const std::string i) -> std::string {
|
||||||
|
return i + i;
|
||||||
|
}, "foobar");
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(mapped, "hogehoge");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(test_and_then)
|
BOOST_AUTO_TEST_CASE(test_and_then)
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
|
@@ -494,6 +494,31 @@ struct result
|
|||||||
return f(std::move(this->as_ok()));
|
return f(std::move(this->as_ok()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// prerequisities
|
||||||
|
// F: E -> U
|
||||||
|
// retval: U
|
||||||
|
template<typename F, typename U>
|
||||||
|
return_type_of_t<F, error_type&>
|
||||||
|
map_err_or_else(F&& f, U&& opt) &
|
||||||
|
{
|
||||||
|
if(this->is_ok()){return std::forward<U>(opt);}
|
||||||
|
return f(this->as_err());
|
||||||
|
}
|
||||||
|
template<typename F, typename U>
|
||||||
|
return_type_of_t<F, error_type const&>
|
||||||
|
map_err_or_else(F&& f, U&& opt) const&
|
||||||
|
{
|
||||||
|
if(this->is_ok()){return std::forward<U>(opt);}
|
||||||
|
return f(this->as_err());
|
||||||
|
}
|
||||||
|
template<typename F, typename U>
|
||||||
|
return_type_of_t<F, error_type&&>
|
||||||
|
map_err_or_else(F&& f, U&& opt) &&
|
||||||
|
{
|
||||||
|
if(this->is_ok()){return std::forward<U>(opt);}
|
||||||
|
return f(std::move(this->as_err()));
|
||||||
|
}
|
||||||
|
|
||||||
// prerequisities:
|
// prerequisities:
|
||||||
// F: func T -> U
|
// F: func T -> U
|
||||||
// toml::err(error_type) should be convertible to U.
|
// toml::err(error_type) should be convertible to U.
|
||||||
|
Reference in New Issue
Block a user