Skip to content

Commit

Permalink
OrcLib: Text: Fmt: add specialization for Result
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienfl-orc committed Jan 26, 2021
1 parent b4e00ee commit 2053818
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/OrcLib/CMakeLists.txt
Expand Up @@ -586,6 +586,7 @@ set(SRC_INOUT_TEXT_FMT
"Output/Text/Fmt/PartitionFlags.h"
"Output/Text/Fmt/PartitionType.h"
"Output/Text/Fmt/path.h"
"Output/Text/Fmt/Result.h"
"Output/Text/Fmt/SYSTEMTIME.h"
"Output/Text/Fmt/TimeUtc.h"
)
Expand All @@ -608,6 +609,7 @@ set(SRC_INOUT_TEXT_FMT_FWD
"Output/Text/Fmt/Fwd/PartitionFlags.h"
"Output/Text/Fmt/Fwd/PartitionType.h"
"Output/Text/Fmt/Fwd/SYSTEMTIME.h"
"Output/Text/Fmt/Fwd/Result.h"
"Output/Text/Fmt/Fwd/TimeUtc.h")

source_group(In&Out\\Output\\Text\\Fmt\\Fwd FILES ${SRC_INOUT_TEXT_FMT_FWD})
Expand Down
1 change: 1 addition & 0 deletions src/OrcLib/Output/Text/Fmt/Formatter.h
Expand Up @@ -25,4 +25,5 @@
#include "Output/Text/Fmt/Fwd/PartitionType.h"
#include "Output/Text/Fmt/Fwd/PartitionFlags.h"
#include "Output/Text/Fmt/Fwd/SYSTEMTIME.h"
#include "Output/Text/Fmt/Fwd/Result.h"
#include "Output/Text/Fmt/Fwd/TimeUtc.h"
26 changes: 26 additions & 0 deletions src/OrcLib/Output/Text/Fmt/Fwd/Result.h
@@ -0,0 +1,26 @@
//
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// Copyright © 2020 ANSSI. All Rights Reserved.
//
// Author(s): fabienfl (ANSSI)
//

#pragma once

#include <fmt/format.h>
#include <boost/outcome/outcome.hpp>

template <typename T>
struct fmt::formatter<boost::outcome_v2::std_result<T>> : public fmt::formatter<std::string_view>
{
template <typename FormatContext>
auto format(const boost::outcome_v2::std_result<T>& result, FormatContext& ctx) -> decltype(ctx.out());
};

template <typename T>
struct fmt::formatter<boost::outcome_v2::std_result<T>, wchar_t> : public fmt::formatter<std::wstring_view, wchar_t>
{
template <typename FormatContext>
auto format(const boost::outcome_v2::std_result<T>& result, FormatContext& ctx) -> decltype(ctx.out());
};
40 changes: 40 additions & 0 deletions src/OrcLib/Output/Text/Fmt/Result.h
@@ -0,0 +1,40 @@
//
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// Copyright © 2020 ANSSI. All Rights Reserved.
//
// Author(s): fabienfl (ANSSI)
//

#pragma once

#include "Output/Text/Fmt/Fwd/Result.h"

#include "Utils/Result.h"

template <typename T>
template <typename FormatContext>
auto fmt::formatter<Orc::Result<T>>::format(const Orc::Result<T>& result, FormatContext& ctx) -> decltype(ctx.out())
{
if (result.has_error())
{
const auto msg = fmt::format("{}", result.error());
return fmt::formatter<std::string_view>::format(msg, ctx);
}

return formatter<std::string_view>::format("Success", ctx);
}

template <typename T>
template <typename FormatContext>
auto fmt::formatter<Orc::Result<T>, wchar_t>::format(const Orc::Result<T>& result, FormatContext& ctx)
-> decltype(ctx.out())
{
if (result.has_error())
{
const auto msg = fmt::format(L"{}", result.error());
return fmt::formatter<std::wstring_view, wchar_t>::format(msg, ctx);
}

return formatter<std::wstring_view, wchar_t>::format(L"Success", ctx);
}

0 comments on commit 2053818

Please sign in to comment.