2020-05-10 10:27:46 +00:00
|
|
|
use crate::{InputValueError, InputValueResult, Result, ScalarType, Value};
|
2020-04-27 09:58:10 +00:00
|
|
|
use async_graphql_derive::Scalar;
|
2020-05-07 06:42:34 +00:00
|
|
|
use bson::oid::{self, ObjectId};
|
2020-05-07 03:05:27 +00:00
|
|
|
use std::convert::TryInto;
|
|
|
|
use std::num::ParseIntError;
|
2020-03-01 16:59:04 +00:00
|
|
|
use std::ops::{Deref, DerefMut};
|
2020-05-07 03:05:27 +00:00
|
|
|
use uuid::Uuid;
|
2020-03-01 16:59:04 +00:00
|
|
|
|
2020-03-09 10:05:52 +00:00
|
|
|
/// ID scalar
|
|
|
|
///
|
2020-05-07 03:05:27 +00:00
|
|
|
/// The input is a `&str`, `String`, `usize` or `uuid::UUID`, and the output is a string.
|
2020-03-01 16:59:04 +00:00
|
|
|
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
|
|
|
|
pub struct ID(String);
|
|
|
|
|
2020-03-20 03:56:08 +00:00
|
|
|
impl std::fmt::Display for ID {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-01 16:59:04 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-17 09:26:59 +00:00
|
|
|
impl From<String> for ID {
|
|
|
|
fn from(value: String) -> Self {
|
|
|
|
ID(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 03:05:27 +00:00
|
|
|
impl Into<String> for ID {
|
|
|
|
fn into(self) -> String {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-17 09:26:59 +00:00
|
|
|
impl<'a> From<&'a str> for ID {
|
|
|
|
fn from(value: &'a str) -> Self {
|
|
|
|
ID(value.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<usize> for ID {
|
|
|
|
fn from(value: usize) -> Self {
|
|
|
|
ID(value.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 03:05:27 +00:00
|
|
|
impl TryInto<usize> for ID {
|
|
|
|
type Error = ParseIntError;
|
|
|
|
|
|
|
|
fn try_into(self) -> std::result::Result<usize, Self::Error> {
|
|
|
|
self.0.parse()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Uuid> for ID {
|
|
|
|
fn from(uuid: Uuid) -> ID {
|
|
|
|
ID(uuid.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryInto<Uuid> for ID {
|
|
|
|
type Error = uuid::Error;
|
|
|
|
|
|
|
|
fn try_into(self) -> std::result::Result<Uuid, Self::Error> {
|
|
|
|
Uuid::parse_str(&self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 06:42:34 +00:00
|
|
|
impl From<ObjectId> for ID {
|
|
|
|
fn from(object_id: ObjectId) -> ID {
|
|
|
|
ID(object_id.to_hex())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryInto<ObjectId> for ID {
|
|
|
|
type Error = oid::Error;
|
|
|
|
|
|
|
|
fn try_into(self) -> std::result::Result<ObjectId, oid::Error> {
|
|
|
|
ObjectId::with_string(&self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 02:20:43 +00:00
|
|
|
impl PartialEq<&str> for ID {
|
|
|
|
fn eq(&self, other: &&str) -> bool {
|
|
|
|
self.0.as_str() == *other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 10:38:20 +00:00
|
|
|
/// Convert any type that implements Display to the ID type
|
|
|
|
pub trait ToGraphQLID {
|
|
|
|
#[allow(missing_docs)]
|
|
|
|
fn to_graphql_id(&self) -> ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> ToGraphQLID for T
|
|
|
|
where
|
|
|
|
T: std::fmt::Display,
|
|
|
|
{
|
|
|
|
fn to_graphql_id(&self) -> ID {
|
|
|
|
ID(self.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 09:58:10 +00:00
|
|
|
#[Scalar(internal)]
|
|
|
|
impl ScalarType for ID {
|
2020-03-01 16:59:04 +00:00
|
|
|
fn type_name() -> &'static str {
|
|
|
|
"ID"
|
|
|
|
}
|
|
|
|
|
2020-05-10 10:27:46 +00:00
|
|
|
fn parse(value: &Value) -> InputValueResult<Self> {
|
2020-03-01 16:59:04 +00:00
|
|
|
match value {
|
2020-05-10 10:27:46 +00:00
|
|
|
Value::Int(n) => Ok(ID(n.to_string())),
|
|
|
|
Value::String(s) => Ok(ID(s.clone())),
|
|
|
|
_ => Err(InputValueError::ExpectedType),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_valid(value: &Value) -> bool {
|
|
|
|
match value {
|
|
|
|
Value::Int(_) | Value::String(_) => true,
|
|
|
|
_ => false,
|
2020-03-01 16:59:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 03:39:28 +00:00
|
|
|
fn to_json(&self) -> Result<serde_json::Value> {
|
|
|
|
Ok(self.0.clone().into())
|
2020-03-01 16:59:04 +00:00
|
|
|
}
|
|
|
|
}
|