From e6acc8ca63553dd27455b26335d6398e6991af42 Mon Sep 17 00:00:00 2001 From: Bakyt Niiazaliev Date: Sun, 15 Jun 2025 00:19:51 +0700 Subject: [PATCH] feat: add PartialEq implementation for QueryParams --- src/urls/query_params.rs | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/urls/query_params.rs b/src/urls/query_params.rs index 82f92bd4..37c66df3 100644 --- a/src/urls/query_params.rs +++ b/src/urls/query_params.rs @@ -24,9 +24,7 @@ fn primitive_value_to_str(value: &Bound<'_, PyAny>) -> PyResult { fn urlencode(s: &str) -> String { s.bytes() .map(|b| match b { - b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => { - (b as char).to_string() - } + b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => (b as char).to_string(), b' ' => "+".to_string(), _ => format!("%{:02X}", b), }) @@ -34,7 +32,7 @@ fn urlencode(s: &str) -> String { } #[pyclass(str)] -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Eq)] pub struct QueryParams { params: IndexMap>, } @@ -96,12 +94,7 @@ impl QueryParams { } #[pyo3(signature = (key, default=None))] - pub fn get( - &self, - py: Python<'_>, - key: String, - default: Option>, - ) -> PyResult>> { + pub fn get(&self, py: Python<'_>, key: String, default: Option>) -> PyResult>> { match self.params.get(&key) { Some(values) => match values.first() { Some(value) => { @@ -185,9 +178,7 @@ impl QueryParams { } pub fn __iter__(&self) -> QueryParamsKeysIterator { - QueryParamsKeysIterator { - params: self.keys(), - } + QueryParamsKeysIterator { params: self.keys() } } pub fn __len__(&self) -> usize { @@ -219,7 +210,9 @@ impl QueryParams { #[allow(unused_variables)] #[pyo3(signature = (params = None))] pub fn update(&self, params: Option<&Bound<'_, PyAny>>) -> PyResult { - Err(PyRuntimeError::new_err("QueryParams are immutable since 0.18.0. Use `q = q.merge(...)` to create an updated copy.")) + Err(PyRuntimeError::new_err( + "QueryParams are immutable since 0.18.0. Use `q = q.merge(...)` to create an updated copy.", + )) } #[allow(unused_variables)] @@ -285,7 +278,7 @@ impl QueryParams { Ok(QueryParams { params }) } - fn from_pyany(obj: &Bound<'_, PyAny>) -> PyResult { + pub fn from_pyany(obj: &Bound<'_, PyAny>) -> PyResult { if obj.is_none() { Ok(QueryParams { params: IndexMap::new(), @@ -313,6 +306,16 @@ impl QueryParams { } } +impl PartialEq for QueryParams { + fn eq(&self, other: &Self) -> bool { + let mut this = self.multi_items().clone(); + let mut other = other.multi_items().clone(); + this.sort(); + other.sort(); + this == other + } +} + impl Display for QueryParams { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let multi_items = self.multi_items();