use crate::{implementation, private, Asserter};
use serde_json::{Map, Number, Value};
pub trait JsonValueAssertion: private::Sealed {
#[track_caller]
#[allow(clippy::wrong_self_convention)]
fn is_null(self);
#[track_caller]
#[allow(clippy::wrong_self_convention)]
fn is_boolean(self) -> Asserter<bool>;
#[track_caller]
#[allow(clippy::wrong_self_convention)]
fn is_number(self) -> Asserter<Number>;
#[track_caller]
#[allow(clippy::wrong_self_convention)]
fn is_string(self) -> Asserter<String>;
#[track_caller]
#[allow(clippy::wrong_self_convention)]
fn is_array(self) -> Asserter<Vec<Value>>;
#[track_caller]
#[allow(clippy::wrong_self_convention)]
fn is_object(self) -> Asserter<Map<String, Value>>;
}
impl JsonValueAssertion for Asserter<Value> {
fn is_null(self) {
implementation::assert_no_expected(self.value.is_null(), self.value, "JSON to be null");
}
fn is_boolean(self) -> Asserter<bool> {
implementation::assert_no_expected(
self.value.is_boolean(),
&self.value,
"JSON to be a boolean value",
);
#[allow(clippy::unreachable)]
let Value::Bool(value) = self.value
else {
unreachable!()
};
Asserter { value }
}
fn is_number(self) -> Asserter<Number> {
implementation::assert_no_expected(
self.value.is_number(),
&self.value,
"JSON to be a number",
);
#[allow(clippy::unreachable)]
let Value::Number(value) = self.value
else {
unreachable!()
};
Asserter { value }
}
fn is_string(self) -> Asserter<String> {
implementation::assert_no_expected(
self.value.is_string(),
&self.value,
"JSON to be a string",
);
#[allow(clippy::unreachable)]
let Value::String(value) = self.value
else {
unreachable!()
};
Asserter { value }
}
fn is_array(self) -> Asserter<Vec<Value>> {
implementation::assert_no_expected(
self.value.is_array(),
&self.value,
"JSON to be an array",
);
#[allow(clippy::unreachable)]
let Value::Array(value) = self.value
else {
unreachable!()
};
Asserter { value }
}
fn is_object(self) -> Asserter<Map<String, Value>> {
implementation::assert_no_expected(
self.value.is_object(),
&self.value,
"JSON to be an object",
);
#[allow(clippy::unreachable)]
let Value::Object(value) = self.value
else {
unreachable!()
};
Asserter { value }
}
}
pub trait JsonObjectAssertion: private::Sealed {
fn get(self, key: &str) -> Asserter<Value>;
}
impl JsonObjectAssertion for Asserter<Map<String, Value>> {
fn get(mut self, key: &str) -> Asserter<Value> {
let maybe_item = self.value.remove(key);
implementation::assert(maybe_item.is_some(), &self.value, "to have the key", key);
#[allow(clippy::unwrap_used)]
let item = maybe_item.unwrap();
Asserter { value: item }
}
}