support for primitive type in CursorType
use std::{
+ char::ParseCharError,
convert::Infallible,
fmt::Display,
num::{ParseFloatError, ParseIntError},
+ str::ParseBoolError,
};
use serde::{de::DeserializeOwned, Serialize};
@@ -23,7 +25,9 @@ pub trait CursorType: Sized {
fn encode_cursor(&self) -> String;
}
-impl CursorType for usize {
+macro_rules! cursor_type_int_impl {
+ ($($t:ty)*) => {$(
+ impl CursorType for $t {
type Error = ParseIntError;
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
@@ -34,9 +38,13 @@ impl CursorType for usize {
self.to_string()
}
}
+ )*}
+}
-impl CursorType for i32 {
- type Error = ParseIntError;
+cursor_type_int_impl! { isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128 }
+
+impl CursorType for f32 {
+ type Error = ParseFloatError;
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
s.parse()
@@ -47,8 +55,8 @@ impl CursorType for i32 {
}
}
-impl CursorType for i64 {
- type Error = ParseIntError;
+impl CursorType for f64 {
+ type Error = ParseFloatError;
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
s.parse()
@@ -59,23 +67,23 @@ impl CursorType for i64 {
}
}
-impl CursorType for String {
- type Error = Infallible;
+impl CursorType for char {
+ type Error = ParseCharError;
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
- Ok(s.to_string())
+ s.parse()
}
fn encode_cursor(&self) -> String {
- self.clone()
+ self.to_string()
}
}
-impl CursorType for ID {
- type Error = Infallible;
+impl CursorType for bool {
+ type Error = ParseBoolError;
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
- Ok(s.to_string().into())
+ s.parse()
}
fn encode_cursor(&self) -> String {
@@ -83,23 +91,23 @@ impl CursorType for ID {
}
}
-impl CursorType for f64 {
- type Error = ParseFloatError;
+impl CursorType for String {
+ type Error = Infallible;
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
- s.parse()
+ Ok(s.to_string())
}
fn encode_cursor(&self) -> String {
- self.to_string()
+ self.clone()
}
}
-impl CursorType for f32 {
- type Error = ParseFloatError;
+impl CursorType for ID {
+ type Error = Infallible;
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
- s.parse()
+ Ok(s.to_string().into())
}
fn encode_cursor(&self) -> String {
