File size: 3,105 Bytes
d8435ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use std::num::NonZeroU64;
use std::time::Duration;

use collection::operations::consistency_params::ReadConsistency;
use schemars::JsonSchema;
use serde::Deserialize;
use validator::Validate;

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Deserialize, JsonSchema, Validate)]
pub struct ReadParams {
    #[serde(default, deserialize_with = "deserialize_read_consistency")]
    #[validate(nested)]
    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>,
{
    #[derive(Deserialize)]
    #[serde(untagged)]
    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",
        )),
    }
}

#[cfg(test)]
mod test {
    use collection::operations::consistency_params::ReadConsistencyType;

    use super::*;

    #[test]
    fn deserialize_empty_string() {
        test_str("", ReadParams::default());
    }

    #[test]
    fn deserialize_empty_value() {
        test("", ReadParams::default());
    }

    #[test]
    fn deserialize_type() {
        test("all", from_type(ReadConsistencyType::All));
        test("majority", from_type(ReadConsistencyType::Majority));
        test("quorum", from_type(ReadConsistencyType::Quorum));
    }

    #[test]
    fn deserialize_factor() {
        for factor in 1..42 {
            test(&factor.to_string(), from_factor(factor));
        }
    }

    #[test]
    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()
        }
    }
}