mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-25 15:19:58 -07:00
global: snapshot
This commit is contained in:
55
crates/brk_interface/src/deser.rs
Normal file
55
crates/brk_interface/src/deser.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
use serde::{Deserialize, Deserializer};
|
||||
|
||||
pub fn de_unquote_i64<'de, D>(deserializer: D) -> Result<Option<i64>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let value: Option<serde_json::Value> = Option::deserialize(deserializer)?;
|
||||
|
||||
match value {
|
||||
None => Ok(None),
|
||||
Some(serde_json::Value::String(mut s)) => {
|
||||
if s.starts_with('"') && s.ends_with('"') && s.len() >= 2 {
|
||||
s = s[1..s.len() - 1].to_string();
|
||||
}
|
||||
s.parse::<i64>().map(Some).map_err(serde::de::Error::custom)
|
||||
}
|
||||
Some(serde_json::Value::Number(n)) => {
|
||||
// If it's a number, convert it to i64
|
||||
n.as_i64()
|
||||
.ok_or_else(|| serde::de::Error::custom("number out of range"))
|
||||
.map(Some)
|
||||
}
|
||||
_ => Err(serde::de::Error::custom("expected a string or number")),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn de_unquote_usize<'de, D>(deserializer: D) -> Result<Option<usize>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let value: Option<serde_json::Value> = Option::deserialize(deserializer)?;
|
||||
|
||||
match value {
|
||||
None => Ok(None),
|
||||
Some(serde_json::Value::String(mut s)) => {
|
||||
if s.starts_with('"') && s.ends_with('"') && s.len() >= 2 {
|
||||
s = s[1..s.len() - 1].to_string();
|
||||
}
|
||||
s.parse::<usize>()
|
||||
.map(Some)
|
||||
.map_err(serde::de::Error::custom)
|
||||
}
|
||||
Some(serde_json::Value::Number(n)) => {
|
||||
// If it's a number, convert it to usize
|
||||
n.as_u64()
|
||||
.ok_or_else(|| serde::de::Error::custom("number out of range"))
|
||||
.map(|v| v as usize)
|
||||
.map(Some)
|
||||
}
|
||||
_ => {
|
||||
dbg!(value);
|
||||
Err(serde::de::Error::custom("expected a string or number"))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ use brk_indexer::Indexer;
|
||||
use brk_vec::AnyCollectableVec;
|
||||
use tabled::settings::Style;
|
||||
|
||||
mod deser;
|
||||
mod format;
|
||||
mod index;
|
||||
mod maybe_ids;
|
||||
|
||||
@@ -1,25 +1,24 @@
|
||||
use schemars::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::Index;
|
||||
use crate::{Index, deser::de_unquote_usize};
|
||||
|
||||
#[derive(Debug, Default, Deserialize, JsonSchema)]
|
||||
pub struct PaginationParam {
|
||||
#[serde(alias = "p")]
|
||||
#[schemars(description = "Pagination index")]
|
||||
#[serde(default)]
|
||||
pub page: usize,
|
||||
#[serde(default, alias = "p", deserialize_with = "de_unquote_usize")]
|
||||
pub page: Option<usize>,
|
||||
}
|
||||
|
||||
impl PaginationParam {
|
||||
const PER_PAGE: usize = 1_000;
|
||||
|
||||
pub fn start(&self, len: usize) -> usize {
|
||||
(self.page * Self::PER_PAGE).clamp(0, len)
|
||||
(self.page.unwrap_or_default() * Self::PER_PAGE).clamp(0, len)
|
||||
}
|
||||
|
||||
pub fn end(&self, len: usize) -> usize {
|
||||
((self.page + 1) * Self::PER_PAGE).clamp(0, len)
|
||||
((self.page.unwrap_or_default() + 1) * Self::PER_PAGE).clamp(0, len)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
use std::ops::Deref;
|
||||
|
||||
use brk_rmcp::schemars::{self, JsonSchema};
|
||||
use serde::{Deserialize, Deserializer};
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::{Format, Index, maybe_ids::MaybeIds};
|
||||
use crate::{
|
||||
Format, Index,
|
||||
deser::{de_unquote_i64, de_unquote_usize},
|
||||
maybe_ids::MaybeIds,
|
||||
};
|
||||
|
||||
#[derive(Debug, Deserialize, JsonSchema)]
|
||||
pub struct Params {
|
||||
@@ -106,57 +110,3 @@ impl ParamsOpt {
|
||||
pub struct IdParam {
|
||||
pub id: String,
|
||||
}
|
||||
|
||||
fn de_unquote_i64<'de, D>(deserializer: D) -> Result<Option<i64>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let value: Option<serde_json::Value> = Option::deserialize(deserializer)?;
|
||||
|
||||
match value {
|
||||
None => Ok(None),
|
||||
Some(serde_json::Value::String(mut s)) => {
|
||||
if s.starts_with('"') && s.ends_with('"') && s.len() >= 2 {
|
||||
s = s[1..s.len() - 1].to_string();
|
||||
}
|
||||
s.parse::<i64>().map(Some).map_err(serde::de::Error::custom)
|
||||
}
|
||||
Some(serde_json::Value::Number(n)) => {
|
||||
// If it's a number, convert it to i64
|
||||
n.as_i64()
|
||||
.ok_or_else(|| serde::de::Error::custom("number out of range"))
|
||||
.map(Some)
|
||||
}
|
||||
_ => Err(serde::de::Error::custom("expected a string or number")),
|
||||
}
|
||||
}
|
||||
|
||||
fn de_unquote_usize<'de, D>(deserializer: D) -> Result<Option<usize>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let value: Option<serde_json::Value> = Option::deserialize(deserializer)?;
|
||||
|
||||
match value {
|
||||
None => Ok(None),
|
||||
Some(serde_json::Value::String(mut s)) => {
|
||||
if s.starts_with('"') && s.ends_with('"') && s.len() >= 2 {
|
||||
s = s[1..s.len() - 1].to_string();
|
||||
}
|
||||
s.parse::<usize>()
|
||||
.map(Some)
|
||||
.map_err(serde::de::Error::custom)
|
||||
}
|
||||
Some(serde_json::Value::Number(n)) => {
|
||||
// If it's a number, convert it to usize
|
||||
n.as_u64()
|
||||
.ok_or_else(|| serde::de::Error::custom("number out of range"))
|
||||
.map(|v| v as usize)
|
||||
.map(Some)
|
||||
}
|
||||
_ => {
|
||||
dbg!(value);
|
||||
Err(serde::de::Error::custom("expected a string or number"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user