mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:57:45 +00:00
AK: Add JSON object/array for-each methods for fallible callbacks
This allows the provided callback to return an ErrorOr-like type to propagate errors back to the caller.
This commit is contained in:
parent
56ab529752
commit
13b18a182a
3 changed files with 129 additions and 0 deletions
|
@ -7,6 +7,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Concepts.h>
|
||||
#include <AK/Error.h>
|
||||
#include <AK/JsonArraySerializer.h>
|
||||
#include <AK/JsonValue.h>
|
||||
#include <AK/Vector.h>
|
||||
|
@ -14,6 +15,9 @@
|
|||
namespace AK {
|
||||
|
||||
class JsonArray {
|
||||
template<typename Callback>
|
||||
using CallbackErrorType = decltype(declval<Callback>()(declval<JsonValue const&>()).release_error());
|
||||
|
||||
public:
|
||||
JsonArray() = default;
|
||||
~JsonArray() = default;
|
||||
|
@ -76,6 +80,14 @@ public:
|
|||
callback(value);
|
||||
}
|
||||
|
||||
template<FallibleFunction<JsonValue const&> Callback>
|
||||
ErrorOr<void, CallbackErrorType<Callback>> try_for_each(Callback&& callback) const
|
||||
{
|
||||
for (auto const& value : m_values)
|
||||
TRY(callback(value));
|
||||
return {};
|
||||
}
|
||||
|
||||
[[nodiscard]] Vector<JsonValue> const& values() const { return m_values; }
|
||||
|
||||
void ensure_capacity(size_t capacity) { m_values.ensure_capacity(capacity); }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue