2023-11-19 03:51:45 +00:00
|
|
|
// trifid-api-old, an open source reimplementation of the Defined Networking nebula management server.
|
2023-04-04 13:56:05 +00:00
|
|
|
// Copyright (C) 2023 c0repwn3r
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2023-04-03 22:39:49 +00:00
|
|
|
use base64::Engine;
|
2023-05-11 00:32:19 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::error::Error;
|
2023-04-03 22:39:49 +00:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
|
|
pub struct Cursor {
|
2023-05-11 00:32:19 +00:00
|
|
|
pub page: u64,
|
2023-04-03 22:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Cursor> for String {
|
|
|
|
type Error = Box<dyn Error>;
|
|
|
|
|
|
|
|
fn try_from(value: Cursor) -> Result<Self, Self::Error> {
|
|
|
|
// 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<String> for Cursor {
|
|
|
|
type Error = Box<dyn Error>;
|
|
|
|
|
|
|
|
fn try_from(value: String) -> Result<Self, Self::Error> {
|
|
|
|
if value.is_empty() {
|
|
|
|
// If empty, it's page 0
|
2023-05-11 00:32:19 +00:00
|
|
|
return Ok(Cursor { page: 0 });
|
2023-04-03 22:39:49 +00:00
|
|
|
}
|
|
|
|
// 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)
|
|
|
|
}
|
2023-05-11 00:32:19 +00:00
|
|
|
}
|