From 04219b55f72d981b93aca77618f549e2cbc32d86 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Wed, 9 Aug 2023 20:06:03 +0300 Subject: [PATCH] message & friends: make formattable So we can easily debug-print them. --- lib/message/mu-fields.hh | 15 +++++++++++++++ lib/message/mu-flags.hh | 12 ++++++++++++ lib/message/mu-message.hh | 11 +++-------- lib/mu-query-results.hh | 9 ++++++++- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/lib/message/mu-fields.hh b/lib/message/mu-fields.hh index 5aebb9e2..24279de8 100644 --- a/lib/message/mu-fields.hh +++ b/lib/message/mu-fields.hh @@ -594,5 +594,20 @@ Option field_from_number(size_t id) } +/** + * Get a fmt-printable representation of Field for fmt + * + * @param field a Field + * + * @return a printable representation + */ +static inline constexpr auto format_as(const Field& field) { + return field.name; +} +static inline constexpr auto format_as(const Field::Id id) { + return format_as(field_from_id(id)); +} + + } // namespace Mu #endif /* MU_FIELDS_HH__ */ diff --git a/lib/message/mu-flags.hh b/lib/message/mu-flags.hh index 8675f00c..d1f317e9 100644 --- a/lib/message/mu-flags.hh +++ b/lib/message/mu-flags.hh @@ -387,6 +387,18 @@ flags_keep_unmutable(Flags old_flags, Flags new_flags, Flags unmutable_flag) */ std::string to_string(Flags flags); + +/** + * Get a string representation of Flags for fmt + * + * @param flags flags + * + * @return string as a sequence of message-flag shortcuts + */ +static inline auto format_as(const Flags& flags) { + return to_string(flags); +} + } // namespace Mu #endif /* MU_FLAGS_HH__ */ diff --git a/lib/message/mu-message.hh b/lib/message/mu-message.hh index aff25716..09a677b9 100644 --- a/lib/message/mu-message.hh +++ b/lib/message/mu-message.hh @@ -460,15 +460,10 @@ private: }; // Message MU_ENABLE_BITOPS(Message::Options); - - -static inline std::ostream& -operator<<(std::ostream& os, const Message& msg) -{ - os << msg.sexp(); - return os; +static inline auto +format_as(const Message& msg) { + return msg.path(); } - } // Mu #endif /* MU_MESSAGE_HH__ */ diff --git a/lib/mu-query-results.hh b/lib/mu-query-results.hh index 4b3875ba..33611707 100644 --- a/lib/mu-query-results.hh +++ b/lib/mu-query-results.hh @@ -109,7 +109,7 @@ QueryMatch::has_flag(QueryMatch::Flags flag) const return any_of(flags & flag); } -inline std::ostream& +static inline std::ostream& operator<<(std::ostream& os, QueryMatch::Flags mflags) { if (mflags == QueryMatch::Flags::None) { @@ -348,6 +348,13 @@ private: QueryMatches& query_matches_; }; + +static inline auto +format_as(const QueryResultsIterator& it) +{ + return it.path().value_or(""); +} + constexpr auto MaxQueryResultsSize = std::numeric_limits::max(); class QueryResults {