|
| 1 | +use crate::ffi; |
| 2 | + |
| 3 | +pub struct URLSearchParams(*mut ffi::ada_url_search_params); |
| 4 | + |
| 5 | +impl Drop for URLSearchParams { |
| 6 | + fn drop(&mut self) { |
| 7 | + unsafe { ffi::ada_free_search_params(self.0) } |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +impl URLSearchParams { |
| 12 | + /// Parses an return a URLSearchParams struct. |
| 13 | + /// |
| 14 | + /// ``` |
| 15 | + /// use ada_url::URLSearchParams; |
| 16 | + /// let params = URLSearchParams::parse("a=1&b=2"); |
| 17 | + /// assert_eq!(params.get("a"), Some("1")); |
| 18 | + /// assert_eq!(params.get("b"), Some("2")); |
| 19 | + /// ``` |
| 20 | + pub fn parse(input: &str) -> Self { |
| 21 | + Self(unsafe { ffi::ada_parse_search_params(input.as_ptr().cast(), input.len()) }) |
| 22 | + } |
| 23 | + |
| 24 | + /// Returns the size of the URLSearchParams struct. |
| 25 | + /// |
| 26 | + /// ``` |
| 27 | + /// use ada_url::URLSearchParams; |
| 28 | + /// let params = URLSearchParams::parse("a=1&b=2"); |
| 29 | + /// assert_eq!(params.size(), 2); |
| 30 | + /// ``` |
| 31 | + pub fn size(&self) -> usize { |
| 32 | + unsafe { ffi::ada_search_params_size(self.0) } |
| 33 | + } |
| 34 | + |
| 35 | + /// Sorts the keys of the URLSearchParams struct. |
| 36 | + pub fn sort(&self) { |
| 37 | + unsafe { ffi::ada_search_params_sort(self.0) } |
| 38 | + } |
| 39 | + |
| 40 | + /// Appends a key/value to the URLSearchParams struct. |
| 41 | + pub fn append(&self, key: &str, value: &str) { |
| 42 | + unsafe { |
| 43 | + ffi::ada_search_params_append( |
| 44 | + self.0, |
| 45 | + key.as_ptr().cast(), |
| 46 | + key.len(), |
| 47 | + value.as_ptr().cast(), |
| 48 | + value.len(), |
| 49 | + ) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// Removes all keys pre-existing keys from the URLSearchParams struct |
| 54 | + /// and appends the new key/value. |
| 55 | + /// |
| 56 | + /// ``` |
| 57 | + /// use ada_url::URLSearchParams; |
| 58 | + /// let params = URLSearchParams::parse("a=1&b=2"); |
| 59 | + /// params.set("a", "3"); |
| 60 | + /// assert_eq!(params.get("a"), Some("3")); |
| 61 | + /// ``` |
| 62 | + pub fn set(&self, key: &str, value: &str) { |
| 63 | + unsafe { |
| 64 | + ffi::ada_search_params_set( |
| 65 | + self.0, |
| 66 | + key.as_ptr().cast(), |
| 67 | + key.len(), |
| 68 | + value.as_ptr().cast(), |
| 69 | + value.len(), |
| 70 | + ) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /// Removes a key/value from the URLSearchParams struct. |
| 75 | + /// Depending on the value parameter, it will either remove |
| 76 | + /// the key/value pair or just the key. |
| 77 | + /// |
| 78 | + /// ``` |
| 79 | + /// use ada_url::URLSearchParams; |
| 80 | + /// let params = URLSearchParams::parse("a=1&b=2"); |
| 81 | + /// params.remove("a", Some("1")); |
| 82 | + /// assert_eq!(params.get("a"), None); |
| 83 | + /// ``` |
| 84 | + pub fn remove(&self, key: &str, value: Option<&str>) { |
| 85 | + if let Some(value) = value { |
| 86 | + unsafe { |
| 87 | + ffi::ada_search_params_remove_value( |
| 88 | + self.0, |
| 89 | + key.as_ptr().cast(), |
| 90 | + key.len(), |
| 91 | + value.as_ptr().cast(), |
| 92 | + value.len(), |
| 93 | + ) |
| 94 | + } |
| 95 | + } else { |
| 96 | + unsafe { ffi::ada_search_params_remove(self.0, key.as_ptr().cast(), key.len()) } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /// Retruns true if the URLSearchParams struct contains the key. |
| 101 | + /// |
| 102 | + /// ``` |
| 103 | + /// use ada_url::URLSearchParams; |
| 104 | + /// let params = URLSearchParams::parse("a=1&b=2"); |
| 105 | + /// assert_eq!(params.has("a", None), true); |
| 106 | + /// ``` |
| 107 | + pub fn has(&self, key: &str, value: Option<&str>) -> bool { |
| 108 | + if let Some(value) = value { |
| 109 | + unsafe { |
| 110 | + ffi::ada_search_params_has_value( |
| 111 | + self.0, |
| 112 | + key.as_ptr().cast(), |
| 113 | + key.len(), |
| 114 | + value.as_ptr().cast(), |
| 115 | + value.len(), |
| 116 | + ) |
| 117 | + } |
| 118 | + } else { |
| 119 | + unsafe { ffi::ada_search_params_has(self.0, key.as_ptr().cast(), key.len()) } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /// Returns the value of the key. |
| 124 | + /// |
| 125 | + /// ``` |
| 126 | + /// use ada_url::URLSearchParams; |
| 127 | + /// let params = URLSearchParams::parse("a=1&b=2"); |
| 128 | + /// assert_eq!(params.get("a"), Some("1")); |
| 129 | + /// assert_eq!(params.get("c"), None); |
| 130 | + /// ``` |
| 131 | + pub fn get(&self, key: &str) -> Option<&str> { |
| 132 | + unsafe { |
| 133 | + let out = ffi::ada_search_params_get(self.0, key.as_ptr().cast(), key.len()); |
| 134 | + |
| 135 | + if out.data.is_null() { |
| 136 | + return None; |
| 137 | + } |
| 138 | + let slice = core::slice::from_raw_parts(out.data.cast(), out.length); |
| 139 | + Some(core::str::from_utf8_unchecked(slice)) |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /// Returns the stringified version of the URLSearchParams struct. |
| 144 | + /// |
| 145 | + /// ``` |
| 146 | + /// use ada_url::URLSearchParams; |
| 147 | + /// let params = URLSearchParams::parse("a=1&b=2"); |
| 148 | + /// assert_eq!(params.to_string(), "a=1&b=2"); |
| 149 | + /// ``` |
| 150 | + pub fn to_string(&self) -> &str { |
| 151 | + unsafe { |
| 152 | + let out = ffi::ada_search_params_to_string(self.0); |
| 153 | + let slice = core::slice::from_raw_parts(out.data.cast(), out.length); |
| 154 | + core::str::from_utf8_unchecked(slice) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + /// Returns all values of the key. |
| 159 | + /// |
| 160 | + /// ``` |
| 161 | + /// use ada_url::URLSearchParams; |
| 162 | + /// let params = URLSearchParams::parse("a=1&a=2"); |
| 163 | + /// assert_eq!(params.get_all("a"), vec!["1", "2"]); |
| 164 | + /// ``` |
| 165 | + pub fn get_all(&self, key: &str) -> Vec<&str> { |
| 166 | + unsafe { |
| 167 | + let strings = ffi::ada_search_params_get_all(self.0, key.as_ptr().cast(), key.len()); |
| 168 | + let size = ffi::ada_strings_size(strings); |
| 169 | + let mut out = Vec::with_capacity(size); |
| 170 | + |
| 171 | + if size == 0 { |
| 172 | + return out; |
| 173 | + } |
| 174 | + |
| 175 | + for index in 0..size { |
| 176 | + let string = ffi::ada_strings_get(strings, index); |
| 177 | + let slice = core::slice::from_raw_parts(string.data.cast(), string.length); |
| 178 | + out.push(core::str::from_utf8_unchecked(slice)); |
| 179 | + } |
| 180 | + |
| 181 | + out |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments