2020-09-18 00:52:13 +00:00
|
|
|
use crate::{InputValueError, InputValueResult, Scalar, ScalarType, Value};
|
2020-05-16 16:08:03 +00:00
|
|
|
#[cfg(feature = "bson")]
|
2020-05-07 06:42:34 +00:00
|
|
|
use bson::oid::{self, ObjectId};
|
2020-07-08 05:58:46 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-05-10 14:13:41 +00:00
|
|
|
use std::convert::TryFrom;
|
2020-05-07 03:05:27 +00:00
|
|
|
use std::num::ParseIntError;
|
2020-03-01 16:59:04 +00:00
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
|
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-07-08 05:58:46 +00:00
|
|
|
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Serialize, Deserialize)]
|
2020-09-15 18:32:13 +00:00
|
|
|
#[serde(transparent)]
|
2020-09-13 09:38:19 +00:00
|
|
|
pub struct ID(pub String);
|
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-09-13 09:38:19 +00:00
|
|
|
impl<T: std::fmt::Display> From<T> for ID {
|
2020-05-10 13:49:29 +00:00
|
|
|
fn from(value: T) -> Self {
|
|
|
|
ID(value.to_string())
|
2020-03-17 09:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-13 09:38:19 +00:00
|
|
|
impl From<ID> for String {
|
|
|
|
fn from(id: ID) -> Self {
|
|
|
|
id.0
|
2020-05-07 03:05:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 04:33:57 +00:00
|
|
|
macro_rules! try_from_integers {
|
|
|
|
($($ty:ty),*) => {
|
|
|
|
$(
|
|
|
|
impl TryFrom<ID> for $ty {
|
|
|
|
type Error = ParseIntError;
|
|
|
|
|
|
|
|
fn try_from(id: ID) -> std::result::Result<Self, Self::Error> {
|
|
|
|
id.0.parse()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
};
|
2020-05-07 03:05:27 +00:00
|
|
|
}
|
|
|
|
|
2020-09-13 09:38:19 +00:00
|
|
|
try_from_integers!(i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, isize, usize);
|
2020-09-06 04:33:57 +00:00
|
|
|
|
2020-09-25 07:40:04 +00:00
|
|
|
#[cfg(feature = "uuid")]
|
2020-05-16 16:08:03 +00:00
|
|
|
impl TryFrom<ID> for uuid::Uuid {
|
2020-05-07 03:05:27 +00:00
|
|
|
type Error = uuid::Error;
|
|
|
|
|
2020-05-10 14:13:41 +00:00
|
|
|
fn try_from(id: ID) -> std::result::Result<Self, Self::Error> {
|
2020-05-16 16:08:03 +00:00
|
|
|
uuid::Uuid::parse_str(&id.0)
|
2020-05-07 03:05:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-16 16:08:03 +00:00
|
|
|
#[cfg(feature = "bson")]
|
2020-05-10 14:13:41 +00:00
|
|
|
impl TryFrom<ID> for ObjectId {
|
2020-05-07 06:42:34 +00:00
|
|
|
type Error = oid::Error;
|
|
|
|
|
2020-05-10 14:13:41 +00:00
|
|
|
fn try_from(id: ID) -> std::result::Result<Self, oid::Error> {
|
|
|
|
ObjectId::with_string(&id.0)
|
2020-05-07 06:42:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-09-18 00:52:13 +00:00
|
|
|
#[Scalar(internal)]
|
2020-04-27 09:58:10 +00:00
|
|
|
impl ScalarType for ID {
|
2020-05-11 13:47:24 +00:00
|
|
|
fn parse(value: Value) -> InputValueResult<Self> {
|
2020-03-01 16:59:04 +00:00
|
|
|
match value {
|
2020-07-28 14:13:07 +00:00
|
|
|
Value::Number(n) if n.is_i64() => Ok(ID(n.to_string())),
|
2020-05-16 13:14:26 +00:00
|
|
|
Value::String(s) => Ok(ID(s)),
|
2020-05-11 13:47:24 +00:00
|
|
|
_ => Err(InputValueError::ExpectedType(value)),
|
2020-05-10 10:27:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_valid(value: &Value) -> bool {
|
|
|
|
match value {
|
2020-07-28 14:13:07 +00:00
|
|
|
Value::Number(n) if n.is_i64() => true,
|
|
|
|
Value::String(_) => true,
|
2020-05-10 10:27:46 +00:00
|
|
|
_ => false,
|
2020-03-01 16:59:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 10:34:43 +00:00
|
|
|
fn to_value(&self) -> Value {
|
|
|
|
Value::String(self.0.clone())
|
2020-03-01 16:59:04 +00:00
|
|
|
}
|
|
|
|
}
|