use std::error::Error; use base64::Engine; use serde::{Serialize, Deserialize}; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Cursor { pub page: u64 } impl TryFrom for String { type Error = Box; fn try_from(value: Cursor) -> Result { // Serialize it to json let json_str = serde_json::to_string(&value)?; // Then base64-encode the json let base64_str = base64::engine::general_purpose::STANDARD.encode(json_str); Ok(base64_str) } } impl TryFrom for Cursor { type Error = Box; fn try_from(value: String) -> Result { if value.is_empty() { // If empty, it's page 0 return Ok(Cursor { page: 0 }) } // Base64-decode the value let json_bytes = base64::engine::general_purpose::STANDARD.decode(value)?; // Convert it into a string let json_str = String::from_utf8(json_bytes)?; // Deserialize it from json let cursor = serde_json::from_str(&json_str)?; Ok(cursor) } }