use std::borrow::Cow;
use async_graphql::{
InputObjectType, InputType, InputValueError, InputValueResult, Object, OutputType, TypeName,
Value,
indexmap::IndexMap,
registry::{MetaInputValue, MetaType, MetaTypeId, Registry},
};
use crate::{
BoundingBox, BoundingBoxFilter, ConfidenceFilter, NullableFace, Score, Sent, TextFilter,
ToGraphqlOutput, graphql::output::composed_tail, sent,
};
use super::{
BarcodeDetection, BarcodeDetectionFilter, Detection, DetectionFilter, DocumentSegment,
DocumentSegmentFilter, Label, LabelWire, LocatedDetection, LocatedDetectionFilter, Symbology,
TextDetection, TextDetectionFilter,
};
#[cfg(test)]
mod tests;
const NO_DETECTION_SHORTHAND: &str = "a detection filter asks about FACETS: name the fields you mean — a bare value does not say \
whether it is the label, the confidence or the box, and there is no whole-detection operator \
for it to be";
#[Object]
impl Detection {
#[doc(hidden)]
#[graphql(name = "label")]
async fn __async_graphql_label__(&self) -> &str {
<Label as ToGraphqlOutput>::to_graphql_output(self.label())
}
#[doc(hidden)]
#[graphql(name = "confidence")]
async fn __async_graphql_confidence__(&self) -> crate::F32 {
<Score as ToGraphqlOutput>::to_graphql_output(&self.confidence())
}
}
impl ToGraphqlOutput for Detection {
type Output<'a> = &'a Self;
#[inline]
fn to_graphql_output(marked: &Self) -> &Self {
marked
}
}
impl<T> TypeName for LocatedDetection<T>
where
T: ToGraphqlOutput + Send + Sync + 'static,
for<'a> T::Output<'a>: OutputType,
{
#[inline]
fn type_name() -> Cow<'static, str> {
Cow::Owned(format!(
"LocatedDetection{}",
composed_tail(&<T::Output<'static> as OutputType>::type_name())
))
}
}
#[Object(name_type)]
impl<T> LocatedDetection<T>
where
T: ToGraphqlOutput + Copy + Send + Sync + 'static,
for<'a> T::Output<'a>: OutputType,
{
#[doc(hidden)]
#[graphql(name = "label")]
async fn __async_graphql_label__(&self) -> &str {
<Label as ToGraphqlOutput>::to_graphql_output(self.label())
}
#[doc(hidden)]
#[graphql(name = "confidence")]
async fn __async_graphql_confidence__(&self) -> crate::F32 {
<Score as ToGraphqlOutput>::to_graphql_output(&self.confidence())
}
#[doc(hidden)]
#[graphql(name = "bbox")]
async fn __async_graphql_bbox__(&self) -> &BoundingBox<T> {
self.bbox()
}
}
impl<T> ToGraphqlOutput for LocatedDetection<T> {
type Output<'a>
= &'a Self
where
T: 'a;
#[inline]
fn to_graphql_output(marked: &Self) -> &Self {
marked
}
}
fn detection_fields(registry: &mut Registry) -> IndexMap<String, MetaInputValue> {
crate::__ingraph_filter_input_fields!(
registry,
"label" : Option<TextFilter<Label, LabelWire>>,
"confidence" : Option<ConfidenceFilter>,
)
}
fn detection_values(filter: &DetectionFilter) -> IndexMap<async_graphql::Name, Value> {
crate::__ingraph_filter_value_map!(filter, "label" : label, "confidence" : confidence)
}
fn detection_facets(
object: &IndexMap<async_graphql::Name, Value>,
) -> InputValueResult<DetectionFilter> {
Ok(DetectionFilter {
label: InputType::parse(object.get("label").cloned()).map_err(InputValueError::propagate)?,
confidence: InputType::parse(object.get("confidence").cloned())
.map_err(InputValueError::propagate)?,
})
}
impl InputType for DetectionFilter {
type RawValueType = Self;
#[inline]
fn type_name() -> Cow<'static, str> {
Cow::Borrowed("DetectionFilter")
}
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_DETECTION_SHORTHAND)),
Sent::Null => return Err(InputValueError::expected_type(Value::Null)),
Sent::Refused(refused) => return Err(InputValueError::expected_type(refused)),
};
detection_facets(&object)
}
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 InputObjectType for DetectionFilter {}
impl NullableFace for DetectionFilter {
fn bare_fields(registry: &mut Registry) -> IndexMap<String, MetaInputValue> {
detection_fields(registry)
}
fn bare_values(&self) -> IndexMap<async_graphql::Name, Value> {
detection_values(self)
}
}
fn located_fields<CF>(registry: &mut Registry) -> IndexMap<String, MetaInputValue>
where
CF: InputType,
{
let mut fields = detection_fields(registry);
fields.extend(crate::__ingraph_filter_input_fields!(
registry,
"bbox" : Option<BoundingBoxFilter<CF>>,
));
fields
}
fn located_values<CF>(filter: &LocatedDetectionFilter<CF>) -> IndexMap<async_graphql::Name, Value>
where
CF: InputType,
{
let mut values = detection_values(&filter.detection);
values.extend(crate::__ingraph_filter_value_map!(filter, "bbox" : bbox));
values
}
impl<CF> InputType for LocatedDetectionFilter<CF>
where
CF: InputType,
{
type RawValueType = Self;
#[inline]
fn type_name() -> Cow<'static, str> {
Cow::Owned(format!(
"LocatedDetection{}",
<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_DETECTION_SHORTHAND)),
Sent::Null => return Err(InputValueError::expected_type(Value::Null)),
Sent::Refused(refused) => return Err(InputValueError::expected_type(refused)),
};
Ok(Self {
detection: detection_facets(&object).map_err(InputValueError::propagate)?,
bbox: InputType::parse(object.get("bbox").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 LocatedDetectionFilter<CF> {}
impl<CF: InputType> NullableFace for LocatedDetectionFilter<CF> {
fn bare_fields(registry: &mut Registry) -> IndexMap<String, MetaInputValue> {
located_fields::<CF>(registry)
}
fn bare_values(&self) -> IndexMap<async_graphql::Name, Value> {
located_values(self)
}
}
#[async_graphql::Scalar(name = "Symbology")]
impl async_graphql::ScalarType for Symbology {
#[inline]
fn parse(value: Value) -> InputValueResult<Self> {
match &value {
Value::String(text) => Ok(Self::new(text)),
_ => Err(InputValueError::expected_type(value.clone())),
}
}
#[inline]
fn is_valid(value: &Value) -> bool {
matches!(value, Value::String(_))
}
#[inline]
fn to_value(&self) -> Value {
Value::String(String::from(self.as_str()))
}
}
impl<T> TypeName for TextDetection<T>
where
T: ToGraphqlOutput + Send + Sync + 'static,
for<'a> T::Output<'a>: OutputType,
{
#[inline]
fn type_name() -> Cow<'static, str> {
Cow::Owned(format!(
"TextDetection{}",
composed_tail(&<T::Output<'static> as OutputType>::type_name())
))
}
}
#[Object(name_type)]
impl<T> TextDetection<T>
where
T: ToGraphqlOutput + Copy + Send + Sync + 'static,
for<'a> T::Output<'a>: OutputType,
{
#[doc(hidden)]
#[graphql(name = "text")]
async fn __async_graphql_text__(&self) -> &str {
self.text()
}
#[doc(hidden)]
#[graphql(name = "confidence")]
async fn __async_graphql_confidence__(&self) -> crate::F32 {
<Score as ToGraphqlOutput>::to_graphql_output(&self.confidence())
}
#[doc(hidden)]
#[graphql(name = "bbox")]
async fn __async_graphql_bbox__(&self) -> &BoundingBox<T> {
self.bbox()
}
}
impl<T> ToGraphqlOutput for TextDetection<T> {
type Output<'a>
= &'a Self
where
T: 'a;
#[inline]
fn to_graphql_output(marked: &Self) -> &Self {
marked
}
}
impl<T> TypeName for BarcodeDetection<T>
where
T: ToGraphqlOutput + Send + Sync + 'static,
for<'a> T::Output<'a>: OutputType,
{
#[inline]
fn type_name() -> Cow<'static, str> {
Cow::Owned(format!(
"BarcodeDetection{}",
composed_tail(&<T::Output<'static> as OutputType>::type_name())
))
}
}
#[Object(name_type)]
impl<T> BarcodeDetection<T>
where
T: ToGraphqlOutput + Copy + Send + Sync + 'static,
for<'a> T::Output<'a>: OutputType,
{
#[doc(hidden)]
#[graphql(name = "payload")]
async fn __async_graphql_payload__(&self) -> &str {
self.payload()
}
#[doc(hidden)]
#[graphql(name = "symbology")]
async fn __async_graphql_symbology__(&self) -> Symbology {
self.symbology().clone()
}
#[doc(hidden)]
#[graphql(name = "confidence")]
async fn __async_graphql_confidence__(&self) -> crate::F32 {
<Score as ToGraphqlOutput>::to_graphql_output(&self.confidence())
}
#[doc(hidden)]
#[graphql(name = "bbox")]
async fn __async_graphql_bbox__(&self) -> &BoundingBox<T> {
self.bbox()
}
}
impl<T> ToGraphqlOutput for BarcodeDetection<T> {
type Output<'a>
= &'a Self
where
T: 'a;
#[inline]
fn to_graphql_output(marked: &Self) -> &Self {
marked
}
}
impl<T> TypeName for DocumentSegment<T>
where
T: ToGraphqlOutput + Send + Sync + 'static,
for<'a> T::Output<'a>: OutputType,
{
#[inline]
fn type_name() -> Cow<'static, str> {
Cow::Owned(format!(
"DocumentSegment{}",
composed_tail(&<T::Output<'static> as OutputType>::type_name())
))
}
}
#[Object(name_type)]
impl<T> DocumentSegment<T>
where
T: ToGraphqlOutput + Copy + Send + Sync + 'static,
for<'a> T::Output<'a>: OutputType,
{
#[doc(hidden)]
#[graphql(name = "quad")]
async fn __async_graphql_quad__(&self) -> &crate::Quad<T> {
self.quad()
}
#[doc(hidden)]
#[graphql(name = "confidence")]
async fn __async_graphql_confidence__(&self) -> crate::F32 {
<Score as ToGraphqlOutput>::to_graphql_output(&self.confidence())
}
}
impl<T> ToGraphqlOutput for DocumentSegment<T> {
type Output<'a>
= &'a Self
where
T: 'a;
#[inline]
fn to_graphql_output(marked: &Self) -> &Self {
marked
}
}
fn text_fields<CF: InputType>(registry: &mut Registry) -> IndexMap<String, MetaInputValue> {
crate::__ingraph_filter_input_fields!(
registry,
"text" : Option<TextFilter<smol_bytes::Utf8Bytes>>,
"confidence" : Option<ConfidenceFilter>,
"bbox" : Option<BoundingBoxFilter<CF>>,
)
}
fn text_values<CF: InputType>(
filter: &TextDetectionFilter<CF>,
) -> IndexMap<async_graphql::Name, Value> {
crate::__ingraph_filter_value_map!(
filter,
"text" : text,
"confidence" : confidence,
"bbox" : bbox
)
}
impl<CF: InputType> InputType for TextDetectionFilter<CF> {
type RawValueType = Self;
#[inline]
fn type_name() -> Cow<'static, str> {
Cow::Owned(format!("TextDetection{}", <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_DETECTION_SHORTHAND)),
Sent::Null => return Err(InputValueError::expected_type(Value::Null)),
Sent::Refused(refused) => return Err(InputValueError::expected_type(refused)),
};
Ok(Self {
text: InputType::parse(object.get("text").cloned()).map_err(InputValueError::propagate)?,
confidence: InputType::parse(object.get("confidence").cloned())
.map_err(InputValueError::propagate)?,
bbox: InputType::parse(object.get("bbox").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 TextDetectionFilter<CF> {}
impl<CF: InputType> NullableFace for TextDetectionFilter<CF> {
fn bare_fields(registry: &mut Registry) -> IndexMap<String, MetaInputValue> {
text_fields::<CF>(registry)
}
fn bare_values(&self) -> IndexMap<async_graphql::Name, Value> {
text_values(self)
}
}
fn barcode_fields<CF: InputType>(registry: &mut Registry) -> IndexMap<String, MetaInputValue> {
crate::__ingraph_filter_input_fields!(
registry,
"payload" : Option<TextFilter<smol_bytes::Utf8Bytes>>,
"symbology" : Option<crate::EnumFilter<Symbology>>,
"confidence" : Option<ConfidenceFilter>,
"bbox" : Option<BoundingBoxFilter<CF>>,
)
}
fn barcode_values<CF: InputType>(
filter: &BarcodeDetectionFilter<CF>,
) -> IndexMap<async_graphql::Name, Value> {
crate::__ingraph_filter_value_map!(
filter,
"payload" : payload,
"symbology" : symbology,
"confidence" : confidence,
"bbox" : bbox
)
}
impl<CF: InputType> InputType for BarcodeDetectionFilter<CF> {
type RawValueType = Self;
#[inline]
fn type_name() -> Cow<'static, str> {
Cow::Owned(format!(
"BarcodeDetection{}",
<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_DETECTION_SHORTHAND)),
Sent::Null => return Err(InputValueError::expected_type(Value::Null)),
Sent::Refused(refused) => return Err(InputValueError::expected_type(refused)),
};
Ok(Self {
payload: InputType::parse(object.get("payload").cloned())
.map_err(InputValueError::propagate)?,
symbology: InputType::parse(object.get("symbology").cloned())
.map_err(InputValueError::propagate)?,
confidence: InputType::parse(object.get("confidence").cloned())
.map_err(InputValueError::propagate)?,
bbox: InputType::parse(object.get("bbox").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 BarcodeDetectionFilter<CF> {}
impl<CF: InputType> NullableFace for BarcodeDetectionFilter<CF> {
fn bare_fields(registry: &mut Registry) -> IndexMap<String, MetaInputValue> {
barcode_fields::<CF>(registry)
}
fn bare_values(&self) -> IndexMap<async_graphql::Name, Value> {
barcode_values(self)
}
}
fn segment_fields<CF: InputType>(registry: &mut Registry) -> IndexMap<String, MetaInputValue> {
crate::__ingraph_filter_input_fields!(
registry,
"quad" : Option<crate::QuadFilter<CF>>,
"confidence" : Option<ConfidenceFilter>,
)
}
fn segment_values<CF: InputType>(
filter: &DocumentSegmentFilter<CF>,
) -> IndexMap<async_graphql::Name, Value> {
crate::__ingraph_filter_value_map!(filter, "quad" : quad, "confidence" : confidence)
}
impl<CF: InputType> InputType for DocumentSegmentFilter<CF> {
type RawValueType = Self;
#[inline]
fn type_name() -> Cow<'static, str> {
Cow::Owned(format!("DocumentSegment{}", <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_DETECTION_SHORTHAND)),
Sent::Null => return Err(InputValueError::expected_type(Value::Null)),
Sent::Refused(refused) => return Err(InputValueError::expected_type(refused)),
};
Ok(Self {
quad: InputType::parse(object.get("quad").cloned()).map_err(InputValueError::propagate)?,
confidence: InputType::parse(object.get("confidence").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 DocumentSegmentFilter<CF> {}
impl<CF: InputType> NullableFace for DocumentSegmentFilter<CF> {
fn bare_fields(registry: &mut Registry) -> IndexMap<String, MetaInputValue> {
segment_fields::<CF>(registry)
}
fn bare_values(&self) -> IndexMap<async_graphql::Name, Value> {
segment_values(self)
}
}