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