Skip to content

Commit 51fb75d

Browse files
committed
feat: add setter functions
1 parent c430d5e commit 51fb75d

File tree

1 file changed

+82
-9
lines changed

1 file changed

+82
-9
lines changed

src/lib.rs

Lines changed: 82 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,45 +157,100 @@ impl Url {
157157
}
158158
}
159159

160+
pub fn set_origin<U: AsRef<str>>(&mut self, input: U) -> bool {
161+
let input_with_0_terminate = std::ffi::CString::new(input.as_ref()).unwrap();
162+
unsafe { ffi::ada_set_origin(self.url, input_with_0_terminate.as_ptr()) }
163+
}
164+
160165
pub fn href(&self) -> &str {
161166
unsafe { ffi::ada_get_href(self.url) }.as_str()
162167
}
163168

169+
pub fn set_href<U: AsRef<str>>(&mut self, input: U) -> bool {
170+
let input_with_0_terminate = std::ffi::CString::new(input.as_ref()).unwrap();
171+
unsafe { ffi::ada_set_href(self.url, input_with_0_terminate.as_ptr()) }
172+
}
173+
164174
pub fn username(&self) -> &str {
165175
unsafe { ffi::ada_get_username(self.url) }.as_str()
166176
}
167177

178+
pub fn set_username<U: AsRef<str>>(&mut self, input: U) -> bool {
179+
let input_with_0_terminate = std::ffi::CString::new(input.as_ref()).unwrap();
180+
unsafe { ffi::ada_set_username(self.url, input_with_0_terminate.as_ptr()) }
181+
}
182+
168183
pub fn password(&self) -> &str {
169184
unsafe { ffi::ada_get_password(self.url) }.as_str()
170185
}
171186

187+
pub fn set_password<U: AsRef<str>>(&mut self, input: U) -> bool {
188+
let input_with_0_terminate = std::ffi::CString::new(input.as_ref()).unwrap();
189+
unsafe { ffi::ada_set_password(self.url, input_with_0_terminate.as_ptr()) }
190+
}
191+
172192
pub fn port(&self) -> &str {
173193
unsafe { ffi::ada_get_port(self.url) }.as_str()
174194
}
175195

196+
pub fn set_port<U: AsRef<str>>(&mut self, input: U) -> bool {
197+
let input_with_0_terminate = std::ffi::CString::new(input.as_ref()).unwrap();
198+
unsafe { ffi::ada_set_port(self.url, input_with_0_terminate.as_ptr()) }
199+
}
200+
176201
pub fn hash(&self) -> &str {
177202
unsafe { ffi::ada_get_hash(self.url) }.as_str()
178203
}
179204

205+
pub fn set_hash<U: AsRef<str>>(&mut self, input: U) {
206+
let input_with_0_terminate = std::ffi::CString::new(input.as_ref()).unwrap();
207+
unsafe { ffi::ada_set_hash(self.url, input_with_0_terminate.as_ptr()) }
208+
}
209+
180210
pub fn host(&self) -> &str {
181211
unsafe { ffi::ada_get_host(self.url) }.as_str()
182212
}
183213

214+
pub fn set_host<U: AsRef<str>>(&mut self, input: U) -> bool {
215+
let input_with_0_terminate = std::ffi::CString::new(input.as_ref()).unwrap();
216+
unsafe { ffi::ada_set_host(self.url, input_with_0_terminate.as_ptr()) }
217+
}
218+
184219
pub fn hostname(&self) -> &str {
185220
unsafe { ffi::ada_get_hostname(self.url) }.as_str()
186221
}
187222

223+
pub fn set_hostname<U: AsRef<str>>(&mut self, input: U) -> bool {
224+
let input_with_0_terminate = std::ffi::CString::new(input.as_ref()).unwrap();
225+
unsafe { ffi::ada_set_hostname(self.url, input_with_0_terminate.as_ptr()) }
226+
}
227+
188228
pub fn pathname(&self) -> &str {
189229
unsafe { ffi::ada_get_pathname(self.url) }.as_str()
190230
}
191231

232+
pub fn set_pathname<U: AsRef<str>>(&mut self, input: U) -> bool {
233+
let input_with_0_terminate = std::ffi::CString::new(input.as_ref()).unwrap();
234+
unsafe { ffi::ada_set_pathname(self.url, input_with_0_terminate.as_ptr()) }
235+
}
236+
192237
pub fn search(&self) -> &str {
193238
unsafe { ffi::ada_get_search(self.url) }.as_str()
194239
}
195240

241+
pub fn set_search<U: AsRef<str>>(&mut self, input: U) {
242+
let input_with_0_terminate = std::ffi::CString::new(input.as_ref()).unwrap();
243+
unsafe { ffi::ada_set_search(self.url, input_with_0_terminate.as_ptr()) }
244+
}
245+
196246
pub fn protocol(&self) -> &str {
197247
unsafe { ffi::ada_get_protocol(self.url) }.as_str()
198248
}
249+
250+
pub fn set_protocol<U: AsRef<str>>(&mut self, input: U) -> bool {
251+
let input_with_0_terminate = std::ffi::CString::new(input.as_ref()).unwrap();
252+
unsafe { ffi::ada_set_protocol(self.url, input_with_0_terminate.as_ptr()) }
253+
}
199254
}
200255

201256
#[cfg(test)]
@@ -211,15 +266,33 @@ mod test {
211266
out.href(),
212267
"https://username:[email protected]:9090/search?query#hash"
213268
);
214-
assert_eq!(out.username(), "username");
215-
assert_eq!(out.password(), "password");
216-
assert_eq!(out.port(), "9090");
217-
assert_eq!(out.hash(), "#hash");
218-
assert_eq!(out.host(), "google.com:9090");
219-
assert_eq!(out.hostname(), "google.com");
220-
assert_eq!(out.pathname(), "/search");
221-
assert_eq!(out.search(), "?query");
222-
assert_eq!(out.protocol(), "https:");
269+
270+
assert!(out.set_username("new-username"));
271+
assert_eq!(out.username(), "new-username");
272+
273+
assert!(out.set_password("new-password"));
274+
assert_eq!(out.password(), "new-password");
275+
276+
assert!(out.set_port("4242"));
277+
assert_eq!(out.port(), "4242");
278+
279+
out.set_hash("#new-hash");
280+
assert_eq!(out.hash(), "#new-hash");
281+
282+
assert!(out.set_host("yagiz.co:9999"));
283+
assert_eq!(out.host(), "yagiz.co:9999");
284+
285+
assert!(out.set_hostname("domain.com"));
286+
assert_eq!(out.hostname(), "domain.com");
287+
288+
assert!(out.set_pathname("/new-search"));
289+
assert_eq!(out.pathname(), "/new-search");
290+
291+
out.set_search("updated-query");
292+
assert_eq!(out.search(), "?updated-query");
293+
294+
out.set_protocol("wss");
295+
assert_eq!(out.protocol(), "wss:");
223296
}
224297

225298
#[test]

0 commit comments

Comments
 (0)