2020-03-05 06:23:55 +00:00
|
|
|
use crate::{Result, GQLScalar, Value};
|
2020-03-01 16:59:04 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 06:23:55 +00:00
|
|
|
impl GQLScalar for ID {
|
2020-03-01 16:59:04 +00:00
|
|
|
fn type_name() -> &'static str {
|
|
|
|
"ID"
|
|
|
|
}
|
|
|
|
|
2020-03-04 02:38:07 +00:00
|
|
|
fn parse(value: &Value) -> Option<Self> {
|
2020-03-01 16:59:04 +00:00
|
|
|
match value {
|
2020-03-03 11:15:18 +00:00
|
|
|
Value::Int(n) => Some(ID(n.as_i64().unwrap().to_string())),
|
2020-03-04 02:38:07 +00:00
|
|
|
Value::String(s) => Some(ID(s.clone())),
|
2020-03-03 11:15:18 +00:00
|
|
|
_ => None,
|
2020-03-01 16:59:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 00:24:49 +00:00
|
|
|
fn to_json(&self) -> Result<serde_json::Value> {
|
2020-03-01 16:59:04 +00:00
|
|
|
Ok(self.0.clone().into())
|
|
|
|
}
|
|
|
|
}
|