2020-05-28 00:02:00 +00:00
|
|
|
use std::convert::Infallible;
|
|
|
|
use std::fmt::Display;
|
2020-09-10 01:09:55 +00:00
|
|
|
use std::num::ParseIntError;
|
2020-05-05 05:21:45 +00:00
|
|
|
|
2020-10-15 06:38:10 +00:00
|
|
|
use crate::ID;
|
|
|
|
|
2020-05-28 00:02:00 +00:00
|
|
|
/// Cursor type
|
2020-05-05 05:21:45 +00:00
|
|
|
///
|
|
|
|
/// A custom scalar that serializes as a string.
|
2021-03-22 02:39:17 +00:00
|
|
|
/// <https://relay.dev/graphql/connections.htm#sec-Cursor>
|
2020-05-28 00:02:00 +00:00
|
|
|
pub trait CursorType: Sized {
|
2020-09-09 16:34:23 +00:00
|
|
|
/// Error type for `decode_cursor`.
|
2020-05-28 00:02:00 +00:00
|
|
|
type Error: Display;
|
2020-05-05 05:21:45 +00:00
|
|
|
|
2020-05-28 00:02:00 +00:00
|
|
|
/// Decode cursor from string.
|
|
|
|
fn decode_cursor(s: &str) -> Result<Self, Self::Error>;
|
2020-05-05 05:21:45 +00:00
|
|
|
|
2020-05-28 00:02:00 +00:00
|
|
|
/// Encode cursor to string.
|
2020-09-09 16:34:23 +00:00
|
|
|
fn encode_cursor(&self) -> String;
|
2020-05-05 05:21:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 00:02:00 +00:00
|
|
|
impl CursorType for usize {
|
2020-09-09 16:34:23 +00:00
|
|
|
type Error = ParseIntError;
|
2020-05-28 00:02:00 +00:00
|
|
|
|
|
|
|
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
|
2020-09-09 16:34:23 +00:00
|
|
|
s.parse()
|
2020-05-05 05:21:45 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 16:34:23 +00:00
|
|
|
fn encode_cursor(&self) -> String {
|
|
|
|
self.to_string()
|
2020-05-10 10:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:11:08 +00:00
|
|
|
impl CursorType for i32 {
|
|
|
|
type Error = ParseIntError;
|
|
|
|
|
|
|
|
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
|
|
|
|
s.parse()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn encode_cursor(&self) -> String {
|
|
|
|
self.to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CursorType for i64 {
|
|
|
|
type Error = ParseIntError;
|
|
|
|
|
|
|
|
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
|
|
|
|
s.parse()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn encode_cursor(&self) -> String {
|
|
|
|
self.to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 00:02:00 +00:00
|
|
|
impl CursorType for String {
|
|
|
|
type Error = Infallible;
|
|
|
|
|
|
|
|
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
|
|
|
|
Ok(s.to_string())
|
2020-05-10 14:25:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 16:34:23 +00:00
|
|
|
fn encode_cursor(&self) -> String {
|
|
|
|
self.clone()
|
2020-05-10 10:27:46 +00:00
|
|
|
}
|
2020-05-28 00:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CursorType for ID {
|
|
|
|
type Error = Infallible;
|
2020-05-10 10:27:46 +00:00
|
|
|
|
2020-05-28 00:02:00 +00:00
|
|
|
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
|
|
|
|
Ok(s.to_string().into())
|
2020-05-05 05:21:45 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 16:34:23 +00:00
|
|
|
fn encode_cursor(&self) -> String {
|
|
|
|
self.to_string()
|
2020-05-05 05:21:45 +00:00
|
|
|
}
|
|
|
|
}
|