mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-06-16 01:39:44 -07:00
bitview: reorg part 9
This commit is contained in:
@@ -1,26 +1,27 @@
|
||||
use serde::{Deserialize, Deserializer};
|
||||
use sonic_rs::{JsonValueTrait, Value};
|
||||
|
||||
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)?;
|
||||
let value: Option<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)
|
||||
if value.is_none() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let value = value.unwrap();
|
||||
|
||||
if let Some(mut s) = value.as_str().map(|s| s.to_string()) {
|
||||
if s.starts_with('"') && s.ends_with('"') && s.len() >= 2 {
|
||||
s = s[1..s.len() - 1].to_string();
|
||||
}
|
||||
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")),
|
||||
s.parse::<i64>().map(Some).map_err(serde::de::Error::custom)
|
||||
} else if let Some(n) = value.as_i64() {
|
||||
Ok(Some(n))
|
||||
} else {
|
||||
Err(serde::de::Error::custom("expected a string or number"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,28 +29,24 @@ pub fn de_unquote_usize<'de, D>(deserializer: D) -> Result<Option<usize>, D::Err
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let value: Option<serde_json::Value> = Option::deserialize(deserializer)?;
|
||||
let value: Option<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"))
|
||||
if value.is_none() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let value = value.unwrap();
|
||||
|
||||
if let Some(mut s) = value.as_str().map(|s| s.to_string()) {
|
||||
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)
|
||||
} else if let Some(n) = value.as_u64() {
|
||||
Ok(Some(n as usize))
|
||||
} else {
|
||||
Err(serde::de::Error::custom("expected a string or number"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ use std::fmt;
|
||||
use derive_deref::Deref;
|
||||
use schemars::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
use sonic_rs::{JsonContainerTrait, JsonValueTrait, Value};
|
||||
|
||||
#[derive(Debug, Deref, JsonSchema)]
|
||||
pub struct MaybeIds(Vec<String>);
|
||||
@@ -27,26 +28,26 @@ impl<'de> Deserialize<'de> for MaybeIds {
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
match serde_json::Value::deserialize(deserializer)? {
|
||||
serde_json::Value::String(str) => {
|
||||
if str.len() <= MAX_STRING_SIZE {
|
||||
Ok(MaybeIds(sanitize_ids(
|
||||
str.split(",").map(|s| s.to_string()),
|
||||
)))
|
||||
} else {
|
||||
Err(serde::de::Error::custom("Given parameter is too long"))
|
||||
}
|
||||
let value = Value::deserialize(deserializer)?;
|
||||
|
||||
if let Some(str) = value.as_str() {
|
||||
if str.len() <= MAX_STRING_SIZE {
|
||||
Ok(MaybeIds(sanitize_ids(
|
||||
str.split(",").map(|s| s.to_string()),
|
||||
)))
|
||||
} else {
|
||||
Err(serde::de::Error::custom("Given parameter is too long"))
|
||||
}
|
||||
serde_json::Value::Array(vec) => {
|
||||
if vec.len() <= MAX_VECS {
|
||||
Ok(MaybeIds(sanitize_ids(
|
||||
vec.into_iter().map(|s| s.as_str().unwrap().to_string()),
|
||||
)))
|
||||
} else {
|
||||
Err(serde::de::Error::custom("Given parameter is too long"))
|
||||
}
|
||||
} else if let Some(vec) = value.as_array() {
|
||||
if vec.len() <= MAX_VECS {
|
||||
Ok(MaybeIds(sanitize_ids(
|
||||
vec.into_iter().map(|s| s.as_str().unwrap().to_string()),
|
||||
)))
|
||||
} else {
|
||||
Err(serde::de::Error::custom("Given parameter is too long"))
|
||||
}
|
||||
_ => Err(serde::de::Error::custom("Bad ids format")),
|
||||
} else {
|
||||
Err(serde::de::Error::custom("Bad ids format"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ pub use format::Format;
|
||||
pub use index::Index;
|
||||
pub use output::{Output, Value};
|
||||
pub use pagination::{PaginatedIndexParam, PaginationParam};
|
||||
pub use params::{IdParam, Params, ParamsOpt};
|
||||
pub use params::{Params, ParamsOpt};
|
||||
use vecs::Vecs;
|
||||
|
||||
use crate::vecs::{IndexToVec, MetricToVec};
|
||||
@@ -222,16 +222,16 @@ impl<'a> Interface<'a> {
|
||||
&self.vecs.index_to_metric_to_vec
|
||||
}
|
||||
|
||||
pub fn get_metric_count(&self) -> usize {
|
||||
pub fn distinct_metric_count(&self) -> usize {
|
||||
self.vecs.metric_count
|
||||
}
|
||||
|
||||
pub fn get_index_count(&self) -> usize {
|
||||
self.vecs.index_count
|
||||
pub fn total_metric_count(&self) -> usize {
|
||||
self.vecs.vec_count
|
||||
}
|
||||
|
||||
pub fn get_vec_count(&self) -> usize {
|
||||
self.vecs.vec_count
|
||||
pub fn index_count(&self) -> usize {
|
||||
self.vecs.index_count
|
||||
}
|
||||
|
||||
pub fn get_indexes(&self) -> &[&'static str] {
|
||||
@@ -250,8 +250,8 @@ impl<'a> Interface<'a> {
|
||||
self.vecs.index_to_ids(paginated_index)
|
||||
}
|
||||
|
||||
pub fn get_vecid_to_indexes(&self, id: String) -> Option<&Vec<&'static str>> {
|
||||
self.vecs.id_to_indexes(id)
|
||||
pub fn metric_to_indexes(&self, metric: String) -> Option<&Vec<&'static str>> {
|
||||
self.vecs.metric_to_indexes(metric)
|
||||
}
|
||||
|
||||
pub fn parser(&self) -> &Parser {
|
||||
|
||||
@@ -106,8 +106,3 @@ impl ParamsOpt {
|
||||
self.format
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, JsonSchema)]
|
||||
pub struct IdParam {
|
||||
pub id: String,
|
||||
}
|
||||
|
||||
@@ -131,8 +131,9 @@ impl<'a> Vecs<'a> {
|
||||
&self.metrics[start..end]
|
||||
}
|
||||
|
||||
pub fn id_to_indexes(&self, id: String) -> Option<&Vec<&'static str>> {
|
||||
self.metric_to_indexes.get(id.as_str())
|
||||
pub fn metric_to_indexes(&self, metric: String) -> Option<&Vec<&'static str>> {
|
||||
self.metric_to_indexes
|
||||
.get(metric.replace("-", "_").as_str())
|
||||
}
|
||||
|
||||
pub fn index_to_ids(
|
||||
|
||||
Reference in New Issue
Block a user