use core::fmt::Display;
use async_graphql::{InputType, InputValueError, Name, Value, indexmap::IndexMap};
pub fn read<W, D, F>(
object: &IndexMap<Name, Value>,
name: &str,
) -> Result<Option<D>, InputValueError<F>>
where
W: InputType + Into<D>,
F: InputType,
{
<Option<W> as InputType>::parse(object.get(name).cloned())
.map(|slot| slot.map(Into::into))
.map_err(InputValueError::propagate)
}
pub fn read_list<W, D, F>(
object: &IndexMap<Name, Value>,
name: &str,
) -> Result<Option<Box<[D]>>, InputValueError<F>>
where
W: InputType + Into<D>,
F: InputType,
{
<Option<Box<[W]>> as InputType>::parse(object.get(name).cloned())
.map(|slot| {
slot.map(|list| {
Vec::from(list)
.into_iter()
.map(Into::into)
.collect::<Box<[D]>>()
})
})
.map_err(InputValueError::propagate)
}
pub fn write<W, D>(slot: &Option<D>) -> Value
where
W: InputType + From<D>,
D: Clone,
{
InputType::to_value(&slot.clone().map(W::from))
}
pub fn write_list<W, D>(slot: &Option<Box<[D]>>) -> Value
where
W: InputType + From<D>,
D: Clone,
{
let wire = slot
.as_ref()
.map(|list| list.iter().cloned().map(W::from).collect::<Box<[W]>>());
InputType::to_value(&wire)
}
pub(crate) fn crossed<C, D, E, F>(
object: &IndexMap<Name, Value>,
name: &str,
cross: fn(C) -> Result<D, E>,
) -> Result<Option<D>, InputValueError<F>>
where
C: InputType,
E: Display,
F: InputType,
{
<Option<C> as InputType>::parse(object.get(name).cloned())
.map_err(InputValueError::propagate)?
.map(cross)
.transpose()
.map_err(InputValueError::custom)
}
pub(crate) fn crossed_list<C, D, E, F>(
object: &IndexMap<Name, Value>,
name: &str,
cross: fn(C) -> Result<D, E>,
) -> Result<Option<Box<[D]>>, InputValueError<F>>
where
C: InputType,
E: Display,
F: InputType,
{
let carried = <Option<Box<[C]>> as InputType>::parse(object.get(name).cloned())
.map_err(InputValueError::propagate)?;
match carried {
Some(list) => Vec::from(list)
.into_iter()
.map(cross)
.collect::<Result<Box<[D]>, E>>()
.map(Some)
.map_err(InputValueError::custom),
None => Ok(None),
}
}
pub(crate) fn carried<C, D>(slot: &Option<D>, carry: fn(&D) -> C) -> Value
where
C: InputType,
{
InputType::to_value(&slot.as_ref().map(carry))
}
pub(crate) fn carried_list<C, D>(slot: &Option<Box<[D]>>, carry: fn(&D) -> C) -> Value
where
C: InputType,
{
let carried = slot
.as_ref()
.map(|list| list.iter().map(carry).collect::<Box<[C]>>());
InputType::to_value(&carried)
}
pub fn cross<D, F>(
object: &IndexMap<Name, Value>,
name: &str,
) -> Result<Option<D>, InputValueError<F>>
where
D: crate::ToGraphqlInput,
D::Input: InputType,
F: InputType,
{
<Option<D::Input> as InputType>::parse(object.get(name).cloned())
.map_err(InputValueError::propagate)?
.map(D::cross)
.transpose()
.map_err(InputValueError::custom)
}
pub fn cross_list<D, F>(
object: &IndexMap<Name, Value>,
name: &str,
) -> Result<Option<Box<[D]>>, InputValueError<F>>
where
D: crate::ToGraphqlInput,
D::Input: InputType,
F: InputType,
{
let carried = <Option<Box<[D::Input]>> as InputType>::parse(object.get(name).cloned())
.map_err(InputValueError::propagate)?;
match carried {
Some(list) => Vec::from(list)
.into_iter()
.map(D::cross)
.collect::<Result<Box<[D]>, D::Error>>()
.map(Some)
.map_err(InputValueError::custom),
None => Ok(None),
}
}
pub fn carry<D>(slot: &Option<D>) -> Value
where
D: crate::ToGraphqlInput,
D::Input: InputType,
{
InputType::to_value(&slot.as_ref().map(D::carry))
}
pub fn carry_list<D>(slot: &Option<Box<[D]>>) -> Value
where
D: crate::ToGraphqlInput,
D::Input: InputType,
{
let carried = slot
.as_ref()
.map(|list| list.iter().map(D::carry).collect::<Box<[D::Input]>>());
InputType::to_value(&carried)
}
pub fn nested<V, F>(object: &IndexMap<Name, Value>, name: &str) -> Result<V, InputValueError<F>>
where
V: InputType,
F: InputType,
{
V::parse(object.get(name).cloned()).map_err(InputValueError::propagate)
}