use super::*;
#[cfg(feature = "async-graphql")]
use crate::{
flags::{self, FlagsValue},
graphql::FlagsInput,
};
#[cfg(test)]
mod tests;
#[derive(Debug, Clone, PartialEq)]
pub struct FlagsFilter<T> {
pub(crate) eq: Option<T>,
pub(crate) ne: Option<T>,
pub(crate) in_: Option<Box<[T]>>,
pub(crate) not_in: Option<Box<[T]>>,
pub(crate) has_all: Option<T>,
pub(crate) has_any: Option<T>,
pub(crate) has_none: Option<T>,
}
impl<T> FlagsFilter<T> {
#[inline]
pub const fn new() -> Self {
Self {
eq: None,
ne: None,
in_: None,
not_in: None,
has_all: None,
has_any: None,
has_none: None,
}
}
#[inline]
pub const fn is_empty(&self) -> bool {
self.eq.is_none()
&& self.ne.is_none()
&& self.in_.is_none()
&& self.not_in.is_none()
&& self.has_all.is_none()
&& self.has_any.is_none()
&& self.has_none.is_none()
}
opt_accessor!(eq: T => with_eq, set_eq, maybe_eq, update_eq);
opt_accessor!(ne: T => with_ne, set_ne, maybe_ne, update_ne);
opt_accessor!(in_: [T] => with_in_, set_in_, maybe_in_, update_in_);
opt_accessor!(not_in: [T] => with_not_in, set_not_in, maybe_not_in, update_not_in);
opt_accessor!(has_all: T => with_has_all, set_has_all, maybe_has_all, update_has_all);
opt_accessor!(has_any: T => with_has_any, set_has_any, maybe_has_any, update_has_any);
opt_accessor!(has_none: T => with_has_none, set_has_none, maybe_has_none, update_has_none);
#[inline]
pub const fn is_referential(&self) -> bool {
(self.eq.is_some() ^ self.in_.is_some())
&& self.ne.is_none()
&& self.not_in.is_none()
&& self.has_all.is_none()
&& self.has_any.is_none()
&& self.has_none.is_none()
}
pub fn try_map<U, E, F>(self, mut f: F) -> Result<FlagsFilter<U>, E>
where
F: FnMut(T) -> Result<U, E>,
{
Ok(FlagsFilter {
eq: map_operand(self.eq, &mut f)?,
ne: map_operand(self.ne, &mut f)?,
in_: map_operands(self.in_, &mut f)?,
not_in: map_operands(self.not_in, &mut f)?,
has_all: map_operand(self.has_all, &mut f)?,
has_any: map_operand(self.has_any, &mut f)?,
has_none: map_operand(self.has_none, &mut f)?,
})
}
}
impl<T> Default for FlagsFilter<T> {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl<T, U> TryMapFilter<T, U> for FlagsFilter<T> {
type Mapped = FlagsFilter<U>;
#[inline]
fn try_map_filter<E, F>(self, f: F) -> Result<Self::Mapped, E>
where
F: FnMut(T) -> Result<U, E>,
{
self.try_map(f)
}
}
impl<T> EqualsFilter<T> for FlagsFilter<T> {
#[inline]
fn equals(value: T) -> Self {
Self::new().with_eq(value)
}
}
impl<T> GraphqlFilter for FlagsFilter<T> {
#[inline]
fn is_empty(&self) -> bool {
Self::is_empty(self)
}
}
#[cfg(feature = "async-graphql")]
impl<T: FlagsValue> FlagsFilter<T> {
fn fold_sentence(&mut self, asked: bool, against: T) -> bool {
let slot = if asked { &mut self.eq } else { &mut self.ne };
if slot.is_some() {
return false;
}
*slot = Some(against);
true
}
fn fold_flag(&mut self, asked: bool, bit: T) {
let slot = if asked {
&mut self.has_all
} else {
&mut self.has_none
};
*slot = Some(slot.map_or(bit, |held| held.union(bit)));
}
}
#[cfg(feature = "async-graphql")]
impl<T: FlagsValue> async_graphql::InputType for FlagsFilter<T> {
type RawValueType = Self;
fn type_name() -> ::std::borrow::Cow<'static, str> {
::std::borrow::Cow::Owned(::std::format!("{}Filter", T::GRAPHQL_NAME))
}
fn create_type_info(registry: &mut async_graphql::registry::Registry) -> ::std::string::String {
registry.create_input_type::<Self, _>(
async_graphql::registry::MetaTypeId::InputObject,
|registry| async_graphql::registry::MetaType::InputObject {
name: ::std::borrow::Cow::into_owned(Self::type_name()),
description: ::std::option::Option::None,
input_fields: <Self as crate::NullableFace>::bare_fields(registry),
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> {
let object = match super::sent(value) {
super::Sent::Longhand(object) => object,
super::Sent::Bare(bare) => {
let mut object = async_graphql::indexmap::IndexMap::new();
object.insert(async_graphql::Name::new("eq"), bare);
object
}
super::Sent::Null => {
return ::std::result::Result::Err(async_graphql::InputValueError::expected_type(
async_graphql::Value::Null,
));
}
super::Sent::Refused(sent) => {
return ::std::result::Result::Err(async_graphql::InputValueError::expected_type(sent));
}
};
let mut filter = Self {
eq: operand::<T, Self>(&object, "eq")?,
ne: operand::<T, Self>(&object, "ne")?,
in_: operands::<T, Self>(&object, "in")?,
not_in: operands::<T, Self>(&object, "notIn")?,
has_all: operand::<T, Self>(&object, "hasAll")?,
has_any: operand::<T, Self>(&object, "hasAny")?,
has_none: operand::<T, Self>(&object, "hasNone")?,
};
for (word, against) in [
("isEmpty", flags::empty::<T>()),
("isAll", flags::all::<T>()),
] {
let ::std::option::Option::Some(asked) =
parse_slot::<::std::option::Option<bool>, Self>(&object, word)?
else {
continue;
};
if !filter.fold_sentence(asked, against) {
return ::std::result::Result::Err(async_graphql::InputValueError::custom(::std::format!(
"`{word}` is `{}` compared against a value, and this filter already carries one — a \
conjunction of two equalities on one column is not something one slot can hold. Write \
the one you mean",
if asked { "eq" } else { "ne" }
)));
}
}
for bit in flags::bits::<T>() {
if let ::std::option::Option::Some(asked) =
parse_slot::<::std::option::Option<bool>, Self>(&object, bit.field)?
{
filter.fold_flag(asked, bit.value);
}
}
::std::result::Result::Ok(filter)
}
fn to_value(&self) -> async_graphql::Value {
async_graphql::Value::Object(<Self as crate::NullableFace>::bare_values(self))
}
#[inline]
fn as_raw_value(&self) -> ::std::option::Option<&Self::RawValueType> {
::std::option::Option::Some(self)
}
}
#[cfg(feature = "async-graphql")]
fn operand<T: FlagsValue, F: async_graphql::InputType>(
object: &async_graphql::indexmap::IndexMap<async_graphql::Name, async_graphql::Value>,
name: &str,
) -> Result<Option<T>, async_graphql::InputValueError<F>> {
Ok(parse_slot::<Option<FlagsInput<T>>, F>(object, name)?.map(FlagsInput::into_inner))
}
#[cfg(feature = "async-graphql")]
fn operands<T: FlagsValue, F: async_graphql::InputType>(
object: &async_graphql::indexmap::IndexMap<async_graphql::Name, async_graphql::Value>,
name: &str,
) -> Result<Option<Box<[T]>>, async_graphql::InputValueError<F>> {
Ok(
parse_slot::<Option<Box<[FlagsInput<T>]>>, F>(object, name)?.map(|carried| {
carried
.into_vec()
.into_iter()
.map(FlagsInput::into_inner)
.collect()
}),
)
}
#[cfg(feature = "async-graphql")]
impl<T: FlagsValue> async_graphql::InputObjectType for FlagsFilter<T> {}
#[cfg(feature = "async-graphql")]
impl<T: FlagsValue> crate::NullableFace for FlagsFilter<T> {
fn bare_fields(
registry: &mut async_graphql::registry::Registry,
) -> async_graphql::indexmap::IndexMap<
::std::string::String,
async_graphql::registry::MetaInputValue,
> {
let mut fields = __ingraph_filter_input_fields!(
registry,
"eq" : ::core::option::Option<FlagsInput<T>>,
"ne" : ::core::option::Option<FlagsInput<T>>,
"in" : ::core::option::Option<::std::boxed::Box<[FlagsInput<T>]>>,
"notIn" : ::core::option::Option<::std::boxed::Box<[FlagsInput<T>]>>,
"hasAll" : ::core::option::Option<FlagsInput<T>>,
"hasAny" : ::core::option::Option<FlagsInput<T>>,
"hasNone" : ::core::option::Option<FlagsInput<T>>,
"isEmpty" : ::core::option::Option<bool>,
"isAll" : ::core::option::Option<bool>,
);
let boolean =
<::core::option::Option<bool> as async_graphql::InputType>::create_type_info(registry);
for bit in flags::bits::<T>() {
fields.insert(
::std::string::String::from(bit.field),
async_graphql::registry::MetaInputValue {
name: ::std::string::String::from(bit.field),
description: ::std::option::Option::None,
ty: boolean.clone(),
deprecation: async_graphql::registry::Deprecation::NoDeprecated,
default_value: ::std::option::Option::None,
visible: ::std::option::Option::None,
inaccessible: false,
tags: ::std::vec::Vec::new(),
is_secret: false,
directive_invocations: ::std::vec::Vec::new(),
},
);
}
fields
}
fn bare_values(
&self,
) -> async_graphql::indexmap::IndexMap<async_graphql::Name, async_graphql::Value> {
let carried = FlagsFilter {
eq: self.eq.map(FlagsInput::new),
ne: self.ne.map(FlagsInput::new),
in_: self
.in_
.as_ref()
.map(|values| values.iter().copied().map(FlagsInput::new).collect()),
not_in: self
.not_in
.as_ref()
.map(|values| values.iter().copied().map(FlagsInput::new).collect()),
has_all: self.has_all.map(FlagsInput::new),
has_any: self.has_any.map(FlagsInput::new),
has_none: self.has_none.map(FlagsInput::new),
};
__ingraph_filter_value_map!(
carried,
"eq" : eq,
"ne" : ne,
"in" : in_,
"notIn" : not_in,
"hasAll" : has_all,
"hasAny" : has_any,
"hasNone" : has_none
)
}
}