From 2fbeda70ae8c8d77dca729ab5a2020277953a013 Mon Sep 17 00:00:00 2001 From: jinser Date: Mon, 27 Feb 2023 16:31:24 +0800 Subject: [PATCH] fix: duplicated params --- Cargo.lock | 2 +- src/params.rs | 33 ++++++++++++++++++++------------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0bd4d22..b3b2437 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -77,7 +77,7 @@ dependencies = [ [[package]] name = "firebase-rs" -version = "2.0.5" +version = "2.0.6" dependencies = [ "itertools", "reqwest", diff --git a/src/params.rs b/src/params.rs index 3ea76c7..6ceb835 100644 --- a/src/params.rs +++ b/src/params.rs @@ -2,9 +2,9 @@ use crate::constants::{ END_AT, EQUAL_TO, EXPORT, FORMAT, LIMIT_TO_FIRST, LIMIT_TO_LAST, ORDER_BY, SHALLOW, START_AT, }; use crate::Firebase; +use itertools::Itertools; use std::collections::HashMap; use url::Url; -use itertools::Itertools; #[derive(Debug)] pub struct Params { @@ -27,11 +27,10 @@ impl Params { } pub fn add_param(&mut self, key: &str, value: T) -> &mut Self - where - T: ToString, + where + T: ToString, { self.params.insert(key.to_string(), value.to_string()); - self.set_params(); self } @@ -68,26 +67,34 @@ impl Params { self.add_param(FORMAT, EXPORT) } - pub fn finish(&self) -> Firebase { + pub fn finish(&mut self) -> Firebase { + self.set_params(); Firebase::new(self.uri.as_str()).unwrap() } } - #[cfg(test)] mod tests { + use crate::params::Params; use std::collections::HashMap; use url::Url; - use crate::params::Params; #[test] fn check_params() { - let mut params: HashMap = HashMap::new(); - params.insert("param_1".to_owned(), "value_1".to_owned()); - params.insert("param_2".to_owned(), "value_2".to_owned()); - let mut param = Params { uri: Url::parse("https://github.com/emreyalvac").unwrap(), params }; + let mut params = HashMap::new(); + params.insert("param_1", "value_1"); + params.insert("param_2", "value_2"); + + let mut param = Params::new(Url::parse("https://github.com/emreyalvac").unwrap()); + + for (k, v) in params { + param.add_param(&k, v); + } param.set_params(); - assert_eq!(param.uri.as_str(), "https://github.com/emreyalvac?param_1=value_1¶m_2=value_2") + assert_eq!( + param.uri.as_str(), + "https://github.com/emreyalvac?param_1=value_1¶m_2=value_2" + ) } -} \ No newline at end of file +}