async_graphql/types/external/
string.rs1use std::borrow::Cow;
2
3use crate::{
4 parser::types::Field, registry, registry::Registry, ContextSelectionSet, InputType,
5 InputValueError, InputValueResult, OutputType, Positioned, Scalar, ScalarType, ServerResult,
6 Value,
7};
8
9#[Scalar(internal)]
13impl ScalarType for String {
14 fn parse(value: Value) -> InputValueResult<Self> {
15 match value {
16 Value::String(s) => Ok(s),
17 _ => Err(InputValueError::expected_type(value)),
18 }
19 }
20
21 fn is_valid(value: &Value) -> bool {
22 matches!(value, Value::String(_))
23 }
24
25 fn to_value(&self) -> Value {
26 Value::String(self.clone())
27 }
28}
29
30macro_rules! impl_input_string_for_smart_ptr {
31 ($ty:ty) => {
32 impl InputType for $ty {
33 type RawValueType = Self;
34
35 fn type_name() -> Cow<'static, str> {
36 Cow::Borrowed("String")
37 }
38
39 fn create_type_info(registry: &mut Registry) -> String {
40 <String as OutputType>::create_type_info(registry)
41 }
42
43 fn parse(value: Option<Value>) -> InputValueResult<Self> {
44 let value = value.unwrap_or_default();
45 match value {
46 Value::String(s) => Ok(s.into()),
47 _ => Err(InputValueError::expected_type(value)),
48 }
49 }
50
51 fn to_value(&self) -> Value {
52 Value::String(self.to_string())
53 }
54
55 fn as_raw_value(&self) -> Option<&Self::RawValueType> {
56 Some(self)
57 }
58 }
59 };
60}
61
62impl_input_string_for_smart_ptr!(Box<str>);
63impl_input_string_for_smart_ptr!(std::sync::Arc<str>);
64
65#[cfg_attr(feature = "boxed-trait", async_trait::async_trait)]
66impl OutputType for str {
67 fn type_name() -> Cow<'static, str> {
68 Cow::Borrowed("String")
69 }
70
71 fn create_type_info(registry: &mut registry::Registry) -> String {
72 <String as OutputType>::create_type_info(registry)
73 }
74
75 async fn resolve(
76 &self,
77 _: &ContextSelectionSet<'_>,
78 _field: &Positioned<Field>,
79 ) -> ServerResult<Value> {
80 Ok(Value::String(self.to_string()))
81 }
82}