diff --git a/Userland/Libraries/LibCore/FileWatcher.h b/Userland/Libraries/LibCore/FileWatcher.h index 48f82de3986..2a0ece2df8d 100644 --- a/Userland/Libraries/LibCore/FileWatcher.h +++ b/Userland/Libraries/LibCore/FileWatcher.h @@ -95,28 +95,30 @@ template<> struct Formatter : Formatter { ErrorOr format(FormatBuilder& builder, Core::FileWatcherEvent::Type const& value) { - StringView type; - switch (value) { - case Core::FileWatcherEvent::Type::ChildCreated: - type = "ChildCreated"sv; - break; - case Core::FileWatcherEvent::Type::ChildDeleted: - type = "ChildDeleted"sv; - break; - case Core::FileWatcherEvent::Type::Deleted: - type = "Deleted"sv; - break; - case Core::FileWatcherEvent::Type::ContentModified: - type = "ContentModified"sv; - break; - case Core::FileWatcherEvent::Type::MetadataModified: - type = "MetadataModified"sv; - break; - default: - VERIFY_NOT_REACHED(); - } + bool had_any_flag = false; - return builder.put_string(type); + auto put_string_if_has_flag = [&](auto mask, auto name) -> ErrorOr { + if (!has_flag(value, mask)) + return {}; + + if (had_any_flag) + TRY(builder.put_string(", "sv)); + TRY(builder.put_string(name)); + + had_any_flag = true; + return {}; + }; + + TRY(builder.put_string("["sv)); + TRY(put_string_if_has_flag(Core::FileWatcherEvent::Type::ChildCreated, "ChildCreated"sv)); + TRY(put_string_if_has_flag(Core::FileWatcherEvent::Type::ChildDeleted, "ChildDeleted"sv)); + TRY(put_string_if_has_flag(Core::FileWatcherEvent::Type::Deleted, "Deleted"sv)); + TRY(put_string_if_has_flag(Core::FileWatcherEvent::Type::ContentModified, "ContentModified"sv)); + TRY(put_string_if_has_flag(Core::FileWatcherEvent::Type::MetadataModified, "MetadataModified"sv)); + TRY(builder.put_string("]"sv)); + + VERIFY(had_any_flag); + return {}; } };