use futures::Stream;
use crate::{FieldError, GraphQLValue, IntoFieldError, ScalarValue};
pub trait IntoFieldResult<T, S> {
type Item;
fn into_result(self) -> Result<T, FieldError<S>>;
}
impl<T, E, S> IntoFieldResult<T, S> for Result<T, E>
where
T: IntoFieldResult<T, S>,
E: IntoFieldError<S>,
{
type Item = T::Item;
fn into_result(self) -> Result<T, FieldError<S>> {
self.map_err(E::into_field_error)
}
}
impl<T, S> IntoFieldResult<T, S> for T
where
T: Stream,
{
type Item = T::Item;
fn into_result(self) -> Result<T, FieldError<S>> {
Ok(self)
}
}
pub struct StreamItem;
pub struct StreamResult;
pub struct ResultStreamItem;
pub struct ResultStreamResult;
pub trait ExtractTypeFromStream<T, S>
where
S: ScalarValue,
{
type Item: GraphQLValue<S>;
}
impl<T, I, S> ExtractTypeFromStream<StreamItem, S> for T
where
T: futures::Stream<Item = I>,
I: GraphQLValue<S>,
S: ScalarValue,
{
type Item = I;
}
impl<Ty, T, E, S> ExtractTypeFromStream<StreamResult, S> for Ty
where
Ty: futures::Stream<Item = Result<T, E>>,
T: GraphQLValue<S>,
S: ScalarValue,
{
type Item = T;
}
impl<T, I, E, S> ExtractTypeFromStream<ResultStreamItem, S> for Result<T, E>
where
T: futures::Stream<Item = I>,
I: GraphQLValue<S>,
S: ScalarValue,
{
type Item = I;
}
impl<T, E, I, ER, S> ExtractTypeFromStream<ResultStreamResult, S> for Result<T, E>
where
T: futures::Stream<Item = Result<I, ER>>,
I: GraphQLValue<S>,
S: ScalarValue,
{
type Item = I;
}