1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use crate::{InputValueError, InputValueResult, Result, ScalarType, Value};
use async_graphql_derive::Scalar;
use std::ops::{Deref, DerefMut};
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub struct Cursor(String);
impl Deref for Cursor {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Cursor {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T> From<T> for Cursor
where
T: std::fmt::Display,
{
fn from(value: T) -> Self {
Cursor(value.to_string())
}
}
#[Scalar(internal)]
impl ScalarType for Cursor {
fn type_name() -> &'static str {
"Cursor"
}
fn parse(value: &Value) -> InputValueResult<Self> {
match value {
Value::String(s) => Ok(Cursor(s.into())),
_ => Err(InputValueError::ExpectedType),
}
}
fn is_valid(value: &Value) -> bool {
match value {
Value::String(_) => true,
_ => false,
}
}
fn to_json(&self) -> Result<serde_json::Value> {
Ok(self.0.to_string().into())
}
}