async_graphql/types/external/
uuid.rs

1use uuid::Uuid;
2
3use crate::{InputValueError, InputValueResult, Scalar, ScalarType, Value};
4
5#[Scalar(
6    internal,
7    name = "UUID",
8    specified_by_url = "http://tools.ietf.org/html/rfc4122"
9)]
10/// A UUID is a unique 128-bit number, stored as 16 octets. UUIDs are parsed as
11/// Strings within GraphQL. UUIDs are used to assign unique identifiers to
12/// entities without requiring a central allocating authority.
13///
14/// # References
15///
16/// * [Wikipedia: Universally Unique Identifier](http://en.wikipedia.org/wiki/Universally_unique_identifier)
17/// * [RFC4122: A Universally Unique IDentifier (UUID) URN Namespace](http://tools.ietf.org/html/rfc4122)
18impl ScalarType for Uuid {
19    fn parse(value: Value) -> InputValueResult<Self> {
20        match value {
21            Value::String(s) => Ok(Uuid::parse_str(&s)?),
22            _ => Err(InputValueError::expected_type(value)),
23        }
24    }
25
26    fn to_value(&self) -> Value {
27        Value::String(self.to_string())
28    }
29}