Pave way to parse ?Sized types from InputValue

This commit is contained in:
tyranron 2022-05-17 16:59:42 +02:00
parent e381602d5a
commit 5a3bd6c8a9
No known key found for this signature in database
GPG key ID: 762E144FB230A4F0
7 changed files with 175 additions and 39 deletions

View file

@ -6,6 +6,12 @@ use crate::{
Selection,
};
#[doc(inline)]
pub use crate::types::{
arc::TryFromInputValue as InputValueAsArc, r#box::TryFromInputValue as InputValueAsBox,
r#ref::TryFromInputValue as InputValueAsRef, rc::TryFromInputValue as InputValueAsRc,
};
pub trait Type<Info: ?Sized, S = DefaultScalarValue> {
fn meta<'r>(registry: &mut Registry<'r, S>, info: &Info) -> MetaType<'r, S>
where
@ -95,26 +101,3 @@ pub trait InputValue<'input, S: 'input = DefaultScalarValue>: Sized {
pub trait InputValueOwned<S = DefaultScalarValue>: for<'i> InputValue<'i, S> {}
impl<T, S> InputValueOwned<S> for T where T: for<'i> InputValue<'i, S> {}
pub trait InputValueAsRef<S = DefaultScalarValue> {
type Error: IntoFieldError<S>;
fn try_from_input_value(v: &graphql::InputValue<S>) -> Result<&Self, Self::Error>;
fn try_from_implicit_null<'a>() -> Result<&'a Self, Self::Error>
where
S: 'a,
{
Self::try_from_input_value(&graphql::InputValue::<S>::Null)
}
}
/*
impl<T, S> InputValueAsRef<S> for T where T: InputValueOwned<S> {
type Error = <T as InputValueOwned<S>>::Error;
fn try_from_input_value(v: &graphql::InputValue<S>) -> Result<&Self, Self::Error> {
<T as InputValueOwned<S>>::try_from_input_value(v).as_ref()
}
}
*/

View file

@ -6,7 +6,8 @@ use crate::{
graphql,
meta::MetaType,
parser::{ParseError, ScalarToken},
resolve, Arguments, BoxFuture, ExecutionResult, Executor, Registry, Selection,
resolve, Arguments, BoxFuture, DefaultScalarValue, ExecutionResult, Executor, IntoFieldError,
Registry, Selection,
};
impl<T, Info, S> resolve::Type<Info, S> for Arc<T>
@ -151,6 +152,48 @@ where
}
}
impl<'inp, T, S> resolve::InputValue<'inp, S> for Arc<T>
where
T: resolve::InputValueAsArc<'inp, S> + ?Sized,
S: 'inp,
{
type Error = <T as resolve::InputValueAsArc<'inp, S>>::Error;
fn try_from_input_value(v: &'inp graphql::InputValue<S>) -> Result<Self, Self::Error> {
<T as resolve::InputValueAsArc<'inp, S>>::try_from_input_value(v)
}
fn try_from_implicit_null() -> Result<Self, Self::Error> {
<T as resolve::InputValueAsArc<'inp, S>>::try_from_implicit_null()
}
}
pub trait TryFromInputValue<'input, S: 'input = DefaultScalarValue> {
type Error: IntoFieldError<S>;
fn try_from_input_value(v: &'input graphql::InputValue<S>) -> Result<Arc<Self>, Self::Error>;
fn try_from_implicit_null() -> Result<Arc<Self>, Self::Error> {
Self::try_from_input_value(&graphql::InputValue::<S>::Null)
}
}
impl<'inp, T, S> TryFromInputValue<'inp, S> for T
where
T: resolve::InputValue<'inp, S>,
S: 'inp,
{
type Error = <T as resolve::InputValue<'inp, S>>::Error;
fn try_from_input_value(v: &'inp graphql::InputValue<S>) -> Result<Arc<Self>, Self::Error> {
<T as resolve::InputValue<'inp, S>>::try_from_input_value(v).map(Arc::new)
}
fn try_from_implicit_null() -> Result<Arc<Self>, Self::Error> {
<T as resolve::InputValue<'inp, S>>::try_from_implicit_null().map(Arc::new)
}
}
impl<T, S> graphql::InputType<S> for Arc<T>
where
T: graphql::InputType<S> + ?Sized,

View file

@ -4,7 +4,8 @@ use crate::{
graphql,
meta::MetaType,
parser::{ParseError, ScalarToken},
resolve, Arguments, BoxFuture, ExecutionResult, Executor, Registry, Selection,
resolve, Arguments, BoxFuture, DefaultScalarValue, ExecutionResult, Executor, IntoFieldError,
Registry, Selection,
};
impl<T, Info, S> resolve::Type<Info, S> for Box<T>
@ -149,19 +150,45 @@ where
}
}
// TODO: how to parse unsized?
impl<'inp, T, S: 'inp> resolve::InputValue<'inp, S> for Box<T>
impl<'inp, T, S> resolve::InputValue<'inp, S> for Box<T>
where
T: resolve::InputValue<'inp, S>,
T: resolve::InputValueAsBox<'inp, S> + ?Sized,
S: 'inp,
{
type Error = <T as resolve::InputValue<'inp, S>>::Error;
type Error = <T as resolve::InputValueAsBox<'inp, S>>::Error;
fn try_from_input_value(v: &'inp graphql::InputValue<S>) -> Result<Self, Self::Error> {
<T as resolve::InputValue<'inp, S>>::try_from_input_value(v).map(Self::new)
<T as resolve::InputValueAsBox<'inp, S>>::try_from_input_value(v)
}
fn try_from_implicit_null() -> Result<Self, Self::Error> {
<T as resolve::InputValue<'inp, S>>::try_from_implicit_null().map(Self::new)
<T as resolve::InputValueAsBox<'inp, S>>::try_from_implicit_null()
}
}
pub trait TryFromInputValue<'input, S: 'input = DefaultScalarValue> {
type Error: IntoFieldError<S>;
fn try_from_input_value(v: &'input graphql::InputValue<S>) -> Result<Box<Self>, Self::Error>;
fn try_from_implicit_null() -> Result<Box<Self>, Self::Error> {
Self::try_from_input_value(&graphql::InputValue::<S>::Null)
}
}
impl<'inp, T, S> TryFromInputValue<'inp, S> for T
where
T: resolve::InputValue<'inp, S>,
S: 'inp,
{
type Error = <T as resolve::InputValue<'inp, S>>::Error;
fn try_from_input_value(v: &'inp graphql::InputValue<S>) -> Result<Box<Self>, Self::Error> {
<T as resolve::InputValue<'inp, S>>::try_from_input_value(v).map(Box::new)
}
fn try_from_implicit_null() -> Result<Box<Self>, Self::Error> {
<T as resolve::InputValue<'inp, S>>::try_from_implicit_null().map(Box::new)
}
}

View file

@ -1,11 +1,11 @@
mod arc;
pub mod arc;
mod array;
mod r#box;
pub mod r#box;
pub mod iter;
mod nullable;
mod option;
mod rc;
mod r#ref;
pub mod rc;
pub mod r#ref;
mod ref_mut;
mod slice;
mod r#str;

View file

@ -6,7 +6,8 @@ use crate::{
graphql,
meta::MetaType,
parser::{ParseError, ScalarToken},
resolve, Arguments, BoxFuture, ExecutionResult, Executor, Registry, Selection,
resolve, Arguments, BoxFuture, DefaultScalarValue, ExecutionResult, Executor, IntoFieldError,
Registry, Selection,
};
impl<T, Info, S> resolve::Type<Info, S> for Rc<T>
@ -151,6 +152,48 @@ where
}
}
impl<'inp, T, S> resolve::InputValue<'inp, S> for Rc<T>
where
T: resolve::InputValueAsRc<'inp, S> + ?Sized,
S: 'inp,
{
type Error = <T as resolve::InputValueAsRc<'inp, S>>::Error;
fn try_from_input_value(v: &'inp graphql::InputValue<S>) -> Result<Self, Self::Error> {
<T as resolve::InputValueAsRc<'inp, S>>::try_from_input_value(v)
}
fn try_from_implicit_null() -> Result<Self, Self::Error> {
<T as resolve::InputValueAsRc<'inp, S>>::try_from_implicit_null()
}
}
pub trait TryFromInputValue<'input, S: 'input = DefaultScalarValue> {
type Error: IntoFieldError<S>;
fn try_from_input_value(v: &'input graphql::InputValue<S>) -> Result<Rc<Self>, Self::Error>;
fn try_from_implicit_null() -> Result<Rc<Self>, Self::Error> {
Self::try_from_input_value(&graphql::InputValue::<S>::Null)
}
}
impl<'inp, T, S> TryFromInputValue<'inp, S> for T
where
T: resolve::InputValue<'inp, S>,
S: 'inp,
{
type Error = <T as resolve::InputValue<'inp, S>>::Error;
fn try_from_input_value(v: &'inp graphql::InputValue<S>) -> Result<Rc<Self>, Self::Error> {
<T as resolve::InputValue<'inp, S>>::try_from_input_value(v).map(Rc::new)
}
fn try_from_implicit_null() -> Result<Rc<Self>, Self::Error> {
<T as resolve::InputValue<'inp, S>>::try_from_implicit_null().map(Rc::new)
}
}
impl<T, S> graphql::InputType<S> for Rc<T>
where
T: graphql::InputType<S> + ?Sized,

View file

@ -6,7 +6,8 @@ use crate::{
graphql,
meta::MetaType,
parser::{ParseError, ScalarToken},
resolve, Arguments, BoxFuture, ExecutionResult, Executor, Registry, Selection,
resolve, Arguments, BoxFuture, DefaultScalarValue, ExecutionResult, Executor, IntoFieldError,
Registry, Selection,
};
impl<'me, T, Info, S> resolve::Type<Info, S> for &'me T
@ -166,6 +167,19 @@ where
}
}
pub trait TryFromInputValue<S = DefaultScalarValue> {
type Error: IntoFieldError<S>;
fn try_from_input_value(v: &graphql::InputValue<S>) -> Result<&Self, Self::Error>;
fn try_from_implicit_null<'a>() -> Result<&'a Self, Self::Error>
where
S: 'a,
{
Self::try_from_input_value(&graphql::InputValue::<S>::Null)
}
}
impl<'me, T, S> graphql::InputType<S> for &'me T
where
T: graphql::InputType<S> + ?Sized,

