async_graphql/types/external/
char.rs

1use crate::{InputValueError, InputValueResult, Scalar, ScalarType, Value};
2
3/// The `Char` scalar type represents a unicode char.
4/// The input and output values are a string, and there can only be one unicode
5/// character in this string.
6#[Scalar(internal)]
7impl ScalarType for char {
8    fn parse(value: Value) -> InputValueResult<Self> {
9        match value {
10            Value::String(s) => {
11                let mut chars = s.chars();
12                match chars.next() {
13                    Some(ch) if chars.next().is_none() => Ok(ch),
14                    Some(_) => Err(InputValueError::custom(
15                        "There can only be one unicode character in the string.",
16                    )),
17                    None => Err(InputValueError::custom("A unicode character is required.")),
18                }
19            }
20            _ => Err(InputValueError::expected_type(value)),
21        }
22    }
23
24    fn is_valid(value: &Value) -> bool {
25        matches!(value, Value::String(_))
26    }
27
28    fn to_value(&self) -> Value {
29        Value::String((*self).into())
30    }
31}