From af8d4d0f4b33f992b21c0b98f5d114bb594bf87d Mon Sep 17 00:00:00 2001 From: sunli Date: Mon, 2 Mar 2020 00:59:04 +0800 Subject: [PATCH] add ID scalar --- README.md | 1 + src/datetime.rs | 1 - src/id.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 ++ src/scalar.rs | 1 - src/type.rs | 1 - src/uuid.rs | 1 - 7 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 src/id.rs diff --git a/README.md b/README.md index 2cf0292b..fc6715bb 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ - [X] Float - [X] String - [X] Bool + - [X] ID - [X] DateTime - [X] UUID - Complex Types diff --git a/src/datetime.rs b/src/datetime.rs index 02530a08..61aa3553 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -1,5 +1,4 @@ use crate::{QueryError, Result, Scalar, Value}; -use anyhow::Error; use chrono::{DateTime, TimeZone, Utc}; impl Scalar for DateTime { diff --git a/src/id.rs b/src/id.rs new file mode 100644 index 00000000..c82dbf2f --- /dev/null +++ b/src/id.rs @@ -0,0 +1,57 @@ +use crate::{QueryError, Result, Scalar, Value}; +use std::ops::{Deref, DerefMut}; + +#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)] +pub struct ID(String); + +impl Deref for ID { + type Target = String; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for ID { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +impl Scalar for ID { + fn type_name() -> &'static str { + "ID" + } + + fn parse(value: Value) -> Result { + match value { + Value::Int(n) => Ok(ID(n.as_i64().unwrap().to_string())), + Value::String(s) => Ok(ID(s)), + _ => { + return Err(QueryError::ExpectedType { + expect: Self::type_name().to_string(), + actual: value, + } + .into()) + } + } + } + + fn parse_from_json(value: serde_json::Value) -> Result { + match value { + serde_json::Value::Number(n) if n.is_i64() => Ok(ID(n.as_i64().unwrap().to_string())), + serde_json::Value::String(s) => Ok(ID(s)), + _ => { + return Err(QueryError::ExpectedJsonType { + expect: Self::type_name().to_string(), + actual: value, + } + .into()) + } + } + } + + fn into_json(self) -> Result { + Ok(self.0.clone().into()) + } +} diff --git a/src/lib.rs b/src/lib.rs index bf011b00..37cd2ac5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,6 +36,7 @@ extern crate thiserror; mod context; mod r#enum; mod error; +mod id; mod query; mod scalar; mod r#type; @@ -54,6 +55,7 @@ pub use async_graphql_derive::{Enum, InputObject, Object}; pub use context::{Context, ContextField, ContextSelectionSet, Data, Variables}; pub use error::{ErrorWithPosition, PositionError, QueryError, QueryParseError}; pub use graphql_parser::query::Value; +pub use id::ID; pub use query::QueryBuilder; pub use scalar::Scalar; diff --git a/src/scalar.rs b/src/scalar.rs index 15c2d13d..d4f9b5b3 100644 --- a/src/scalar.rs +++ b/src/scalar.rs @@ -1,6 +1,5 @@ use crate::r#type::{GQLInputValue, GQLOutputValue, GQLType}; use crate::{ContextSelectionSet, QueryError, Result}; -use anyhow::Error; use graphql_parser::query::Value; pub trait Scalar: Sized + Send { diff --git a/src/type.rs b/src/type.rs index 2405739c..cdd361a2 100644 --- a/src/type.rs +++ b/src/type.rs @@ -1,5 +1,4 @@ use crate::{ContextSelectionSet, ErrorWithPosition, QueryError, Result}; -use anyhow::Error; use graphql_parser::query::Value; #[doc(hidden)] diff --git a/src/uuid.rs b/src/uuid.rs index af7d2775..9e252d50 100644 --- a/src/uuid.rs +++ b/src/uuid.rs @@ -1,5 +1,4 @@ use crate::{QueryError, Result, Scalar, Value}; -use anyhow::Error; use uuid::Uuid; impl Scalar for Uuid {