use std::path::PathBuf;
use crate::{
assertion::traits::{Equality, JsonSchema},
assertion::Assertion,
dsl::expression::Predicate::{self, Is, IsNot, Schema},
LogSettings,
};
use serde_json::Value;
pub trait JsonBodyDsl<T> {
fn is(&self, actual: T) -> Assertion<Value>;
fn is_not(&self, actual: T) -> Assertion<Value>;
fn schema(&self, schema: T) -> Assertion<Value>;
fn eval(
&self,
actual: T,
predicate: Predicate,
log_settings: &LogSettings,
) -> Assertion<Value> {
match predicate {
Is => self.is(actual).assert(log_settings),
IsNot => self.is_not(actual).assert(log_settings),
Schema => self.schema(actual).assert(log_settings),
_ => unimplemented!("Invalid predicate for the json body DSL: {predicate}"),
}
}
}
impl JsonBodyDsl<Value> for Value {
fn is(&self, actual: Value) -> Assertion<Value> {
actual.is_eq(self)
}
fn is_not(&self, actual: Value) -> Assertion<Value> {
actual.is_ne(self)
}
fn schema(&self, actual: Value) -> Assertion<Value> {
actual.matches_schema(self)
}
}
impl JsonBodyDsl<Value> for &str {
fn is(&self, actual: Value) -> Assertion<Value> {
actual.is_eq(*self)
}
fn is_not(&self, actual: Value) -> Assertion<Value> {
actual.is_ne(*self)
}
fn schema(&self, actual: Value) -> Assertion<Value> {
actual.matches_schema(*self)
}
}
impl JsonBodyDsl<Value> for String {
fn is(&self, actual: Value) -> Assertion<Value> {
actual.is_eq(self)
}
fn is_not(&self, actual: Value) -> Assertion<Value> {
actual.is_ne(self)
}
fn schema(&self, actual: Value) -> Assertion<Value> {
actual.matches_schema(self)
}
}
impl JsonBodyDsl<Value> for PathBuf {
fn is(&self, actual: Value) -> Assertion<Value> {
actual.is_eq(self)
}
fn is_not(&self, actual: Value) -> Assertion<Value> {
actual.is_ne(self)
}
fn schema(&self, actual: Value) -> Assertion<Value> {
actual.matches_schema(self)
}
}