mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
LibWeb: Add a few more helpers to extract WebDriver properties
This commit is contained in:
parent
8944c6db3f
commit
71c1a1d8f4
Notes:
github-actions[bot]
2024-10-01 09:03:57 +00:00
Author: https://github.com/trflynn89 Commit: https://github.com/LadybirdBrowser/ladybird/commit/71c1a1d8f4d Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1566
1 changed files with 46 additions and 8 deletions
|
@ -15,12 +15,9 @@
|
|||
namespace Web::WebDriver {
|
||||
|
||||
template<typename PropertyType = ByteString>
|
||||
static ErrorOr<PropertyType, WebDriver::Error> get_property(JsonValue const& payload, StringView key)
|
||||
static ErrorOr<PropertyType, WebDriver::Error> get_property(JsonObject const& payload, StringView key)
|
||||
{
|
||||
if (!payload.is_object())
|
||||
return WebDriver::Error::from_code(ErrorCode::InvalidArgument, "Payload is not a JSON object");
|
||||
|
||||
auto property = payload.as_object().get(key);
|
||||
auto property = payload.get(key);
|
||||
|
||||
if (!property.has_value())
|
||||
return WebDriver::Error::from_code(ErrorCode::InvalidArgument, ByteString::formatted("No property called '{}' present", key));
|
||||
|
@ -33,9 +30,13 @@ static ErrorOr<PropertyType, WebDriver::Error> get_property(JsonValue const& pay
|
|||
if (!property->is_bool())
|
||||
return WebDriver::Error::from_code(ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' is not a Boolean", key));
|
||||
return property->as_bool();
|
||||
} else if constexpr (IsSame<PropertyType, u32>) {
|
||||
if (auto maybe_u32 = property->get_u32(); maybe_u32.has_value())
|
||||
return *maybe_u32;
|
||||
} else if constexpr (IsIntegral<PropertyType>) {
|
||||
if (auto maybe_number = property->get_integer<PropertyType>(); maybe_number.has_value())
|
||||
return *maybe_number;
|
||||
return WebDriver::Error::from_code(ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' is not an Integer", key));
|
||||
} else if constexpr (IsSame<PropertyType, double>) {
|
||||
if (auto maybe_number = property->get_double_with_precision_loss(); maybe_number.has_value())
|
||||
return *maybe_number;
|
||||
return WebDriver::Error::from_code(ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' is not a Number", key));
|
||||
} else if constexpr (IsSame<PropertyType, JsonArray const*>) {
|
||||
if (!property->is_array())
|
||||
|
@ -51,4 +52,41 @@ static ErrorOr<PropertyType, WebDriver::Error> get_property(JsonValue const& pay
|
|||
}
|
||||
}
|
||||
|
||||
template<typename PropertyType = ByteString>
|
||||
static ErrorOr<PropertyType, WebDriver::Error> get_property(JsonValue const& payload, StringView key)
|
||||
{
|
||||
if (!payload.is_object())
|
||||
return WebDriver::Error::from_code(ErrorCode::InvalidArgument, "Payload is not a JSON object");
|
||||
return get_property<PropertyType>(payload.as_object(), key);
|
||||
}
|
||||
|
||||
template<typename PropertyType = ByteString>
|
||||
static ErrorOr<Optional<PropertyType>, WebDriver::Error> get_optional_property(JsonObject const& object, StringView key)
|
||||
{
|
||||
if (!object.has(key))
|
||||
return OptionalNone {};
|
||||
return get_property<PropertyType>(object, key);
|
||||
}
|
||||
|
||||
template<Arithmetic PropertyType>
|
||||
static ErrorOr<PropertyType, WebDriver::Error> get_property_with_limits(JsonObject const& object, StringView key, Optional<PropertyType> min, Optional<PropertyType> max)
|
||||
{
|
||||
auto value = TRY(get_property<PropertyType>(object, key));
|
||||
|
||||
if (min.has_value() && value < *min)
|
||||
return WebDriver::Error::from_code(WebDriver::ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' must not be less than {}", key, *min));
|
||||
if (max.has_value() && value > *max)
|
||||
return WebDriver::Error::from_code(WebDriver::ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' must not be greater than {}", key, *max));
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
template<Arithmetic PropertyType>
|
||||
static ErrorOr<Optional<PropertyType>, WebDriver::Error> get_optional_property_with_limits(JsonObject const& object, StringView key, Optional<PropertyType> min, Optional<PropertyType> max)
|
||||
{
|
||||
if (!object.has(key))
|
||||
return OptionalNone {};
|
||||
return get_property_with_limits<PropertyType>(object, key, min, max);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue