[Enhancement] Implement format methods for Status (#26133)

This commit is contained in:
Liu Zhenlong
2023-11-01 16:55:25 +08:00
committed by GitHub
parent 387e33fa34
commit bfca1bf206
2 changed files with 24 additions and 0 deletions

View File

@ -612,3 +612,17 @@ using ResultError = unexpected<Status>;
// clang-format on
} // namespace doris
// specify formatter for Status
template <>
struct fmt::formatter<doris::Status> {
template <typename ParseContext>
constexpr auto parse(ParseContext& ctx) {
return ctx.begin();
}
template <typename FormatContext>
auto format(doris::Status const& status, FormatContext& ctx) {
return fmt::format_to(ctx.out(), "{}", status.to_string());
}
};

View File

@ -73,4 +73,14 @@ TEST_F(StatusTest, Error) {
}
}
TEST_F(StatusTest /*unused*/, Format /*unused*/) {
// status == ok
Status st_ok = Status::OK();
EXPECT_TRUE(fmt::format("{}", st_ok).compare(fmt::format("{}", st_ok.to_string())) == 0);
// status == error
Status st_error = Status::InternalError("123");
EXPECT_TRUE(fmt::format("{}", st_error).compare(fmt::format("{}", st_error.to_string())) == 0);
}
} // namespace doris