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
use crate::ID;
use byteorder::{ReadBytesExt, BE};
use std::convert::Infallible;
use std::fmt::Display;
pub trait CursorType: Sized {
type Error: Display;
fn decode_cursor(s: &str) -> Result<Self, Self::Error>;
fn encode_cursor(&self) -> Result<String, Self::Error>;
}
impl CursorType for usize {
type Error = anyhow::Error;
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
let data = base64::decode(s)?;
Ok(data.as_slice().read_u32::<BE>()? as usize)
}
fn encode_cursor(&self) -> Result<String, Self::Error> {
Ok(base64::encode((*self as u32).to_be_bytes()))
}
}
impl CursorType for String {
type Error = Infallible;
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
Ok(s.to_string())
}
fn encode_cursor(&self) -> Result<String, Self::Error> {
Ok(self.clone())
}
}
impl CursorType for ID {
type Error = Infallible;
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
Ok(s.to_string().into())
}
fn encode_cursor(&self) -> Result<String, Self::Error> {
Ok(self.to_string())
}
}