LibWeb/IDB: Introduce an Invalid KeyType

This cleans up the code around failure/invalid/exception a bit
This commit is contained in:
stelar7 2025-04-25 17:59:06 +02:00 committed by Jelle Raaijmakers
commit facfcd87c2
Notes: github-actions[bot] 2025-04-28 09:32:57 +00:00
7 changed files with 68 additions and 56 deletions

View file

@ -31,6 +31,7 @@ class Key : public JS::Cell {
// A key has an associated type which is one of: number, date, string, binary, or array.
enum KeyType {
Invalid,
Number,
Date,
String,
@ -45,6 +46,8 @@ public:
[[nodiscard]] KeyType type() { return m_type; }
[[nodiscard]] KeyValue value() { return m_value; }
[[nodiscard]] bool is_invalid() { return m_type == Invalid; }
[[nodiscard]] double value_as_double() { return m_value.get<double>(); }
[[nodiscard]] AK::String value_as_string() { return m_value.get<AK::String>(); }
[[nodiscard]] ByteBuffer value_as_byte_buffer() { return m_value.get<ByteBuffer>(); }
@ -60,6 +63,7 @@ public:
[[nodiscard]] static GC::Ref<Key> create_string(JS::Realm& realm, AK::String const& value) { return create(realm, String, value); }
[[nodiscard]] static GC::Ref<Key> create_binary(JS::Realm& realm, ByteBuffer const& value) { return create(realm, Binary, value); }
[[nodiscard]] static GC::Ref<Key> create_array(JS::Realm& realm, Vector<GC::Root<Key>> const& value) { return create(realm, Array, value); }
[[nodiscard]] static GC::Ref<Key> create_invalid(JS::Realm& realm, AK::String const& value) { return create(realm, Invalid, value); }
[[nodiscard]] static i8 compare_two_keys(GC::Ref<Key> a, GC::Ref<Key> b);
[[nodiscard]] static bool equals(GC::Ref<Key> a, GC::Ref<Key> b) { return compare_two_keys(a, b) == 0; }