View file

@ -2,6 +2,8 @@
//!
//! [`str`]: primitive@std::str
use std::{sync::Arc, rc::Rc};
use futures::future;
use crate::{
@ -65,14 +67,14 @@ where
}
}
impl<'me, S: ScalarValue> resolve::ScalarToken<S> for str {
impl<S: ScalarValue> resolve::ScalarToken<S> for str {
fn parse_scalar_token(token: ScalarToken<'_>) -> Result<S, ParseError<'_>> {
// TODO: Replace with `resolve::ScalarToken<S>`
<String as crate::ParseScalarValue<S>>::from_str(token)
}
}
impl<'me, S: ScalarValue> resolve::InputValueAsRef<S> for str {
impl<S: ScalarValue> resolve::InputValueAsRef<S> for str {
type Error = String;
fn try_from_input_value(v: &graphql::InputValue<S>) -> Result<&Self, Self::Error> {
@ -81,6 +83,30 @@ impl<'me, S: ScalarValue> resolve::InputValueAsRef<S> for str {
}
}
impl<'inp, S: ScalarValue> resolve::InputValueAsBox<'inp, S> for str {
type Error = String;
fn try_from_input_value(v: &'inp graphql::InputValue<S>) -> Result<Box<Self>, Self::Error> {
<str as resolve::InputValueAsRef<S>>::try_from_input_value(v).map(Into::into)
}
}
impl<'inp, S: ScalarValue> resolve::InputValueAsArc<'inp, S> for str {
type Error = String;
fn try_from_input_value(v: &'inp graphql::InputValue<S>) -> Result<Arc<Self>, Self::Error> {
<str as resolve::InputValueAsRef<S>>::try_from_input_value(v).map(Into::into)
}
}
impl<'inp, S: ScalarValue> resolve::InputValueAsRc<'inp, S> for str {
type Error = String;
fn try_from_input_value(v: &'inp graphql::InputValue<S>) -> Result<Rc<Self>, Self::Error> {
<str as resolve::InputValueAsRef<S>>::try_from_input_value(v).map(Into::into)
}
}
impl<S> graphql::InputType<S> for str {
fn assert_input_type() {}
}