use std::{ convert::Infallible, fmt::Display, num::{ParseFloatError, ParseIntError}, }; use crate::ID; /// Cursor type /// /// A custom scalar that serializes as a string. /// pub trait CursorType: Sized { /// Error type for `decode_cursor`. type Error: Display; /// Decode cursor from string. fn decode_cursor(s: &str) -> Result; /// Encode cursor to string. fn encode_cursor(&self) -> String; } impl CursorType for usize { type Error = ParseIntError; fn decode_cursor(s: &str) -> Result { s.parse() } fn encode_cursor(&self) -> String { self.to_string() } } impl CursorType for i32 { type Error = ParseIntError; fn decode_cursor(s: &str) -> Result { s.parse() } fn encode_cursor(&self) -> String { self.to_string() } } impl CursorType for i64 { type Error = ParseIntError; fn decode_cursor(s: &str) -> Result { s.parse() } fn encode_cursor(&self) -> String { self.to_string() } } impl CursorType for String { type Error = Infallible; fn decode_cursor(s: &str) -> Result { Ok(s.to_string()) } fn encode_cursor(&self) -> String { self.clone() } } impl CursorType for ID { type Error = Infallible; fn decode_cursor(s: &str) -> Result { Ok(s.to_string().into()) } fn encode_cursor(&self) -> String { self.to_string() } } impl CursorType for f64 { type Error = ParseFloatError; fn decode_cursor(s: &str) -> Result { s.parse() } fn encode_cursor(&self) -> String { self.to_string() } } impl CursorType for f32 { type Error = ParseFloatError; fn decode_cursor(s: &str) -> Result { s.parse() } fn encode_cursor(&self) -> String { self.to_string() } }