url2/query_unique.rs
1use std::collections::HashMap;
2
3use crate::*;
4
5/// Gives access to the query string restricting the view to unique keys.
6///
7/// Dereferences to `HashMap<String, String>` for easy key/value manipulation.
8///
9/// # Example
10///
11/// ```rust
12/// use url2::prelude::*;
13///
14/// let mut url = Url2::parse("https://test.com/?a=1&b=2");
15///
16/// url.query_unique().remove("b");
17///
18/// assert_eq!("https://test.com/?a=1", url.as_str());
19/// ```
20pub struct Url2QueryUnique<'lt> {
21 pub(crate) url_ref: &'lt mut Url2,
22}
23
24impl<'lt> Url2QueryUnique<'lt> {
25 /// builder-style helper for chaining
26 ///
27 /// # Example
28 ///
29 /// ```rust
30 /// use url2::prelude::*;
31 ///
32 /// let mut url = Url2::default();
33 /// url.query_unique()
34 /// .set_pair("a", "1")
35 /// .set_pair("b", "2");
36 ///
37 /// assert!(
38 /// "none:?a=1&b=2" == url.as_str() ||
39 /// "none:?b=2&a=1" == url.as_str()
40 /// );
41 /// ```
42 pub fn set_pair(self, name: &str, value: &str) -> Self {
43 (self.url_ref.0)
44 .1
45 .as_mut()
46 .unwrap()
47 .insert(name.to_string(), value.to_string());
48 self
49 }
50}
51
52impl<'lt> Drop for Url2QueryUnique<'lt> {
53 fn drop(&mut self) {
54 self.url_ref.priv_sync_query_unique_cache();
55 }
56}
57
58impl<'lt> std::ops::Deref for Url2QueryUnique<'lt> {
59 type Target = HashMap<String, String>;
60
61 fn deref(&self) -> &Self::Target {
62 (self.url_ref.0).1.as_ref().unwrap()
63 }
64}
65
66impl<'lt> std::ops::DerefMut for Url2QueryUnique<'lt> {
67 fn deref_mut(&mut self) -> &mut Self::Target {
68 (self.url_ref.0).1.as_mut().unwrap()
69 }
70}