1use crate::{ffi, translate::*, Bytes, Error, UriFlags, UriHideFlags};
6
7crate::wrapper! {
8 #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
9 pub struct Uri(Shared<ffi::GUri>);
10
11 match fn {
12 ref => |ptr| ffi::g_uri_ref(ptr),
13 unref => |ptr| ffi::g_uri_unref(ptr),
14 type_ => || ffi::g_uri_get_type(),
15 }
16}
17
18impl Uri {
19 #[doc(alias = "g_uri_get_auth_params")]
20 #[doc(alias = "get_auth_params")]
21 pub fn auth_params(&self) -> Option<crate::GString> {
22 unsafe { from_glib_none(ffi::g_uri_get_auth_params(self.to_glib_none().0)) }
23 }
24
25 #[doc(alias = "g_uri_get_flags")]
26 #[doc(alias = "get_flags")]
27 pub fn flags(&self) -> UriFlags {
28 unsafe { from_glib(ffi::g_uri_get_flags(self.to_glib_none().0)) }
29 }
30
31 #[doc(alias = "g_uri_get_fragment")]
32 #[doc(alias = "get_fragment")]
33 pub fn fragment(&self) -> Option<crate::GString> {
34 unsafe { from_glib_none(ffi::g_uri_get_fragment(self.to_glib_none().0)) }
35 }
36
37 #[doc(alias = "g_uri_get_host")]
38 #[doc(alias = "get_host")]
39 pub fn host(&self) -> Option<crate::GString> {
40 unsafe { from_glib_none(ffi::g_uri_get_host(self.to_glib_none().0)) }
41 }
42
43 #[doc(alias = "g_uri_get_password")]
44 #[doc(alias = "get_password")]
45 pub fn password(&self) -> Option<crate::GString> {
46 unsafe { from_glib_none(ffi::g_uri_get_password(self.to_glib_none().0)) }
47 }
48
49 #[doc(alias = "g_uri_get_path")]
50 #[doc(alias = "get_path")]
51 pub fn path(&self) -> crate::GString {
52 unsafe { from_glib_none(ffi::g_uri_get_path(self.to_glib_none().0)) }
53 }
54
55 #[doc(alias = "g_uri_get_port")]
56 #[doc(alias = "get_port")]
57 pub fn port(&self) -> i32 {
58 unsafe { ffi::g_uri_get_port(self.to_glib_none().0) }
59 }
60
61 #[doc(alias = "g_uri_get_query")]
62 #[doc(alias = "get_query")]
63 pub fn query(&self) -> Option<crate::GString> {
64 unsafe { from_glib_none(ffi::g_uri_get_query(self.to_glib_none().0)) }
65 }
66
67 #[doc(alias = "g_uri_get_scheme")]
68 #[doc(alias = "get_scheme")]
69 pub fn scheme(&self) -> crate::GString {
70 unsafe { from_glib_none(ffi::g_uri_get_scheme(self.to_glib_none().0)) }
71 }
72
73 #[doc(alias = "g_uri_get_user")]
74 #[doc(alias = "get_user")]
75 pub fn user(&self) -> Option<crate::GString> {
76 unsafe { from_glib_none(ffi::g_uri_get_user(self.to_glib_none().0)) }
77 }
78
79 #[doc(alias = "g_uri_get_userinfo")]
80 #[doc(alias = "get_userinfo")]
81 pub fn userinfo(&self) -> Option<crate::GString> {
82 unsafe { from_glib_none(ffi::g_uri_get_userinfo(self.to_glib_none().0)) }
83 }
84
85 #[doc(alias = "g_uri_parse_relative")]
86 pub fn parse_relative(&self, uri_ref: &str, flags: UriFlags) -> Result<Uri, crate::Error> {
87 unsafe {
88 let mut error = std::ptr::null_mut();
89 let ret = ffi::g_uri_parse_relative(
90 self.to_glib_none().0,
91 uri_ref.to_glib_none().0,
92 flags.into_glib(),
93 &mut error,
94 );
95 if error.is_null() {
96 Ok(from_glib_full(ret))
97 } else {
98 Err(from_glib_full(error))
99 }
100 }
101 }
102
103 #[doc(alias = "g_uri_to_string")]
104 #[doc(alias = "to_string")]
105 pub fn to_str(&self) -> crate::GString {
106 unsafe { from_glib_full(ffi::g_uri_to_string(self.to_glib_none().0)) }
107 }
108
109 #[doc(alias = "g_uri_to_string_partial")]
110 pub fn to_string_partial(&self, flags: UriHideFlags) -> crate::GString {
111 unsafe {
112 from_glib_full(ffi::g_uri_to_string_partial(
113 self.to_glib_none().0,
114 flags.into_glib(),
115 ))
116 }
117 }
118
119 #[doc(alias = "g_uri_build")]
120 pub fn build(
121 flags: UriFlags,
122 scheme: &str,
123 userinfo: Option<&str>,
124 host: Option<&str>,
125 port: i32,
126 path: &str,
127 query: Option<&str>,
128 fragment: Option<&str>,
129 ) -> Uri {
130 unsafe {
131 from_glib_full(ffi::g_uri_build(
132 flags.into_glib(),
133 scheme.to_glib_none().0,
134 userinfo.to_glib_none().0,
135 host.to_glib_none().0,
136 port,
137 path.to_glib_none().0,
138 query.to_glib_none().0,
139 fragment.to_glib_none().0,
140 ))
141 }
142 }
143
144 #[doc(alias = "g_uri_build_with_user")]
145 pub fn build_with_user(
146 flags: UriFlags,
147 scheme: &str,
148 user: Option<&str>,
149 password: Option<&str>,
150 auth_params: Option<&str>,
151 host: Option<&str>,
152 port: i32,
153 path: &str,
154 query: Option<&str>,
155 fragment: Option<&str>,
156 ) -> Uri {
157 unsafe {
158 from_glib_full(ffi::g_uri_build_with_user(
159 flags.into_glib(),
160 scheme.to_glib_none().0,
161 user.to_glib_none().0,
162 password.to_glib_none().0,
163 auth_params.to_glib_none().0,
164 host.to_glib_none().0,
165 port,
166 path.to_glib_none().0,
167 query.to_glib_none().0,
168 fragment.to_glib_none().0,
169 ))
170 }
171 }
172
173 #[doc(alias = "g_uri_escape_bytes")]
174 pub fn escape_bytes(unescaped: &[u8], reserved_chars_allowed: Option<&str>) -> crate::GString {
175 let length = unescaped.len() as _;
176 unsafe {
177 from_glib_full(ffi::g_uri_escape_bytes(
178 unescaped.to_glib_none().0,
179 length,
180 reserved_chars_allowed.to_glib_none().0,
181 ))
182 }
183 }
184
185 #[doc(alias = "g_uri_escape_string")]
186 pub fn escape_string(
187 unescaped: &str,
188 reserved_chars_allowed: Option<&str>,
189 allow_utf8: bool,
190 ) -> crate::GString {
191 unsafe {
192 from_glib_full(ffi::g_uri_escape_string(
193 unescaped.to_glib_none().0,
194 reserved_chars_allowed.to_glib_none().0,
195 allow_utf8.into_glib(),
196 ))
197 }
198 }
199
200 #[doc(alias = "g_uri_is_valid")]
201 pub fn is_valid(uri_string: &str, flags: UriFlags) -> Result<(), crate::Error> {
202 unsafe {
203 let mut error = std::ptr::null_mut();
204 let is_ok =
205 ffi::g_uri_is_valid(uri_string.to_glib_none().0, flags.into_glib(), &mut error);
206 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
207 if error.is_null() {
208 Ok(())
209 } else {
210 Err(from_glib_full(error))
211 }
212 }
213 }
214
215 #[doc(alias = "g_uri_join")]
216 pub fn join(
217 flags: UriFlags,
218 scheme: Option<&str>,
219 userinfo: Option<&str>,
220 host: Option<&str>,
221 port: i32,
222 path: &str,
223 query: Option<&str>,
224 fragment: Option<&str>,
225 ) -> crate::GString {
226 unsafe {
227 from_glib_full(ffi::g_uri_join(
228 flags.into_glib(),
229 scheme.to_glib_none().0,
230 userinfo.to_glib_none().0,
231 host.to_glib_none().0,
232 port,
233 path.to_glib_none().0,
234 query.to_glib_none().0,
235 fragment.to_glib_none().0,
236 ))
237 }
238 }
239
240 #[doc(alias = "g_uri_join_with_user")]
241 pub fn join_with_user(
242 flags: UriFlags,
243 scheme: Option<&str>,
244 user: Option<&str>,
245 password: Option<&str>,
246 auth_params: Option<&str>,
247 host: Option<&str>,
248 port: i32,
249 path: &str,
250 query: Option<&str>,
251 fragment: Option<&str>,
252 ) -> crate::GString {
253 unsafe {
254 from_glib_full(ffi::g_uri_join_with_user(
255 flags.into_glib(),
256 scheme.to_glib_none().0,
257 user.to_glib_none().0,
258 password.to_glib_none().0,
259 auth_params.to_glib_none().0,
260 host.to_glib_none().0,
261 port,
262 path.to_glib_none().0,
263 query.to_glib_none().0,
264 fragment.to_glib_none().0,
265 ))
266 }
267 }
268
269 #[doc(alias = "g_uri_list_extract_uris")]
270 pub fn list_extract_uris(uri_list: &str) -> Vec<crate::GString> {
271 unsafe {
272 FromGlibPtrContainer::from_glib_full(ffi::g_uri_list_extract_uris(
273 uri_list.to_glib_none().0,
274 ))
275 }
276 }
277
278 #[doc(alias = "g_uri_parse")]
279 pub fn parse(uri_string: &str, flags: UriFlags) -> Result<Uri, crate::Error> {
280 unsafe {
281 let mut error = std::ptr::null_mut();
282 let ret = ffi::g_uri_parse(uri_string.to_glib_none().0, flags.into_glib(), &mut error);
283 if error.is_null() {
284 Ok(from_glib_full(ret))
285 } else {
286 Err(from_glib_full(error))
287 }
288 }
289 }
290
291 #[doc(alias = "g_uri_parse_scheme")]
297 pub fn parse_scheme(uri: &str) -> Option<crate::GString> {
298 unsafe { from_glib_full(ffi::g_uri_parse_scheme(uri.to_glib_none().0)) }
299 }
300
301 #[doc(alias = "g_uri_peek_scheme")]
302 pub fn peek_scheme(uri: &str) -> Option<crate::GString> {
303 unsafe { from_glib_none(ffi::g_uri_peek_scheme(uri.to_glib_none().0)) }
304 }
305
306 #[doc(alias = "g_uri_resolve_relative")]
307 pub fn resolve_relative(
308 base_uri_string: Option<&str>,
309 uri_ref: &str,
310 flags: UriFlags,
311 ) -> Result<crate::GString, crate::Error> {
312 unsafe {
313 let mut error = std::ptr::null_mut();
314 let ret = ffi::g_uri_resolve_relative(
315 base_uri_string.to_glib_none().0,
316 uri_ref.to_glib_none().0,
317 flags.into_glib(),
318 &mut error,
319 );
320 if error.is_null() {
321 Ok(from_glib_full(ret))
322 } else {
323 Err(from_glib_full(error))
324 }
325 }
326 }
327
328 #[doc(alias = "g_uri_split")]
329 pub fn split(
330 uri_ref: &str,
331 flags: UriFlags,
332 ) -> Result<
333 (
334 Option<crate::GString>,
335 Option<crate::GString>,
336 Option<crate::GString>,
337 i32,
338 crate::GString,
339 Option<crate::GString>,
340 Option<crate::GString>,
341 ),
342 crate::Error,
343 > {
344 unsafe {
345 let mut scheme = std::ptr::null_mut();
346 let mut userinfo = std::ptr::null_mut();
347 let mut host = std::ptr::null_mut();
348 let mut port = std::mem::MaybeUninit::uninit();
349 let mut path = std::ptr::null_mut();
350 let mut query = std::ptr::null_mut();
351 let mut fragment = std::ptr::null_mut();
352 let mut error = std::ptr::null_mut();
353 let is_ok = ffi::g_uri_split(
354 uri_ref.to_glib_none().0,
355 flags.into_glib(),
356 &mut scheme,
357 &mut userinfo,
358 &mut host,
359 port.as_mut_ptr(),
360 &mut path,
361 &mut query,
362 &mut fragment,
363 &mut error,
364 );
365 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
366 if error.is_null() {
367 Ok((
368 from_glib_full(scheme),
369 from_glib_full(userinfo),
370 from_glib_full(host),
371 port.assume_init(),
372 from_glib_full(path),
373 from_glib_full(query),
374 from_glib_full(fragment),
375 ))
376 } else {
377 Err(from_glib_full(error))
378 }
379 }
380 }
381
382 #[doc(alias = "g_uri_split_network")]
383 pub fn split_network(
384 uri_string: &str,
385 flags: UriFlags,
386 ) -> Result<(Option<crate::GString>, Option<crate::GString>, i32), crate::Error> {
387 unsafe {
388 let mut scheme = std::ptr::null_mut();
389 let mut host = std::ptr::null_mut();
390 let mut port = std::mem::MaybeUninit::uninit();
391 let mut error = std::ptr::null_mut();
392 let is_ok = ffi::g_uri_split_network(
393 uri_string.to_glib_none().0,
394 flags.into_glib(),
395 &mut scheme,
396 &mut host,
397 port.as_mut_ptr(),
398 &mut error,
399 );
400 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
401 if error.is_null() {
402 Ok((
403 from_glib_full(scheme),
404 from_glib_full(host),
405 port.assume_init(),
406 ))
407 } else {
408 Err(from_glib_full(error))
409 }
410 }
411 }
412
413 #[doc(alias = "g_uri_split_with_user")]
414 pub fn split_with_user(
415 uri_ref: &str,
416 flags: UriFlags,
417 ) -> Result<
418 (
419 Option<crate::GString>,
420 Option<crate::GString>,
421 Option<crate::GString>,
422 Option<crate::GString>,
423 Option<crate::GString>,
424 i32,
425 crate::GString,
426 Option<crate::GString>,
427 Option<crate::GString>,
428 ),
429 crate::Error,
430 > {
431 unsafe {
432 let mut scheme = std::ptr::null_mut();
433 let mut user = std::ptr::null_mut();
434 let mut password = std::ptr::null_mut();
435 let mut auth_params = std::ptr::null_mut();
436 let mut host = std::ptr::null_mut();
437 let mut port = std::mem::MaybeUninit::uninit();
438 let mut path = std::ptr::null_mut();
439 let mut query = std::ptr::null_mut();
440 let mut fragment = std::ptr::null_mut();
441 let mut error = std::ptr::null_mut();
442 let is_ok = ffi::g_uri_split_with_user(
443 uri_ref.to_glib_none().0,
444 flags.into_glib(),
445 &mut scheme,
446 &mut user,
447 &mut password,
448 &mut auth_params,
449 &mut host,
450 port.as_mut_ptr(),
451 &mut path,
452 &mut query,
453 &mut fragment,
454 &mut error,
455 );
456 debug_assert_eq!(is_ok == crate::ffi::GFALSE, !error.is_null());
457 if error.is_null() {
458 Ok((
459 from_glib_full(scheme),
460 from_glib_full(user),
461 from_glib_full(password),
462 from_glib_full(auth_params),
463 from_glib_full(host),
464 port.assume_init(),
465 from_glib_full(path),
466 from_glib_full(query),
467 from_glib_full(fragment),
468 ))
469 } else {
470 Err(from_glib_full(error))
471 }
472 }
473 }
474
475 #[doc(alias = "g_uri_unescape_bytes")]
476 pub fn unescape_bytes(
477 escaped_string: &str,
478 illegal_characters: Option<&str>,
479 ) -> Result<Bytes, crate::Error> {
480 let length = escaped_string.len() as _;
481 unsafe {
482 let mut error = std::ptr::null_mut();
483 let ret = ffi::g_uri_unescape_bytes(
484 escaped_string.to_glib_none().0,
485 length,
486 illegal_characters.to_glib_none().0,
487 &mut error,
488 );
489 if error.is_null() {
490 Ok(from_glib_full(ret))
491 } else {
492 Err(from_glib_full(error))
493 }
494 }
495 }
496
497 #[doc(alias = "g_uri_unescape_segment")]
498 pub fn unescape_segment(
499 escaped_string: Option<&str>,
500 escaped_string_end: Option<&str>,
501 illegal_characters: Option<&str>,
502 ) -> Option<crate::GString> {
503 unsafe {
504 from_glib_full(ffi::g_uri_unescape_segment(
505 escaped_string.to_glib_none().0,
506 escaped_string_end.to_glib_none().0,
507 illegal_characters.to_glib_none().0,
508 ))
509 }
510 }
511
512 #[doc(alias = "g_uri_unescape_string")]
513 pub fn unescape_string(
514 escaped_string: &str,
515 illegal_characters: Option<&str>,
516 ) -> Option<crate::GString> {
517 unsafe {
518 from_glib_full(ffi::g_uri_unescape_string(
519 escaped_string.to_glib_none().0,
520 illegal_characters.to_glib_none().0,
521 ))
522 }
523 }
524}
525
526impl std::fmt::Display for Uri {
527 #[inline]
528 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
529 f.write_str(&self.to_str())
530 }
531}
532
533unsafe impl Send for Uri {}
534unsafe impl Sync for Uri {}