async_graphql/types/external/list/
hashbrown_hash_set.rs

1use std::{borrow::Cow, collections::HashSet as StdHashSet, hash::Hash};
2
3use hashbrown::HashSet;
4
5use crate::{
6    ContextSelectionSet, InputType, InputValueError, InputValueResult, OutputType, Positioned,
7    Result, ServerResult, Value, parser::types::Field, registry, resolver_utils::resolve_list,
8};
9
10impl<T: InputType + Hash + Eq> InputType for HashSet<T> {
11    type RawValueType = Self;
12
13    fn type_name() -> Cow<'static, str> {
14        <StdHashSet<T> as InputType>::type_name()
15    }
16
17    fn qualified_type_name() -> String {
18        <StdHashSet<T> as InputType>::qualified_type_name()
19    }
20
21    fn create_type_info(registry: &mut registry::Registry) -> String {
22        <StdHashSet<T> as InputType>::create_type_info(registry)
23    }
24
25    fn parse(value: Option<Value>) -> InputValueResult<Self> {
26        match value.unwrap_or_default() {
27            Value::List(values) => values
28                .into_iter()
29                .map(|value| InputType::parse(Some(value)))
30                .collect::<Result<_, _>>()
31                .map_err(InputValueError::propagate),
32            value => Ok({
33                let mut result = Self::default();
34                result.insert(InputType::parse(Some(value)).map_err(InputValueError::propagate)?);
35                result
36            }),
37        }
38    }
39
40    fn to_value(&self) -> Value {
41        Value::List(self.iter().map(InputType::to_value).collect())
42    }
43
44    fn as_raw_value(&self) -> Option<&Self::RawValueType> {
45        Some(self)
46    }
47}
48
49#[cfg_attr(feature = "boxed-trait", async_trait::async_trait)]
50impl<T: OutputType + Hash + Eq> OutputType for HashSet<T> {
51    fn type_name() -> Cow<'static, str> {
52        <StdHashSet<T> as OutputType>::type_name()
53    }
54
55    fn qualified_type_name() -> String {
56        <StdHashSet<T> as OutputType>::qualified_type_name()
57    }
58
59    fn create_type_info(registry: &mut registry::Registry) -> String {
60        <StdHashSet<T> as OutputType>::create_type_info(registry)
61    }
62
63    async fn resolve(
64        &self,
65        ctx: &ContextSelectionSet<'_>,
66        field: &Positioned<Field>,
67    ) -> ServerResult<Value> {
68        resolve_list(ctx, field, self, Some(self.len())).await
69    }
70}