use super::*;
#[derive(Debug, Clone, PartialEq)]
pub struct RefFilter<F, R> {
pub(crate) column: F,
pub(crate) related: Option<Box<R>>,
}
impl<F, R> RefFilter<F, R> {
#[inline]
pub const fn new(column: F) -> Self {
Self {
column,
related: None,
}
}
#[inline]
pub const fn column(&self) -> &F {
&self.column
}
#[inline]
pub fn related(&self) -> Option<&R> {
self.related.as_deref()
}
#[must_use]
#[inline]
pub fn with_related(mut self, related: R) -> Self {
self.related = Some(Box::new(related));
self
}
#[inline]
pub fn into_parts(self) -> (F, Option<Box<R>>) {
(self.column, self.related)
}
pub fn try_map<G, E>(self, f: impl FnOnce(F) -> Result<G, E>) -> Result<RefFilter<G, R>, E> {
Ok(RefFilter {
column: f(self.column)?,
related: self.related,
})
}
}
impl<F: GraphqlFilter, R> RefFilter<F, R> {
#[inline]
pub fn is_empty(&self) -> bool {
self.related.is_none() && self.column.is_empty()
}
}
impl<F: Default, R> Default for RefFilter<F, R> {
#[inline]
fn default() -> Self {
Self::new(F::default())
}
}
impl<F: GraphqlFilter, R> GraphqlFilter for RefFilter<F, R> {
#[inline]
fn is_empty(&self) -> bool {
Self::is_empty(self)
}
}
impl<F: TryMapFilter<D, V>, R, D, V> TryMapFilter<D, V> for RefFilter<F, R> {
type Mapped = RefFilter<F::Mapped, R>;
#[inline]
fn try_map_filter<E, G>(self, f: G) -> Result<Self::Mapped, E>
where
G: FnMut(D) -> Result<V, E>,
{
self.try_map(|column| column.try_map_filter(f))
}
}
impl<F: EqualsFilter<T>, R, T> EqualsFilter<T> for RefFilter<F, R> {
#[inline]
fn equals(value: T) -> Self {
Self::new(F::equals(value))
}
}
#[cfg(feature = "async-graphql")]
impl<F: async_graphql::InputType, R: async_graphql::InputType> async_graphql::InputType
for RefFilter<F, R>
{
type RawValueType = Self;
fn type_name() -> ::std::borrow::Cow<'static, str> {
let (column, target) = (F::type_name(), R::type_name());
::std::borrow::Cow::Owned(::std::format!(
"{}Ref{}Filter",
super::operand_of(&column),
super::operand_of(&target),
))
}
fn create_type_info(registry: &mut async_graphql::registry::Registry) -> ::std::string::String {
registry.create_input_type::<Self, _>(
async_graphql::registry::MetaTypeId::InputObject,
|registry| {
let column = <F as async_graphql::InputType>::create_type_info(registry);
let mut input_fields = match registry.types.get(column.trim_end_matches('!')) {
::std::option::Option::Some(async_graphql::registry::MetaType::InputObject {
input_fields,
..
}) => input_fields.clone(),
_ => async_graphql::indexmap::IndexMap::new(),
};
input_fields.extend(__ingraph_filter_input_fields!(
registry,
"related" : ::core::option::Option<R>
));
async_graphql::registry::MetaType::InputObject {
name: ::std::borrow::Cow::into_owned(Self::type_name()),
description: ::std::option::Option::None,
input_fields,
visible: ::std::option::Option::None,
inaccessible: false,
tags: ::std::vec::Vec::new(),
rust_typename: ::std::option::Option::Some(::std::any::type_name::<Self>()),
oneof: false,
directive_invocations: ::std::vec::Vec::new(),
}
},
)
}
fn parse(
value: ::std::option::Option<async_graphql::Value>,
) -> async_graphql::InputValueResult<Self> {
if let ::std::option::Option::Some(async_graphql::Value::Object(object)) = value {
let related = <::core::option::Option<R> as async_graphql::InputType>::parse(
object.get("related").cloned(),
)
.map_err(async_graphql::InputValueError::propagate)?
.map(::std::boxed::Box::new);
let column = <F as async_graphql::InputType>::parse(::std::option::Option::Some(
async_graphql::Value::Object(object),
))
.map_err(async_graphql::InputValueError::propagate)?;
return ::std::result::Result::Ok(Self { column, related });
}
<F as async_graphql::InputType>::parse(value)
.map(Self::new)
.map_err(async_graphql::InputValueError::propagate)
}
fn to_value(&self) -> async_graphql::Value {
let mut map = match async_graphql::InputType::to_value(&self.column) {
async_graphql::Value::Object(map) => map,
_ => async_graphql::indexmap::IndexMap::new(),
};
map.insert(
async_graphql::Name::new("related"),
match &self.related {
::std::option::Option::Some(related) => async_graphql::InputType::to_value(&**related),
::std::option::Option::None => async_graphql::Value::Null,
},
);
async_graphql::Value::Object(map)
}
#[inline]
fn as_raw_value(&self) -> ::std::option::Option<&Self::RawValueType> {
::std::option::Option::Some(self)
}
}
#[cfg(feature = "async-graphql")]
impl<F: async_graphql::InputType, R: async_graphql::InputType> async_graphql::InputObjectType
for RefFilter<F, R>
{
}
#[cfg(test)]
mod tests;