-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(format): add formatter for common standard library types (#83)
* std::optional * std::exception * std::filesystem::path * std::variant
- Loading branch information
Showing
7 changed files
with
258 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,5 +39,6 @@ | |
#include "format.hpp" | ||
#include "ranges.hpp" | ||
#include "scan.hpp" | ||
#include "std.hpp" | ||
|
||
#endif // EMIO_Z_MAIN_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// | ||
// Copyright (c) 2024 - present, Toni Neubert | ||
// All rights reserved. | ||
// | ||
// For the license information refer to emio.hpp | ||
|
||
#pragma once | ||
|
||
#include <exception> | ||
#include <filesystem> | ||
#include <optional> | ||
#include <variant> | ||
|
||
#include "formatter.hpp" | ||
|
||
namespace emio { | ||
|
||
/** | ||
* Formatter for std::optional. | ||
* @tparam T The type to format. | ||
*/ | ||
template <typename T> | ||
requires is_formattable_v<T> | ||
class formatter<std::optional<T>> { | ||
public: | ||
static constexpr result<void> validate(reader& format_rdr) noexcept { | ||
return detail::format::validate_trait<T>(format_rdr); | ||
} | ||
|
||
constexpr result<void> parse(reader& format_rdr) noexcept { | ||
return underlying_.parse(format_rdr); | ||
} | ||
|
||
constexpr result<void> format(writer& out, const std::optional<T>& arg) const noexcept { | ||
if (!arg.has_value()) { | ||
return out.write_str(detail::sv("none")); | ||
} else { | ||
EMIO_TRYV(out.write_str(detail::sv("optional("))); | ||
EMIO_TRYV(underlying_.format(out, *arg)); | ||
return out.write_char(')'); | ||
} | ||
} | ||
|
||
private: | ||
formatter<T> underlying_; | ||
}; | ||
|
||
/** | ||
* Formatter for std::exception. | ||
* @tparam T The type. | ||
*/ | ||
template <typename T> | ||
requires std::is_base_of_v<std::exception, T> | ||
class formatter<T> : public formatter<std::string_view> { | ||
public: | ||
result<void> format(writer& out, const std::exception& arg) const noexcept { | ||
return formatter<std::string_view>::format(out, arg.what()); | ||
} | ||
}; | ||
|
||
/** | ||
* Formatter for std::filesystem::path. | ||
*/ | ||
template <> | ||
class formatter<std::filesystem::path> : public formatter<std::string_view> { | ||
public: | ||
result<void> format(writer& out, const std::filesystem::path& arg) const noexcept { | ||
return formatter<std::string_view>::format(out, arg.native()); | ||
} | ||
}; | ||
|
||
/** | ||
* Formatter for std::monostate. | ||
*/ | ||
template <> | ||
class formatter<std::monostate> { | ||
public: | ||
static constexpr result<void> validate(reader& format_rdr) noexcept { | ||
return format_rdr.read_if_match_char('}'); | ||
} | ||
|
||
// NOLINTNEXTLINE(readability-convert-member-functions-to-static): API requests this to be a member function. | ||
constexpr result<void> parse(reader& format_rdr) noexcept { | ||
return format_rdr.read_if_match_char('}'); | ||
} | ||
|
||
// NOLINTNEXTLINE(readability-convert-member-functions-to-static): API requests this to be a member function. | ||
constexpr result<void> format(writer& out, const std::monostate& /*arg*/) const noexcept { | ||
return out.write_str(detail::sv("monostate")); | ||
} | ||
}; | ||
|
||
/** | ||
* Formatter for std::variant. | ||
* @tparam Ts The types to format. | ||
*/ | ||
template <typename... Ts> | ||
requires(is_formattable_v<Ts> && ...) | ||
class formatter<std::variant<Ts...>> { | ||
public: | ||
static constexpr result<void> validate(reader& format_rdr) noexcept { | ||
return format_rdr.read_if_match_char('}'); | ||
} | ||
|
||
constexpr result<void> parse(reader& format_rdr) noexcept { | ||
return format_rdr.read_if_match_char('}'); | ||
} | ||
|
||
constexpr result<void> format(writer& out, const std::variant<Ts...>& arg) noexcept { | ||
EMIO_TRYV(out.write_str(detail::sv("variant("))); | ||
#ifdef __EXCEPTIONS | ||
try { | ||
#endif | ||
EMIO_TRYV(std::visit(detail::overload{ | ||
[&](const char& val) -> result<void> { | ||
return out.write_char_escaped(val); | ||
}, | ||
[&](const std::string_view& val) -> result<void> { | ||
return out.write_str_escaped(val); | ||
}, | ||
[&](const auto& val) -> result<void> { | ||
using T = std::decay_t<decltype(val)>; | ||
if constexpr (!std::is_null_pointer_v<T> && | ||
std::is_constructible_v<std::string_view, T>) { | ||
return out.write_str_escaped(std::string_view{val}); | ||
} else { | ||
formatter<T> fmt; | ||
emio::reader rdr{detail::sv("}")}; | ||
EMIO_TRYV(fmt.parse(rdr)); | ||
return fmt.format(out, val); | ||
} | ||
}, | ||
}, | ||
arg)); | ||
#ifdef __EXCEPTIONS | ||
} catch (const std::bad_variant_access&) { | ||
EMIO_TRYV(out.write_str(detail::sv("valueless by exception"))); | ||
} | ||
#endif | ||
return out.write_char(')'); | ||
} // namespace emio | ||
}; | ||
|
||
} // namespace emio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Unit under test. | ||
#include <emio/std.hpp> | ||
|
||
// Other includes. | ||
#include <catch2/catch_test_macros.hpp> | ||
#include <emio/format.hpp> | ||
#include <emio/ranges.hpp> | ||
|
||
struct unformattable {}; | ||
|
||
TEST_CASE("std::optional") { | ||
CHECK(emio::format("{}", std::optional<int>{}) == "none"); | ||
CHECK(emio::format("{:x}", std::optional<int>{42}) == "optional(2a)"); | ||
CHECK(emio::format("{:x}", std::optional<int>{42}) == "optional(2a)"); | ||
CHECK(emio::format(emio::runtime("{:x}"), std::optional<int>{42}) == "optional(2a)"); | ||
CHECK(emio::format("{}", std::optional{std::vector{'h', 'e', 'l', 'l', 'o'}}) == | ||
"optional(['h', 'e', 'l', 'l', 'o'])"); | ||
CHECK(emio::format("{::d}", std::optional{std::vector{'h', 'e', 'l', 'l', 'o'}}) == | ||
"optional([104, 101, 108, 108, 111])"); | ||
|
||
STATIC_CHECK(emio::is_formattable_v<std::optional<int>>); | ||
STATIC_CHECK_FALSE(emio::is_formattable_v<std::optional<unformattable>>); | ||
} | ||
|
||
TEST_CASE("std::exception") { | ||
CHECK(emio::format("{}", std::exception{}) == "std::exception"); | ||
CHECK(emio::format("{}", std::runtime_error{"hello"}) == "hello"); | ||
} | ||
|
||
TEST_CASE("std::filesystem::path") { | ||
using std::filesystem::path; | ||
|
||
CHECK(emio::format("{}", path{}) == ""); | ||
CHECK(emio::format("{}", path{"/abc/dev"}) == "/abc/dev"); | ||
CHECK(emio::format("{:x>11}", path{"/abc/dev"}) == "xxx/abc/dev"); | ||
CHECK(emio::format("{:x<11?}", path{"/abc/dev"}) == "\"/abc/dev\"x"); | ||
CHECK(emio::format(emio::runtime("{:x<11?}"), path{"/abc/dev"}) == "\"/abc/dev\"x"); | ||
} | ||
|
||
namespace { | ||
|
||
struct throws_on_move { | ||
throws_on_move() = default; | ||
throws_on_move(throws_on_move&&) { | ||
throw std::runtime_error{"As expected..."}; | ||
} | ||
}; | ||
|
||
std::string_view format_as(const throws_on_move&) { | ||
throw std::logic_error{"Shouldn't be called."}; | ||
} | ||
|
||
} // namespace | ||
|
||
TEST_CASE("std::variant") { | ||
STATIC_CHECK(emio::is_formattable_v<std::string>); | ||
|
||
std::variant<std::monostate, int, double, char, std::nullptr_t, std::string> v{}; | ||
CHECK(emio::format("{}", v) == "variant(monostate)"); | ||
CHECK(emio::format(emio::runtime("{}"), v) == "variant(monostate)"); | ||
v = 42; | ||
CHECK(emio::format("{}", v) == "variant(42)"); | ||
v = 4.2; | ||
CHECK(emio::format("{}", v) == "variant(4.2)"); | ||
v = 'x'; | ||
CHECK(emio::format("{}", v) == "variant('x')"); | ||
v = nullptr; | ||
CHECK(emio::format("{}", v) == "variant(0x0)"); | ||
v = "abc"; | ||
CHECK(emio::format("{}", v) == "variant(\"abc\")"); | ||
|
||
SECTION("valueless by exception") { | ||
std::variant<std::monostate, throws_on_move> v2; | ||
try { | ||
throws_on_move thrower; | ||
v2.emplace<throws_on_move>(std::move(thrower)); | ||
} catch (const std::runtime_error&) { | ||
} | ||
CHECK(emio::format("{}", v2) == "variant(valueless by exception)"); | ||
} | ||
} |