pub mod subscription;
use std::convert::Infallible;
use derive_more::with_trait::Display;
use futures::future::{self, BoxFuture};
use crate::{FieldError, InputValue, ScalarValue, ToScalarValue};
pub trait ExtractError {
type Error;
}
impl<T, E> ExtractError for Result<T, E> {
type Error = E;
}
pub fn err_fut<'ok, D, Ok, S>(msg: D) -> BoxFuture<'ok, Result<Ok, FieldError<S>>>
where
D: Display,
Ok: Send + 'ok,
S: Send + 'static,
{
Box::pin(future::err(FieldError::from(msg)))
}
pub fn err_unnamed_type<S>(name: &str) -> FieldError<S> {
FieldError::from(format!(
"Expected `{name}` type to implement `GraphQLType::name`",
))
}
pub fn err_unnamed_type_fut<'ok, Ok, S>(name: &str) -> BoxFuture<'ok, Result<Ok, FieldError<S>>>
where
Ok: Send + 'ok,
S: Send + 'static,
{
Box::pin(future::err(err_unnamed_type(name)))
}
#[derive(Display)]
#[display("Expected GraphQL scalar, found: {_0}")]
pub struct NotScalarError<'a, S: ScalarValue>(pub &'a InputValue<S>);
pub trait ToResultCall {
type Input;
type Output;
type Error;
fn __to_result_call(&self, input: Self::Input) -> Result<Self::Output, Self::Error>;
}
impl<I, O, E> ToResultCall for &fn(I) -> Result<O, E> {
type Input = I;
type Output = O;
type Error = E;
fn __to_result_call(&self, input: Self::Input) -> Result<Self::Output, Self::Error> {
self(input)
}
}
impl<I, O> ToResultCall for fn(I) -> O {
type Input = I;
type Output = O;
type Error = Infallible;
fn __to_result_call(&self, input: Self::Input) -> Result<Self::Output, Self::Error> {
Ok(self(input))
}
}
pub trait ToScalarValueCall<S: ScalarValue> {
type Input;
fn __to_scalar_value_call(&self, input: Self::Input) -> S;
}
impl<I, S> ToScalarValueCall<S> for &&&fn(I) -> S
where
S: ScalarValue,
{
type Input = I;
fn __to_scalar_value_call(&self, input: Self::Input) -> S {
self(input)
}
}
impl<I, S> ToScalarValueCall<S> for &&fn(I) -> String
where
S: ScalarValue,
{
type Input = I;
fn __to_scalar_value_call(&self, input: Self::Input) -> S {
self(input).into()
}
}
impl<I, O, S> ToScalarValueCall<S> for &fn(I) -> O
where
S: ScalarValue,
O: ToScalarValue<S>,
{
type Input = I;
fn __to_scalar_value_call(&self, input: Self::Input) -> S {
self(input).to_scalar_value()
}
}
impl<I, O, S> ToScalarValueCall<S> for fn(I) -> O
where
S: ScalarValue,
O: Display,
{
type Input = I;
fn __to_scalar_value_call(&self, input: Self::Input) -> S {
S::from_displayable_non_static(&self(input))
}
}