Add OpaqueCursor #1040
This commit is contained in:
parent
d26df5c402
commit
fca337d8a0
|
@ -53,6 +53,7 @@ serde_json = "1.0.64"
|
||||||
static_assertions = "1.1.0"
|
static_assertions = "1.1.0"
|
||||||
tempfile = "3.2.0"
|
tempfile = "3.2.0"
|
||||||
thiserror = "1.0.24"
|
thiserror = "1.0.24"
|
||||||
|
base64 = "0.13.0"
|
||||||
|
|
||||||
# Feature optional dependencies
|
# Feature optional dependencies
|
||||||
bson = { version = "2.0.0", optional = true, features = ["chrono-0_4"] }
|
bson = { version = "2.0.0", optional = true, features = ["chrono-0_4"] }
|
||||||
|
|
|
@ -4,6 +4,8 @@ use std::{
|
||||||
num::{ParseFloatError, ParseIntError},
|
num::{ParseFloatError, ParseIntError},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
|
|
||||||
use crate::ID;
|
use crate::ID;
|
||||||
|
|
||||||
/// Cursor type
|
/// Cursor type
|
||||||
|
@ -104,3 +106,23 @@ impl CursorType for f32 {
|
||||||
self.to_string()
|
self.to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A opaque cursor that encode/decode the value to base64
|
||||||
|
pub struct OpaqueCursor<T>(T);
|
||||||
|
|
||||||
|
impl<T> CursorType for OpaqueCursor<T>
|
||||||
|
where
|
||||||
|
T: Serialize + DeserializeOwned,
|
||||||
|
{
|
||||||
|
type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||||
|
|
||||||
|
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
|
||||||
|
let data = base64::decode_config(s, base64::URL_SAFE_NO_PAD)?;
|
||||||
|
Ok(Self(serde_json::from_slice(&data)?))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn encode_cursor(&self) -> String {
|
||||||
|
let value = serde_json::to_vec(&self.0).unwrap_or_default();
|
||||||
|
base64::encode_config(value, base64::URL_SAFE_NO_PAD)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ mod page_info;
|
||||||
use std::{fmt::Display, future::Future};
|
use std::{fmt::Display, future::Future};
|
||||||
|
|
||||||
pub use connection_type::Connection;
|
pub use connection_type::Connection;
|
||||||
pub use cursor::CursorType;
|
pub use cursor::{CursorType, OpaqueCursor};
|
||||||
pub use edge::Edge;
|
pub use edge::Edge;
|
||||||
pub use page_info::PageInfo;
|
pub use page_info::PageInfo;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user