use std::borrow::Cow;
use async_graphql::{
InputObjectType, InputType, InputValueError, InputValueResult, Object, OutputType, TypeName,
Value,
registry::{MetaInputValue, MetaType, MetaTypeId, Registry},
};
use crate::{NullableFace, Sent, ToGraphqlOutput, graphql::output::composed_tail, sent};
use super::{BoundingBox, BoundingBoxFilter, Point2, Point2Filter, Quad, QuadFilter};
const NO_GEOMETRY_SHORTHAND: &str = "a geometry filter asks about COMPONENTS: name the fields you mean — a bare value does not say \
which of them it is asking about, and there is no whole-shape operator for it to be";
impl<T> TypeName for Point2<T>
where
T: ToGraphqlOutput + Send + Sync + 'static,
for<'a> T::Output<'a>: OutputType,
{
#[inline]
fn type_name() -> Cow<'static, str> {
Cow::Owned(format!(
"Point2{}",
composed_tail(&<T::Output<'static> as OutputType>::type_name())
))
}
}
#[Object(name_type)]
impl<T> Point2<T>
where
T: ToGraphqlOutput + Copy + Send + Sync + 'static,
for<'a> T::Output<'a>: OutputType,
{
#[doc(hidden)]
#[graphql(name = "x")]
async fn __async_graphql_x__(&self) -> T::Output<'_> {
T::to_graphql_output(&self.x)
}
#[doc(hidden)]
#[graphql(name = "y")]
async fn __async_graphql_y__(&self) -> T::Output<'_> {
T::to_graphql_output(&self.y)
}
}
impl<T> ToGraphqlOutput for Point2<T> {
type Output<'a>
= &'a Self
where
T: 'a;
#[inline]
fn to_graphql_output(marked: &Self) -> &Self {
marked
}
}
impl<T> TypeName for BoundingBox<T>
where
T: ToGraphqlOutput + Send + Sync + 'static,
for<'a> T::Output<'a>: OutputType,
{
#[inline]
fn type_name() -> Cow<'static, str> {
Cow::Owned(format!(
"BoundingBox{}",
composed_tail(&<T::Output<'static> as OutputType>::type_name())
))
}
}
#[Object(name_type)]
impl<T> BoundingBox<T>
where
T: ToGraphqlOutput + Copy + Send + Sync + 'static,
for<'a> T::Output<'a>: OutputType,
{
#[doc(hidden)]
#[graphql(name = "x")]
async fn __async_graphql_x__(&self) -> T::Output<'_> {
T::to_graphql_output(&self.x)
}
#[doc(hidden)]
#[graphql(name = "y")]
async fn __async_graphql_y__(&self) -> T::Output<'_> {
T::to_graphql_output(&self.y)
}
#[doc(hidden)]
#[graphql(name = "width")]
async fn __async_graphql_width__(&self) -> T::Output<'_> {
T::to_graphql_output(&self.width)
}
#[doc(hidden)]
#[graphql(name = "height")]
async fn __async_graphql_height__(&self) -> T::Output<'_> {
T::to_graphql_output(&self.height)
}
}
impl<T> ToGraphqlOutput for BoundingBox<T> {
type Output<'a>
= &'a Self
where
T: 'a;
#[inline]
fn to_graphql_output(marked: &Self) -> &Self {
marked
}
}
impl<T> TypeName for Quad<T>
where
T: ToGraphqlOutput + Send + Sync + 'static,
for<'a> T::Output<'a>: OutputType,
{
#[inline]
fn type_name() -> Cow<'static, str> {
Cow::Owned(format!(
"Quad{}",
composed_tail(&<T::Output<'static> as OutputType>::type_name())
))
}
}
#[Object(name_type)]
impl<T> Quad<T>
where
T: ToGraphqlOutput + Copy + Send + Sync + 'static,
for<'a> T::Output<'a>: OutputType,
{
#[doc(hidden)]
#[graphql(name = "topLeft")]
async fn __async_graphql_top_left__(&self) -> Point2<T> {
self.top_left()
}
#[doc(hidden)]
#[graphql(name = "topRight")]
async fn __async_graphql_top_right__(&self) -> Point2<T> {
self.top_right()
}
#[doc(hidden)]
#[graphql(name = "bottomRight")]
async fn __async_graphql_bottom_right__(&self) -> Point2<T> {
self.bottom_right()
}
#[doc(hidden)]
#[graphql(name = "bottomLeft")]
async fn __async_graphql_bottom_left__(&self) -> Point2<T> {
self.bottom_left()
}
}
impl<T> ToGraphqlOutput for Quad<T> {
type Output<'a>
= &'a Self
where
T: 'a;
#[inline]
fn to_graphql_output(marked: &Self) -> &Self {
marked
}
}
fn point_fields<CF>(
registry: &mut Registry,
) -> async_graphql::indexmap::IndexMap<String, MetaInputValue>
where
CF: InputType,
{
crate::__ingraph_filter_input_fields!(registry, "x" : Option<CF>, "y" : Option<CF>,)
}
fn point_values<CF>(
filter: &Point2Filter<CF>,
) -> async_graphql::indexmap::IndexMap<async_graphql::Name, Value>
where
CF: InputType,
{
crate::__ingraph_filter_value_map!(filter, "x" : x, "y" : y)
}
impl<CF> InputType for Point2Filter<CF>
where
CF: InputType,
{
type RawValueType = Self;
#[inline]
fn type_name() -> Cow<'static, str> {
Cow::Owned(format!("Point2{}", <CF as InputType>::type_name()))
}
fn create_type_info(registry: &mut Registry) -> String {
registry.create_input_type::<Self, _>(MetaTypeId::InputObject, |registry| {
MetaType::InputObject {
name: String::from(<Self as InputType>::type_name()),
description: None,
input_fields: <Self as NullableFace>::bare_fields(registry),
visible: None,
inaccessible: false,
tags: Vec::new(),
rust_typename: Some(std::any::type_name::<Self>()),
oneof: false,
directive_invocations: Vec::new(),
}
})
}
fn parse(value: Option<Value>) -> InputValueResult<Self> {
let object = match sent(value) {
Sent::Longhand(object) => object,
Sent::Bare(_) => return Err(InputValueError::custom(NO_GEOMETRY_SHORTHAND)),
Sent::Null => return Err(InputValueError::expected_type(Value::Null)),
Sent::Refused(refused) => return Err(InputValueError::expected_type(refused)),
};
Ok(Self {
x: InputType::parse(object.get("x").cloned()).map_err(InputValueError::propagate)?,
y: InputType::parse(object.get("y").cloned()).map_err(InputValueError::propagate)?,
})
}
fn to_value(&self) -> Value {
Value::Object(<Self as NullableFace>::bare_values(self))
}
#[inline]
fn as_raw_value(&self) -> Option<&Self::RawValueType> {
Some(self)
}
}
impl<CF: InputType> InputObjectType for Point2Filter<CF> {}
impl<CF: InputType> NullableFace for Point2Filter<CF> {
fn bare_fields(
registry: &mut Registry,
) -> async_graphql::indexmap::IndexMap<String, MetaInputValue> {
point_fields::<CF>(registry)
}
fn bare_values(&self) -> async_graphql::indexmap::IndexMap<async_graphql::Name, Value> {
point_values(self)
}
}
fn box_fields<CF>(
registry: &mut Registry,
) -> async_graphql::indexmap::IndexMap<String, MetaInputValue>
where
CF: InputType,
{
crate::__ingraph_filter_input_fields!(
registry,
"x" : Option<CF>,
"y" : Option<CF>,
"width" : Option<CF>,
"height" : Option<CF>,
"area" : Option<CF>,
)
}
fn box_values<CF>(
filter: &BoundingBoxFilter<CF>,
) -> async_graphql::indexmap::IndexMap<async_graphql::Name, Value>
where
CF: InputType,
{
crate::__ingraph_filter_value_map!(
filter,
"x" : x,
"y" : y,
"width" : width,
"height" : height,
"area" : area
)
}
impl<CF> InputType for BoundingBoxFilter<CF>
where
CF: InputType,
{
type RawValueType = Self;
#[inline]
fn type_name() -> Cow<'static, str> {
Cow::Owned(format!("BoundingBox{}", <CF as InputType>::type_name()))
}
fn create_type_info(registry: &mut Registry) -> String {
registry.create_input_type::<Self, _>(MetaTypeId::InputObject, |registry| {
MetaType::InputObject {
name: String::from(<Self as InputType>::type_name()),
description: None,
input_fields: <Self as NullableFace>::bare_fields(registry),
visible: None,
inaccessible: false,
tags: Vec::new(),
rust_typename: Some(std::any::type_name::<Self>()),
oneof: false,
directive_invocations: Vec::new(),
}
})
}
fn parse(value: Option<Value>) -> InputValueResult<Self> {
let object = match sent(value) {
Sent::Longhand(object) => object,
Sent::Bare(_) => return Err(InputValueError::custom(NO_GEOMETRY_SHORTHAND)),
Sent::Null => return Err(InputValueError::expected_type(Value::Null)),
Sent::Refused(refused) => return Err(InputValueError::expected_type(refused)),
};
Ok(Self {
x: InputType::parse(object.get("x").cloned()).map_err(InputValueError::propagate)?,
y: InputType::parse(object.get("y").cloned()).map_err(InputValueError::propagate)?,
width: InputType::parse(object.get("width").cloned()).map_err(InputValueError::propagate)?,
height: InputType::parse(object.get("height").cloned())
.map_err(InputValueError::propagate)?,
area: InputType::parse(object.get("area").cloned()).map_err(InputValueError::propagate)?,
})
}
fn to_value(&self) -> Value {
Value::Object(<Self as NullableFace>::bare_values(self))
}
#[inline]
fn as_raw_value(&self) -> Option<&Self::RawValueType> {
Some(self)
}
}
impl<CF: InputType> InputObjectType for BoundingBoxFilter<CF> {}
impl<CF: InputType> NullableFace for BoundingBoxFilter<CF> {
fn bare_fields(
registry: &mut Registry,
) -> async_graphql::indexmap::IndexMap<String, MetaInputValue> {
box_fields::<CF>(registry)
}
fn bare_values(&self) -> async_graphql::indexmap::IndexMap<async_graphql::Name, Value> {
box_values(self)
}
}
fn quad_fields<CF>(
registry: &mut Registry,
) -> async_graphql::indexmap::IndexMap<String, MetaInputValue>
where
CF: InputType,
{
crate::__ingraph_filter_input_fields!(
registry,
"topLeft" : Option<Point2Filter<CF>>,
"topRight" : Option<Point2Filter<CF>>,
"bottomRight" : Option<Point2Filter<CF>>,
"bottomLeft" : Option<Point2Filter<CF>>,
)
}
fn quad_values<CF>(
filter: &QuadFilter<CF>,
) -> async_graphql::indexmap::IndexMap<async_graphql::Name, Value>
where
CF: InputType,
{
crate::__ingraph_filter_value_map!(
filter,
"topLeft" : top_left,
"topRight" : top_right,
"bottomRight" : bottom_right,
"bottomLeft" : bottom_left
)
}
impl<CF> InputType for QuadFilter<CF>
where
CF: InputType,
{
type RawValueType = Self;
#[inline]
fn type_name() -> Cow<'static, str> {
Cow::Owned(format!("Quad{}", <CF as InputType>::type_name()))
}
fn create_type_info(registry: &mut Registry) -> String {
registry.create_input_type::<Self, _>(MetaTypeId::InputObject, |registry| {
MetaType::InputObject {
name: String::from(<Self as InputType>::type_name()),
description: None,
input_fields: <Self as NullableFace>::bare_fields(registry),
visible: None,
inaccessible: false,
tags: Vec::new(),
rust_typename: Some(std::any::type_name::<Self>()),
oneof: false,
directive_invocations: Vec::new(),
}
})
}
fn parse(value: Option<Value>) -> InputValueResult<Self> {
let object = match sent(value) {
Sent::Longhand(object) => object,
Sent::Bare(_) => return Err(InputValueError::custom(NO_GEOMETRY_SHORTHAND)),
Sent::Null => return Err(InputValueError::expected_type(Value::Null)),
Sent::Refused(refused) => return Err(InputValueError::expected_type(refused)),
};
Ok(Self {
top_left: InputType::parse(object.get("topLeft").cloned())
.map_err(InputValueError::propagate)?,
top_right: InputType::parse(object.get("topRight").cloned())
.map_err(InputValueError::propagate)?,
bottom_right: InputType::parse(object.get("bottomRight").cloned())
.map_err(InputValueError::propagate)?,
bottom_left: InputType::parse(object.get("bottomLeft").cloned())
.map_err(InputValueError::propagate)?,
})
}
fn to_value(&self) -> Value {
Value::Object(<Self as NullableFace>::bare_values(self))
}
#[inline]
fn as_raw_value(&self) -> Option<&Self::RawValueType> {
Some(self)
}
}
impl<CF: InputType> InputObjectType for QuadFilter<CF> {}
impl<CF: InputType> NullableFace for QuadFilter<CF> {
fn bare_fields(
registry: &mut Registry,
) -> async_graphql::indexmap::IndexMap<String, MetaInputValue> {
quad_fields::<CF>(registry)
}
fn bare_values(&self) -> async_graphql::indexmap::IndexMap<async_graphql::Name, Value> {
quad_values(self)
}
}