Spaces:
Build error
Build error
use std::num::NonZeroU64; | |
use std::time::Duration; | |
use collection::operations::consistency_params::ReadConsistency; | |
use schemars::JsonSchema; | |
use serde::Deserialize; | |
use validator::Validate; | |
pub struct ReadParams { | |
pub consistency: Option<ReadConsistency>, | |
/// If set, overrides global timeout for this request. Unit is seconds. | |
pub timeout: Option<NonZeroU64>, | |
} | |
impl ReadParams { | |
pub fn timeout(&self) -> Option<Duration> { | |
self.timeout.map(|num| Duration::from_secs(num.get())) | |
} | |
pub(crate) fn timeout_as_secs(&self) -> Option<usize> { | |
self.timeout.map(|i| i.get() as usize) | |
} | |
} | |
fn deserialize_read_consistency<'de, D>( | |
deserializer: D, | |
) -> Result<Option<ReadConsistency>, D::Error> | |
where | |
D: serde::Deserializer<'de>, | |
{ | |
enum Helper<'a> { | |
ReadConsistency(ReadConsistency), | |
Str(&'a str), | |
} | |
match Helper::deserialize(deserializer)? { | |
Helper::ReadConsistency(read_consistency) => Ok(Some(read_consistency)), | |
Helper::Str("") => Ok(None), | |
_ => Err(serde::de::Error::custom( | |
"failed to deserialize read consistency query parameter value", | |
)), | |
} | |
} | |
mod test { | |
use collection::operations::consistency_params::ReadConsistencyType; | |
use super::*; | |
fn deserialize_empty_string() { | |
test_str("", ReadParams::default()); | |
} | |
fn deserialize_empty_value() { | |
test("", ReadParams::default()); | |
} | |
fn deserialize_type() { | |
test("all", from_type(ReadConsistencyType::All)); | |
test("majority", from_type(ReadConsistencyType::Majority)); | |
test("quorum", from_type(ReadConsistencyType::Quorum)); | |
} | |
fn deserialize_factor() { | |
for factor in 1..42 { | |
test(&factor.to_string(), from_factor(factor)); | |
} | |
} | |
fn try_deserialize_factor_0() { | |
assert!(try_deserialize(&str("0")).is_err()); | |
} | |
fn test(value: &str, params: ReadParams) { | |
test_str(&str(value), params); | |
} | |
fn test_str(str: &str, params: ReadParams) { | |
assert_eq!(deserialize(str), params); | |
} | |
fn deserialize(str: &str) -> ReadParams { | |
try_deserialize(str).unwrap() | |
} | |
fn try_deserialize(str: &str) -> Result<ReadParams, serde_urlencoded::de::Error> { | |
serde_urlencoded::from_str(str) | |
} | |
fn str(value: &str) -> String { | |
format!("consistency={value}") | |
} | |
fn from_type(r#type: ReadConsistencyType) -> ReadParams { | |
ReadParams { | |
consistency: Some(ReadConsistency::Type(r#type)), | |
..Default::default() | |
} | |
} | |
fn from_factor(factor: usize) -> ReadParams { | |
ReadParams { | |
consistency: Some(ReadConsistency::Factor(factor)), | |
..Default::default() | |
} | |
} | |
} | |