pub struct BetterUrl { /* private fields */ }Expand description
A wrapper around a Url with extra metadata.
Currently the only included metadata is a HostDetails, which currently only caches PSL information for more efficient reg domain, domain suffix, etc..
Implementations§
Source§impl BetterUrl
impl BetterUrl
Sourcepub fn path_has_segments(&self) -> bool
pub fn path_has_segments(&self) -> bool
Returns true if the path has segments.
Sourcepub fn set_path_segments<'a, I>(
&mut self,
iter: I,
) -> Result<(), SetPathSegmentsError>where
I: IntoIterator<Item = &'a str>,
pub fn set_path_segments<'a, I>(
&mut self,
iter: I,
) -> Result<(), SetPathSegmentsError>where
I: IntoIterator<Item = &'a str>,
Set Url::path_segments.
§Errors
If the URL doesn’t have path segments, returns the error SetPathSegmentsError::UrlDoesNotHavePathSegments.
If iter has a length of zero, returns the error SetPathSegmentsError::CannotHaveZeroPathSegments.
If a segment contains a /, returns the error SetPathSegmentsError::PathSegmentCannotContainSlash.
§Examples
use better_url::*;
let mut url = BetterUrl::parse("https://example.com").unwrap();
url.set_path_segments([]).unwrap_err();
url.set_path_segments(["a"]).unwrap();
assert_eq!(url.path(), "/a");
url.set_path_segments(["a", "b"]).unwrap();
assert_eq!(url.path(), "/a/b");
url.set_path_segments(["a/b/c"]).unwrap_err();
assert_eq!(url.path(), "/a/b");Sourcepub fn set_raw_path_segments<'a, I>(
&mut self,
iter: I,
) -> Result<(), SetPathSegmentsError>where
I: IntoIterator<Item = &'a str>,
pub fn set_raw_path_segments<'a, I>(
&mut self,
iter: I,
) -> Result<(), SetPathSegmentsError>where
I: IntoIterator<Item = &'a str>,
Set Url::path_segments without checking if a segment contains a /.
Useful for optimizations.
§Errors
If the URL doesn’t have path segments, returns the error SetPathSegmentsError::UrlDoesNotHavePathSegments.
If iter has a length of zero, returns the error SetPathSegmentsError::CannotHaveZeroPathSegments.
§Examples
use better_url::*;
let mut url = BetterUrl::parse("https://example.com").unwrap();
url.set_raw_path_segments([]).unwrap_err();
url.set_raw_path_segments(["a"]).unwrap();
assert_eq!(url.path(), "/a");
url.set_raw_path_segments(["a", "b"]).unwrap();
assert_eq!(url.path(), "/a/b");
url.set_raw_path_segments(["a/b/c"]).unwrap();
assert_eq!(url.path(), "/a/b/c");Sourcepub fn set_path_segments_str(
&mut self,
to: &str,
) -> Result<(), SetPathSegmentsStrError>
pub fn set_path_segments_str( &mut self, to: &str, ) -> Result<(), SetPathSegmentsStrError>
Set Url::path_segments.
§Errors
If the URL doesn’t have path segments, returns the error SetPathSegmentsStrError::UrlDoesNotHavePathSegments.
Sourcepub fn path_segment(&self, index: isize) -> Option<Option<&str>>
pub fn path_segment(&self, index: isize) -> Option<Option<&str>>
Gets the specified path segment.
§Examples
use better_url::*;
let mut url = BetterUrl::parse("https://example.com/a/b/c").unwrap();
assert_eq!(url.path_segment(-4), Some(None ));
assert_eq!(url.path_segment(-3), Some(Some("a".into())));
assert_eq!(url.path_segment(-2), Some(Some("b".into())));
assert_eq!(url.path_segment(-1), Some(Some("c".into())));
assert_eq!(url.path_segment( 0), Some(Some("a".into())));
assert_eq!(url.path_segment( 1), Some(Some("b".into())));
assert_eq!(url.path_segment( 2), Some(Some("c".into())));
assert_eq!(url.path_segment( 3), Some(None ));Sourcepub fn path_segments_mut(&mut self) -> Option<PathSegmentsMut<'_>>
pub fn path_segments_mut(&mut self) -> Option<PathSegmentsMut<'_>>
Gets an object that can mutate the segments of Self’s path.
§Examples
use better_url::*;
let mut url = BetterUrl::parse("https://example.com/a/b/c/").unwrap();
url.path_segments_mut().unwrap().pop(); assert_eq!(url.path(), "/a/b/c");
url.path_segments_mut().unwrap().pop(); assert_eq!(url.path(), "/a/b");
url.path_segments_mut().unwrap().pop(); assert_eq!(url.path(), "/a");
url.path_segments_mut().unwrap().pop(); assert_eq!(url.path(), "/");
url.path_segments_mut().unwrap().pop(); assert_eq!(url.path(), "/");Sourcepub fn path_segments_str(&self) -> Option<&str>
pub fn path_segments_str(&self) -> Option<&str>
Url::path with the leading / removed.
When split on /, gives identical values to Url::path_segments.
Sourcepub fn set_path_segment(
&mut self,
index: isize,
value: Option<&str>,
) -> Result<(), SetPathSegmentError>
pub fn set_path_segment( &mut self, index: isize, value: Option<&str>, ) -> Result<(), SetPathSegmentError>
Sets the specified path segment.
§Errors
If a segment contains a /, returns the error SetPathSegmentsError::PathSegmentCannotContainSlash.
If the call to Self::set_raw_path_segment returns an error, that error is returned.
§Examples
use better_url::*;
let mut url = BetterUrl::parse("https://example.com/aa/bb/cc").unwrap();
url.set_path_segment(-4, Some("-4")).unwrap_err(); assert_eq!(url.path(), "/aa/bb/cc");
url.set_path_segment(-3, Some("-3")).unwrap (); assert_eq!(url.path(), "/-3/bb/cc");
url.set_path_segment(-2, Some("-2")).unwrap (); assert_eq!(url.path(), "/-3/-2/cc");
url.set_path_segment(-1, Some("-1")).unwrap (); assert_eq!(url.path(), "/-3/-2/-1");
url.set_path_segment( 0, Some("00")).unwrap (); assert_eq!(url.path(), "/00/-2/-1");
url.set_path_segment( 1, Some("+1")).unwrap (); assert_eq!(url.path(), "/00/+1/-1");
url.set_path_segment( 2, Some("+2")).unwrap (); assert_eq!(url.path(), "/00/+1/+2");
url.set_path_segment( 3, Some("+3")).unwrap_err(); assert_eq!(url.path(), "/00/+1/+2");
url.set_path_segment( 0, None).unwrap(); assert_eq!(url.path(), "/+1/+2");
url.set_path_segment(-1, None).unwrap(); assert_eq!(url.path(), "/+1");Sourcepub fn set_raw_path_segment(
&mut self,
index: isize,
value: Option<&str>,
) -> Result<(), SetPathSegmentError>
pub fn set_raw_path_segment( &mut self, index: isize, value: Option<&str>, ) -> Result<(), SetPathSegmentError>
Sets the specified path segment without checking if the segment contains a /.
Useful for optimizations.
§Errors
If the call to Self::path_segments_str returns None, returns the error SetPathSegmentError::UrlDoesNotHavePathSegments.
If the specified path segment isn’t found, returns the error SetPathSegmentError::SegmentNotFound.
Sourcepub fn insert_path_segment(
&mut self,
index: isize,
value: &str,
) -> Result<(), InsertPathSegmentError>
pub fn insert_path_segment( &mut self, index: isize, value: &str, ) -> Result<(), InsertPathSegmentError>
Inserts a path segment at the specified path segment.
If the specified segment is one after the last, inserts a new segment at the end.
§Errors
If the call to Self::path_segments_str returns None, returns the error InsertPathSegmentError::UrlDoesNotHavePathSegments.
If the specified path segment isn’t found, returns the error InsertPathSegmentError::SegmentNotFound.
If a segment contains a /, returns the error SetPathSegmentsError::PathSegmentCannotContainSlash.
§Examples
use better_url::*;
let mut url = BetterUrl::parse("https://example.com/").unwrap();
url.insert_path_segment(0, "abc").unwrap();
assert_eq!(url.path(), "/abc/");
url.insert_path_segment(0, "def").unwrap();
assert_eq!(url.path(), "/def/abc/");
url.insert_path_segment(3, "ghi").unwrap();
assert_eq!(url.path(), "/def/abc//ghi");
url.insert_path_segment(6, "err").unwrap_err();
assert_eq!(url.path(), "/def/abc//ghi");
url.insert_path_segment(0, "a/b").unwrap_err();
assert_eq!(url.path(), "/def/abc//ghi");Sourcepub fn insert_raw_path_segment(
&mut self,
index: isize,
value: &str,
) -> Result<(), InsertPathSegmentError>
pub fn insert_raw_path_segment( &mut self, index: isize, value: &str, ) -> Result<(), InsertPathSegmentError>
Inserts a path segment at the specified path segment without checking if a segment contains a /.
If the specified segment is one after the last, inserts a new segment at the end.
§Errors
If the call to Self::path_segments_str returns None, returns the error InsertPathSegmentError::UrlDoesNotHavePathSegments.
If the specified path segment isn’t found, returns the error InsertPathSegmentError::SegmentNotFound.
§Examples
use better_url::*;
let mut url = BetterUrl::parse("https://example.com/").unwrap();
url.insert_raw_path_segment(0, "abc").unwrap();
assert_eq!(url.path(), "/abc/");
url.insert_raw_path_segment(0, "def").unwrap();
assert_eq!(url.path(), "/def/abc/");
url.insert_raw_path_segment(3, "ghi").unwrap();
assert_eq!(url.path(), "/def/abc//ghi");
url.insert_raw_path_segment(6, "err").unwrap_err();
assert_eq!(url.path(), "/def/abc//ghi");
url.insert_raw_path_segment(0, "a/b").unwrap();
assert_eq!(url.path(), "/a/b/def/abc//ghi");Sourcepub fn first_n_path_segments(&self, n: usize) -> Option<Option<&str>>
pub fn first_n_path_segments(&self, n: usize) -> Option<Option<&str>>
Get the first n path segments.
Sourcepub fn path_segments_after_first_n(&self, n: usize) -> Option<Option<&str>>
pub fn path_segments_after_first_n(&self, n: usize) -> Option<Option<&str>>
Gets the path segments except for the first n.
Sourcepub fn last_n_path_segments(&self, n: usize) -> Option<Option<&str>>
pub fn last_n_path_segments(&self, n: usize) -> Option<Option<&str>>
Get the last n path segments.
Sourcepub fn path_segments_before_last_n(&self, n: usize) -> Option<Option<&str>>
pub fn path_segments_before_last_n(&self, n: usize) -> Option<Option<&str>>
Gets the path segments except for the last n.
Sourcepub fn set_first_n_path_segments(
&mut self,
n: usize,
to: Option<&str>,
) -> Result<(), SetFirstNPathSegmentsError>
pub fn set_first_n_path_segments( &mut self, n: usize, to: Option<&str>, ) -> Result<(), SetFirstNPathSegmentsError>
Sets the first n path segments.
Does not require to to have n segments.
§Errors
If the URL doesn’t have path segments, returns the error SetFirstNPathSegmentsError::UrlDoesNotHavePathSegments.
If there aren’t enough path segments, returns the error SetFirstNPathSegmentsError::NotEnoughPathSegments.
§Examples
use better_url::*;
let mut url = BetterUrl::parse("https://example.com/0/1/2/3/4/5").unwrap();
url.set_first_n_path_segments(0, Some("a")).unwrap();
assert_eq!(url.path(), "/0/1/2/3/4/5");
url.set_first_n_path_segments(0, None).unwrap();
assert_eq!(url.path(), "/0/1/2/3/4/5");
url.set_first_n_path_segments(1, Some("a")).unwrap();
assert_eq!(url.path(), "/a/1/2/3/4/5");
url.set_first_n_path_segments(1, None).unwrap();
assert_eq!(url.path(), "/1/2/3/4/5");
url.set_first_n_path_segments(2, Some("a")).unwrap();
assert_eq!(url.path(), "/a/3/4/5");
url.set_first_n_path_segments(2, None).unwrap();
assert_eq!(url.path(), "/4/5");
url.set_first_n_path_segments(3, Some("a")).unwrap_err();
assert_eq!(url.path(), "/4/5");
url.set_first_n_path_segments(3, None).unwrap_err();
assert_eq!(url.path(), "/4/5");Sourcepub fn set_path_segments_after_first_n(
&mut self,
n: usize,
to: Option<&str>,
) -> Result<(), SetPathSegmentsAfterFirstNError>
pub fn set_path_segments_after_first_n( &mut self, n: usize, to: Option<&str>, ) -> Result<(), SetPathSegmentsAfterFirstNError>
Sets the path segments except for the first n.
Does not require to to have n segments.
§Errors
If the URL doesn’t have path segments, returns the error SetFirstNPathSegmentsError::UrlDoesNotHavePathSegments.
If there aren’t enough path segments, returns the error SetFirstNPathSegmentsError::NotEnoughPathSegments.
§Examples
use better_url::*;
let mut url = BetterUrl::parse("https://example.com/0/1/2/3/4/5").unwrap();
url.set_path_segments_after_first_n(3, Some("a")).unwrap();
assert_eq!(url.path(), "/0/1/2/a");
url.set_path_segments_after_first_n(3, None).unwrap();
assert_eq!(url.path(), "/0/1/2");
url.set_path_segments_after_first_n(2, Some("a")).unwrap();
assert_eq!(url.path(), "/0/1/a");
url.set_path_segments_after_first_n(2, None).unwrap();
assert_eq!(url.path(), "/0/1");
url.set_path_segments_after_first_n(1, Some("a")).unwrap();
assert_eq!(url.path(), "/0/a");
url.set_path_segments_after_first_n(1, None).unwrap();
assert_eq!(url.path(), "/0");
url.set_path_segments_after_first_n(0, Some("a")).unwrap();
assert_eq!(url.path(), "/a");
url.set_path_segments_after_first_n(0, None).unwrap_err();
assert_eq!(url.path(), "/a");Sourcepub fn set_last_n_path_segments(
&mut self,
n: usize,
to: Option<&str>,
) -> Result<(), SetLastNPathSegmentsError>
pub fn set_last_n_path_segments( &mut self, n: usize, to: Option<&str>, ) -> Result<(), SetLastNPathSegmentsError>
Sets the last n path segments.
Does not require to to have n segments.
§Errors
If the URL doesn’t have path segments, returns the error SetLastNPathSegmentsError::UrlDoesNotHavePathSegments.
If there aren’t enough path segments, returns the error SetLastNPathSegmentsError::NotEnoughPathSegments.
§Examples
use better_url::*;
let mut url = BetterUrl::parse("https://example.com/0/1/2/3/4/5").unwrap();
url.set_last_n_path_segments(0, Some("a")).unwrap();
assert_eq!(url.path(), "/0/1/2/3/4/5");
url.set_last_n_path_segments(0, None).unwrap();
assert_eq!(url.path(), "/0/1/2/3/4/5");
url.set_last_n_path_segments(1, Some("a")).unwrap();
assert_eq!(url.path(), "/0/1/2/3/4/a");
url.set_last_n_path_segments(1, None).unwrap();
assert_eq!(url.path(), "/0/1/2/3/4");
url.set_last_n_path_segments(2, Some("a")).unwrap();
assert_eq!(url.path(), "/0/1/2/a");
url.set_last_n_path_segments(2, None).unwrap();
assert_eq!(url.path(), "/0/1");
url.set_last_n_path_segments(3, Some("a")).unwrap_err();
assert_eq!(url.path(), "/0/1");
url.set_last_n_path_segments(3, None).unwrap_err();
assert_eq!(url.path(), "/0/1");Sourcepub fn set_path_segments_before_last_n(
&mut self,
n: usize,
to: Option<&str>,
) -> Result<(), SetPathSegmentsBeforeLastNError>
pub fn set_path_segments_before_last_n( &mut self, n: usize, to: Option<&str>, ) -> Result<(), SetPathSegmentsBeforeLastNError>
Sets the path segments except for the last n.
Does not require to to have n segments.
§Errors
If the URL doesn’t have path segments, returns the error SetLastNPathSegmentsError::UrlDoesNotHavePathSegments.
If there aren’t enough path segments, returns the error SetLastNPathSegmentsError::NotEnoughPathSegments.
§Examples
use better_url::*;
let mut url = BetterUrl::parse("https://example.com/0/1/2/3/4/5").unwrap();
url.set_path_segments_before_last_n(3, Some("a")).unwrap();
assert_eq!(url.path(), "/a/3/4/5");
url.set_path_segments_before_last_n(3, None).unwrap();
assert_eq!(url.path(), "/3/4/5");
url.set_path_segments_before_last_n(2, Some("a")).unwrap();
assert_eq!(url.path(), "/a/4/5");
url.set_path_segments_before_last_n(2, None).unwrap();
assert_eq!(url.path(), "/4/5");
url.set_path_segments_before_last_n(1, Some("a")).unwrap();
assert_eq!(url.path(), "/a/5");
url.set_path_segments_before_last_n(1, None).unwrap();
assert_eq!(url.path(), "/5");
url.set_path_segments_before_last_n(0, Some("a")).unwrap();
assert_eq!(url.path(), "/a");
url.set_path_segments_before_last_n(0, None).unwrap_err();
assert_eq!(url.path(), "/a");Sourcepub fn remove_first_n_path_segments(
&mut self,
n: usize,
) -> Result<(), RemoveFirstNPathSegmentsError>
pub fn remove_first_n_path_segments( &mut self, n: usize, ) -> Result<(), RemoveFirstNPathSegmentsError>
Remove the first n path segments.
The number of path segments after this succeeds is equal to the number of path segments before this is applied minus n.
Because a path can’t have zero segments, trying to remove all segments counts as not having enough segments. If this is a serious ergonomics issue for you, I’ll prioritize making a workaround.
§Errors
If the URL doesn’t have path segments, returns the error RemoveFirstNPathSegmentsError::UrlDoesNotHavePathSegments.
If there aren’t enough segments, returns the error RemoveFirstNPathSegmentsError::NotEnoughPathSegments.
Sourcepub fn keep_first_n_path_segments(
&mut self,
n: usize,
) -> Result<(), KeepFirstNPathSegmentsError>
pub fn keep_first_n_path_segments( &mut self, n: usize, ) -> Result<(), KeepFirstNPathSegmentsError>
Keep the first n path segments.
The number of path segments after this succeeds is equal to n.
Because a path can’t have zero segments, trying to keep zero segments always errors. This is easy to just not do.
§Errors
If the URL doesn’t have path segments, returns the error KeepFirstNPathSegmentsError::UrlDoesNotHavePathSegments.
If there aren’t enough segments, returns the error KeepFirstNPathSegmentsError::NotEnoughPathSegments.
Sourcepub fn remove_last_n_path_segments(
&mut self,
n: usize,
) -> Result<(), RemoveLastNPathSegmentsError>
pub fn remove_last_n_path_segments( &mut self, n: usize, ) -> Result<(), RemoveLastNPathSegmentsError>
Remove the last n path segments.
The number of path segments after this succeeds is equal to the number of path segments before this is applied minus n.
Because a path can’t have zero segments, trying to remove all segments counts as not having enough segments. If this is a serious ergonomics issue for you, I’ll prioritize making a workaround.
§Errors
If the URL doesn’t have path segments, returns the error RemoveLastNPathSegmentsError::UrlDoesNotHavePathSegments.
If there aren’t enough segments, returns the error RemoveLastNPathSegmentsError::NotEnoughPathSegments.
Sourcepub fn keep_last_n_path_segments(
&mut self,
n: usize,
) -> Result<(), KeepLastNPathSegmentsError>
pub fn keep_last_n_path_segments( &mut self, n: usize, ) -> Result<(), KeepLastNPathSegmentsError>
Keep the last n path segments.
The number of path segments after this succeeds is equal to n.
Because a path can’t have zero segments, trying to keep zero segments always errors. This is easy to just not do.
§Errors
If the URL doesn’t have path segments, returns the error KeepLastNPathSegmentsError::UrlDoesNotHavePathSegments.
If there aren’t enough segments, returns the error KeepLastNPathSegmentsError::NotEnoughPathSegments.
Source§impl BetterUrl
impl BetterUrl
Sourcepub fn domain_segment(&self, index: isize) -> Option<&str>
pub fn domain_segment(&self, index: isize) -> Option<&str>
§Examples
use better_url::*;
let url = BetterUrl::parse("https://abc.def.example.co.uk").unwrap();
assert_eq!(url.domain_segment(-6), None );
assert_eq!(url.domain_segment(-5), Some("abc" .into()));
assert_eq!(url.domain_segment(-4), Some("def" .into()));
assert_eq!(url.domain_segment(-3), Some("example".into()));
assert_eq!(url.domain_segment(-2), Some("co" .into()));
assert_eq!(url.domain_segment(-1), Some("uk" .into()));
assert_eq!(url.domain_segment( 0), Some("abc" .into()));
assert_eq!(url.domain_segment( 1), Some("def" .into()));
assert_eq!(url.domain_segment( 2), Some("example".into()));
assert_eq!(url.domain_segment( 3), Some("co" .into()));
assert_eq!(url.domain_segment( 4), Some("uk" .into()));
assert_eq!(url.domain_segment( 5), None );Sourcepub fn set_domain_segment(
&mut self,
index: isize,
value: Option<&str>,
) -> Result<(), SetDomainSegmentError>
pub fn set_domain_segment( &mut self, index: isize, value: Option<&str>, ) -> Result<(), SetDomainSegmentError>
Sets the specified domain segment.
§Errors
If the URL doesn’t have a domain, returns the error SetDomainSegmentError::UrlDoesNotHaveDomain.
If the segment isn’t found, returns the error SetDomainSegmentError::SegmentNotFound.
If the call to Self::set_domain returns an error, that error is returned.
§Examples
use better_url::*;
let mut url = BetterUrl::parse("https://abc.def.example.co.uk").unwrap();
url.set_domain_segment(-6, Some("n6")).unwrap_err(); assert_eq!(url.host_str(), Some("abc.def.example.co.uk"));
url.set_domain_segment(-5, Some("n5")).unwrap (); assert_eq!(url.host_str(), Some("n5.def.example.co.uk" ));
url.set_domain_segment(-4, Some("n4")).unwrap (); assert_eq!(url.host_str(), Some("n5.n4.example.co.uk" ));
url.set_domain_segment(-3, Some("n3")).unwrap (); assert_eq!(url.host_str(), Some("n5.n4.n3.co.uk" ));
url.set_domain_segment(-2, Some("n2")).unwrap (); assert_eq!(url.host_str(), Some("n5.n4.n3.n2.uk" ));
url.set_domain_segment(-1, Some("n1")).unwrap (); assert_eq!(url.host_str(), Some("n5.n4.n3.n2.n1" ));
url.set_domain_segment( 0, Some("p0")).unwrap (); assert_eq!(url.host_str(), Some("p0.n4.n3.n2.n1" ));
url.set_domain_segment( 1, Some("p1")).unwrap (); assert_eq!(url.host_str(), Some("p0.p1.n3.n2.n1" ));
url.set_domain_segment( 2, Some("p2")).unwrap (); assert_eq!(url.host_str(), Some("p0.p1.p2.n2.n1" ));
url.set_domain_segment( 3, Some("p3")).unwrap (); assert_eq!(url.host_str(), Some("p0.p1.p2.p3.n1" ));
url.set_domain_segment( 4, Some("p4")).unwrap (); assert_eq!(url.host_str(), Some("p0.p1.p2.p3.p4" ));
url.set_domain_segment( 5, Some("p5")).unwrap_err(); assert_eq!(url.host_str(), Some("p0.p1.p2.p3.p4" ));
url.set_domain_segment( 0, None).unwrap(); assert_eq!(url.host_str(), Some("p1.p2.p3.p4"));
url.set_domain_segment(-1, None).unwrap(); assert_eq!(url.host_str(), Some("p1.p2.p3"));Sourcepub fn insert_domain_segment(
&mut self,
index: isize,
value: &str,
) -> Result<(), InsertDomainSegmentError>
pub fn insert_domain_segment( &mut self, index: isize, value: &str, ) -> Result<(), InsertDomainSegmentError>
Inserts a new domain segment at the specified index.
§Errors
If the URL doesn’t have a domain, returns the error InsertDomainSegmentError::UrlDoesNotHaveDomain.
If the segment isn’t found, returns the error InsertDomainSegmentError::SegmentNotFound.
If the call to Self::set_domain returns an error, that error is returned.
Source§impl BetterUrl
impl BetterUrl
Sourcepub fn subdomain_segment(&self, index: isize) -> Option<&str>
pub fn subdomain_segment(&self, index: isize) -> Option<&str>
§Examples
use better_url::*;
let url = BetterUrl::parse("https://abc.def.example.co.uk").unwrap();
assert_eq!(url.subdomain_segment(-3), None );
assert_eq!(url.subdomain_segment(-2), Some("abc".into()));
assert_eq!(url.subdomain_segment(-1), Some("def".into()));
assert_eq!(url.subdomain_segment( 0), Some("abc".into()));
assert_eq!(url.subdomain_segment( 1), Some("def".into()));
assert_eq!(url.subdomain_segment( 2), None );Sourcepub fn set_subdomain_segment(
&mut self,
index: isize,
value: Option<&str>,
) -> Result<(), SetSubdomainSegmentError>
pub fn set_subdomain_segment( &mut self, index: isize, value: Option<&str>, ) -> Result<(), SetSubdomainSegmentError>
Sets the specified subdomain segment.
§Errors
If the URL doesn’t have a domain, returns the error SetSubdomainSegmentError::UrlDoesNotHaveSubdomain.
If the segment isn’t found, returns the error SetSubdomainSegmentError::SegmentNotFound.
If the call to Self::set_subdomain returns an error, that error is returned.
§Examples
use better_url::*;
let mut url = BetterUrl::parse("https://abc.def.example.co.uk").unwrap();
url.set_subdomain_segment(-3, Some("n3")).unwrap_err(); assert_eq!(url.host_str(), Some("abc.def.example.co.uk"));
url.set_subdomain_segment(-2, Some("n2")).unwrap (); assert_eq!(url.host_str(), Some("n2.def.example.co.uk"));
url.set_subdomain_segment(-1, Some("n1")).unwrap (); assert_eq!(url.host_str(), Some("n2.n1.example.co.uk"));
url.set_subdomain_segment( 0, Some("p0")).unwrap (); assert_eq!(url.host_str(), Some("p0.n1.example.co.uk"));
url.set_subdomain_segment( 1, Some("p1")).unwrap (); assert_eq!(url.host_str(), Some("p0.p1.example.co.uk"));
url.set_subdomain_segment( 2, Some("p2")).unwrap_err(); assert_eq!(url.host_str(), Some("p0.p1.example.co.uk"));
url.set_subdomain_segment( 0, None).unwrap(); assert_eq!(url.host_str(), Some("p1.example.co.uk"));
url.set_subdomain_segment(-1, None).unwrap(); assert_eq!(url.host_str(), Some("example.co.uk"));Sourcepub fn insert_subdomain_segment(
&mut self,
index: isize,
value: &str,
) -> Result<(), InsertSubdomainSegmentError>
pub fn insert_subdomain_segment( &mut self, index: isize, value: &str, ) -> Result<(), InsertSubdomainSegmentError>
Inserts a new subdomain segment at the specified index.
§Errors
If the URL doesn’t have a domain, returns the error InsertSubdomainSegmentError::UrlDoesNotHaveSubdomain.
If the segment isn’t found, returns the error InsertSubdomainSegmentError::SegmentNotFound.
If the call to Self::set_subdomain returns an error, that error is returned.
Source§impl BetterUrl
impl BetterUrl
Sourcepub fn domain_suffix_segment(&self, index: isize) -> Option<&str>
pub fn domain_suffix_segment(&self, index: isize) -> Option<&str>
§Examples
use better_url::*;
let url = BetterUrl::parse("https://abc.def.example.co.uk").unwrap();
assert_eq!(url.domain_suffix_segment(-3), None );
assert_eq!(url.domain_suffix_segment(-2), Some("co".into()));
assert_eq!(url.domain_suffix_segment(-1), Some("uk".into()));
assert_eq!(url.domain_suffix_segment( 0), Some("co".into()));
assert_eq!(url.domain_suffix_segment( 1), Some("uk".into()));
assert_eq!(url.domain_suffix_segment( 2), None );Sourcepub fn set_domain_suffix_segment(
&mut self,
index: isize,
value: Option<&str>,
) -> Result<(), SetDomainSuffixSegmentError>
pub fn set_domain_suffix_segment( &mut self, index: isize, value: Option<&str>, ) -> Result<(), SetDomainSuffixSegmentError>
Sets the specified domain suffix segment.
§Errors
If the URL doesn’t have a domain, returns the error SetDomainSuffixSegmentError::UrlDoesNotHaveDomainSuffix.
If the segment isn’t found, returns the error SetDomainSuffixSegmentError::SegmentNotFound.
If the call to Self::set_domain_suffix returns an error, that error is returned.
§Examples
use better_url::*;
let mut url = BetterUrl::parse("https://abc.def.example.co.uk").unwrap();
url.set_domain_suffix_segment(-3, Some("n3")).unwrap_err(); assert_eq!(url.host_str(), Some("abc.def.example.co.uk"));
url.set_domain_suffix_segment(-2, Some("n2")).unwrap (); assert_eq!(url.host_str(), Some("abc.def.example.n2.uk"));
url.set_domain_suffix_segment(-1, Some("n1")).unwrap (); assert_eq!(url.host_str(), Some("abc.def.example.n2.n1"));
// Setting a domain suffix segment may alter the amount of domain segments the suffix has, leading to weird results.
url.set_domain_suffix_segment( 0, Some("p0")).unwrap (); assert_eq!(url.host_str(), Some("abc.def.example.n2.p0"));
url.set_domain_suffix_segment( 1, Some("p1")).unwrap_err(); assert_eq!(url.host_str(), Some("abc.def.example.n2.p0"));
url.set_domain_suffix_segment( 0, None).unwrap(); assert_eq!(url.host_str(), Some("abc.def.example.n2"));
url.set_domain_suffix_segment(-1, None).unwrap(); assert_eq!(url.host_str(), Some("abc.def.example"));Sourcepub fn insert_domain_suffix_segment(
&mut self,
index: isize,
value: &str,
) -> Result<(), InsertDomainSuffixSegmentError>
pub fn insert_domain_suffix_segment( &mut self, index: isize, value: &str, ) -> Result<(), InsertDomainSuffixSegmentError>
Inserts a new domain suffix segment at the specified index.
§Errors
If the URL doesn’t have a domain, returns the error InsertDomainSuffixSegmentError::UrlDoesNotHaveDomainSuffix.
If the segment isn’t found, returns the error InsertDomainSuffixSegmentError::SegmentNotFound.
If the call to Self::set_domain_suffix returns an error, that error is returned.
Source§impl BetterUrl
impl BetterUrl
Sourcepub fn domain(&self) -> Option<&str>
pub fn domain(&self) -> Option<&str>
The domain, not including any fully qualified domain name period.
Specifically, if Self::host_details is HostDetails::Domain, return the substring of Url::host_str specified by DomainDetails::domain_bounds.
§Examples
use better_url::*;
assert_eq!(BetterUrl::parse("https://example.com" ).unwrap().domain(), Some("example.com" ));
assert_eq!(BetterUrl::parse("https://example.co.uk" ).unwrap().domain(), Some("example.co.uk" ));
assert_eq!(BetterUrl::parse("https://www.example.com" ).unwrap().domain(), Some("www.example.com" ));
assert_eq!(BetterUrl::parse("https://www.example.co.uk" ).unwrap().domain(), Some("www.example.co.uk"));
assert_eq!(BetterUrl::parse("https://www.example.com." ).unwrap().domain(), Some("www.example.com" ));
assert_eq!(BetterUrl::parse("https://www.example.co.uk.").unwrap().domain(), Some("www.example.co.uk"));Sourcepub fn subdomain(&self) -> Option<&str>
pub fn subdomain(&self) -> Option<&str>
The subdomain.
Specifically, if Self::host_details is HostDetails::Domain, return the substring of Url::host_str specified by DomainDetails::subdomain_bounds.
§Examples
use better_url::*;
assert_eq!(BetterUrl::parse("https://example.com" ).unwrap().subdomain(), None);
assert_eq!(BetterUrl::parse("https://example.co.uk" ).unwrap().subdomain(), None);
assert_eq!(BetterUrl::parse("https://www.example.com" ).unwrap().subdomain(), Some("www"));
assert_eq!(BetterUrl::parse("https://www.example.co.uk" ).unwrap().subdomain(), Some("www"));
assert_eq!(BetterUrl::parse("https://www.example.com." ).unwrap().subdomain(), Some("www"));
assert_eq!(BetterUrl::parse("https://www.example.co.uk.").unwrap().subdomain(), Some("www"));Sourcepub fn not_domain_suffix(&self) -> Option<&str>
pub fn not_domain_suffix(&self) -> Option<&str>
The “not domain suffix”, being Self::subdomain and Self::domain_middle.
Specifically, if Self::host_details is HostDetails::Domain, return the substring of Url::host_str specified by DomainDetails::not_domain_suffix_bounds.
§Examples
use better_url::*;
assert_eq!(BetterUrl::parse("https://example.com" ).unwrap().not_domain_suffix(), Some("example"));
assert_eq!(BetterUrl::parse("https://example.co.uk" ).unwrap().not_domain_suffix(), Some("example"));
assert_eq!(BetterUrl::parse("https://www.example.com" ).unwrap().not_domain_suffix(), Some("www.example"));
assert_eq!(BetterUrl::parse("https://www.example.co.uk" ).unwrap().not_domain_suffix(), Some("www.example"));
assert_eq!(BetterUrl::parse("https://www.example.com." ).unwrap().not_domain_suffix(), Some("www.example"));
assert_eq!(BetterUrl::parse("https://www.example.co.uk.").unwrap().not_domain_suffix(), Some("www.example"));Sourcepub fn domain_middle(&self) -> Option<&str>
pub fn domain_middle(&self) -> Option<&str>
The domain middle.
Specifically, if Self::host_details is HostDetails::Domain, return the substring of Url::host_str specified by DomainDetails::domain_middle_bounds.
§Examples
use better_url::*;
assert_eq!(BetterUrl::parse("https://example.com" ).unwrap().domain_middle(), Some("example"));
assert_eq!(BetterUrl::parse("https://example.co.uk" ).unwrap().domain_middle(), Some("example"));
assert_eq!(BetterUrl::parse("https://www.example.com" ).unwrap().domain_middle(), Some("example"));
assert_eq!(BetterUrl::parse("https://www.example.co.uk" ).unwrap().domain_middle(), Some("example"));
assert_eq!(BetterUrl::parse("https://www.example.com." ).unwrap().domain_middle(), Some("example"));
assert_eq!(BetterUrl::parse("https://www.example.co.uk.").unwrap().domain_middle(), Some("example"));Sourcepub fn reg_domain(&self) -> Option<&str>
pub fn reg_domain(&self) -> Option<&str>
The “registerable domain”, being the Self::domain_middle and Self::domain_suffix.
Specifically, if Self::host_details is HostDetails::Domain, return the substring of Url::host_str specified by DomainDetails::reg_domain_bounds.
§Examples
use better_url::*;
assert_eq!(BetterUrl::parse("https://example.com" ).unwrap().reg_domain(), Some("example.com" ));
assert_eq!(BetterUrl::parse("https://example.co.uk" ).unwrap().reg_domain(), Some("example.co.uk"));
assert_eq!(BetterUrl::parse("https://www.example.com" ).unwrap().reg_domain(), Some("example.com" ));
assert_eq!(BetterUrl::parse("https://www.example.co.uk" ).unwrap().reg_domain(), Some("example.co.uk"));
assert_eq!(BetterUrl::parse("https://www.example.com." ).unwrap().reg_domain(), Some("example.com" ));
assert_eq!(BetterUrl::parse("https://www.example.co.uk.").unwrap().reg_domain(), Some("example.co.uk"));Sourcepub fn domain_suffix(&self) -> Option<&str>
pub fn domain_suffix(&self) -> Option<&str>
The domain suffix, as defined by the public suffix list, not including any fully qualified domain name period.
Specifically, if Self::host_details is HostDetails::Domain, return the substring of Url::host_str specified by DomainDetails::domain_suffix_bounds.
§Examples
use better_url::*;
assert_eq!(BetterUrl::parse("https://example.com" ).unwrap().domain_suffix(), Some("com" ));
assert_eq!(BetterUrl::parse("https://example.co.uk" ).unwrap().domain_suffix(), Some("co.uk"));
assert_eq!(BetterUrl::parse("https://www.example.com" ).unwrap().domain_suffix(), Some("com" ));
assert_eq!(BetterUrl::parse("https://www.example.co.uk" ).unwrap().domain_suffix(), Some("co.uk"));
assert_eq!(BetterUrl::parse("https://www.example.com." ).unwrap().domain_suffix(), Some("com" ));
assert_eq!(BetterUrl::parse("https://www.example.co.uk.").unwrap().domain_suffix(), Some("co.uk"));Sourcepub fn set_domain(&mut self, to: Option<&str>) -> Result<(), SetDomainError>
pub fn set_domain(&mut self, to: Option<&str>) -> Result<(), SetDomainError>
Sets the domain.
If this function returns Ok, then Self::domain returns the value passed into it.
§Errors
If the URL doesn’t have a domain host, to isn’t a domain, and/or to is a fully qualified domain, returns the error SetDomainError::Other.
§Examples
use better_url::*;
let mut url = BetterUrl::parse( "https://example.com" ).unwrap(); url.set_domain(Some("example.com")).unwrap(); assert_eq!(url.host_str(), Some("example.com" ));
let mut url = BetterUrl::parse("https://www.example.com" ).unwrap(); url.set_domain(Some("example.com")).unwrap(); assert_eq!(url.host_str(), Some("example.com" ));
let mut url = BetterUrl::parse( "https://example.co.uk" ).unwrap(); url.set_domain(Some("example.com")).unwrap(); assert_eq!(url.host_str(), Some("example.com" ));
let mut url = BetterUrl::parse("https://www.example.co.uk" ).unwrap(); url.set_domain(Some("example.com")).unwrap(); assert_eq!(url.host_str(), Some("example.com" ));
let mut url = BetterUrl::parse( "https://example.com." ).unwrap(); url.set_domain(Some("example.com")).unwrap(); assert_eq!(url.host_str(), Some("example.com."));
let mut url = BetterUrl::parse("https://www.example.com." ).unwrap(); url.set_domain(Some("example.com")).unwrap(); assert_eq!(url.host_str(), Some("example.com."));
let mut url = BetterUrl::parse( "https://example.co.uk.").unwrap(); url.set_domain(Some("example.com")).unwrap(); assert_eq!(url.host_str(), Some("example.com."));
let mut url = BetterUrl::parse("https://www.example.co.uk.").unwrap(); url.set_domain(Some("example.com")).unwrap(); assert_eq!(url.host_str(), Some("example.com."));Sourcepub fn set_subdomain(
&mut self,
to: Option<&str>,
) -> Result<(), SetSubdomainError>
pub fn set_subdomain( &mut self, to: Option<&str>, ) -> Result<(), SetSubdomainError>
Sets the subdomain.
If this function returns Ok, then Self::subdomain returns the value passed into it.
§Errors
If Self’s host isn’t a domain, returns the error SetSubdomainError::HostIsNotADomain.
If Self doesn’t have a reg domain, returns the error SetSubdomainError::MissingRegDomain.
§Examples
use better_url::*;
let mut url = BetterUrl::parse( "https://example.com" ).unwrap(); url.set_subdomain(None ).unwrap(); assert_eq!(url.host_str(), Some( "example.com" )); assert_eq!(url.host_details(), Some(&HostDetails::Domain(DomainDetails {middle_start: Some(0), suffix_start: Some(8 ), fqdn_period: None})));
let mut url = BetterUrl::parse( "https://example.com" ).unwrap(); url.set_subdomain(Some("www")).unwrap(); assert_eq!(url.host_str(), Some("www.example.com" )); assert_eq!(url.host_details(), Some(&HostDetails::Domain(DomainDetails {middle_start: Some(4), suffix_start: Some(12), fqdn_period: None})));
let mut url = BetterUrl::parse("https://www.example.com" ).unwrap(); url.set_subdomain(None ).unwrap(); assert_eq!(url.host_str(), Some( "example.com" )); assert_eq!(url.host_details(), Some(&HostDetails::Domain(DomainDetails {middle_start: Some(0), suffix_start: Some(8 ), fqdn_period: None})));
let mut url = BetterUrl::parse("https://www.example.com" ).unwrap(); url.set_subdomain(Some("www")).unwrap(); assert_eq!(url.host_str(), Some("www.example.com" )); assert_eq!(url.host_details(), Some(&HostDetails::Domain(DomainDetails {middle_start: Some(4), suffix_start: Some(12), fqdn_period: None})));
let mut url = BetterUrl::parse( "https://example.co.uk" ).unwrap(); url.set_subdomain(None ).unwrap(); assert_eq!(url.host_str(), Some( "example.co.uk" )); assert_eq!(url.host_details(), Some(&HostDetails::Domain(DomainDetails {middle_start: Some(0), suffix_start: Some(8 ), fqdn_period: None})));
let mut url = BetterUrl::parse( "https://example.co.uk" ).unwrap(); url.set_subdomain(Some("www")).unwrap(); assert_eq!(url.host_str(), Some("www.example.co.uk" )); assert_eq!(url.host_details(), Some(&HostDetails::Domain(DomainDetails {middle_start: Some(4), suffix_start: Some(12), fqdn_period: None})));
let mut url = BetterUrl::parse("https://www.example.co.uk" ).unwrap(); url.set_subdomain(None ).unwrap(); assert_eq!(url.host_str(), Some( "example.co.uk" )); assert_eq!(url.host_details(), Some(&HostDetails::Domain(DomainDetails {middle_start: Some(0), suffix_start: Some(8 ), fqdn_period: None})));
let mut url = BetterUrl::parse("https://www.example.co.uk" ).unwrap(); url.set_subdomain(Some("www")).unwrap(); assert_eq!(url.host_str(), Some("www.example.co.uk" )); assert_eq!(url.host_details(), Some(&HostDetails::Domain(DomainDetails {middle_start: Some(4), suffix_start: Some(12), fqdn_period: None})));
let mut url = BetterUrl::parse( "https://example.com." ).unwrap(); url.set_subdomain(None ).unwrap(); assert_eq!(url.host_str(), Some( "example.com." )); assert_eq!(url.host_details(), Some(&HostDetails::Domain(DomainDetails {middle_start: Some(0), suffix_start: Some(8 ), fqdn_period: Some(11)})));
let mut url = BetterUrl::parse( "https://example.com." ).unwrap(); url.set_subdomain(Some("www")).unwrap(); assert_eq!(url.host_str(), Some("www.example.com." )); assert_eq!(url.host_details(), Some(&HostDetails::Domain(DomainDetails {middle_start: Some(4), suffix_start: Some(12), fqdn_period: Some(15)})));
let mut url = BetterUrl::parse("https://www.example.com." ).unwrap(); url.set_subdomain(None ).unwrap(); assert_eq!(url.host_str(), Some( "example.com." )); assert_eq!(url.host_details(), Some(&HostDetails::Domain(DomainDetails {middle_start: Some(0), suffix_start: Some(8 ), fqdn_period: Some(11)})));
let mut url = BetterUrl::parse("https://www.example.com." ).unwrap(); url.set_subdomain(Some("www")).unwrap(); assert_eq!(url.host_str(), Some("www.example.com." )); assert_eq!(url.host_details(), Some(&HostDetails::Domain(DomainDetails {middle_start: Some(4), suffix_start: Some(12), fqdn_period: Some(15)})));
let mut url = BetterUrl::parse( "https://example.co.uk.").unwrap(); url.set_subdomain(None ).unwrap(); assert_eq!(url.host_str(), Some( "example.co.uk.")); assert_eq!(url.host_details(), Some(&HostDetails::Domain(DomainDetails {middle_start: Some(0), suffix_start: Some(8 ), fqdn_period: Some(13)})));
let mut url = BetterUrl::parse( "https://example.co.uk.").unwrap(); url.set_subdomain(Some("www")).unwrap(); assert_eq!(url.host_str(), Some("www.example.co.uk.")); assert_eq!(url.host_details(), Some(&HostDetails::Domain(DomainDetails {middle_start: Some(4), suffix_start: Some(12), fqdn_period: Some(17)})));
let mut url = BetterUrl::parse("https://www.example.co.uk.").unwrap(); url.set_subdomain(None ).unwrap(); assert_eq!(url.host_str(), Some( "example.co.uk.")); assert_eq!(url.host_details(), Some(&HostDetails::Domain(DomainDetails {middle_start: Some(0), suffix_start: Some(8 ), fqdn_period: Some(13)})));
let mut url = BetterUrl::parse("https://www.example.co.uk.").unwrap(); url.set_subdomain(Some("www")).unwrap(); assert_eq!(url.host_str(), Some("www.example.co.uk.")); assert_eq!(url.host_details(), Some(&HostDetails::Domain(DomainDetails {middle_start: Some(4), suffix_start: Some(12), fqdn_period: Some(17)})));Sourcepub fn set_not_domain_suffix(
&mut self,
to: Option<&str>,
) -> Result<(), SetNotDomainSuffixError>
pub fn set_not_domain_suffix( &mut self, to: Option<&str>, ) -> Result<(), SetNotDomainSuffixError>
Sets the “not domain suffix”.
If this function returns Ok, then Self::not_domain_suffix returns the value passed into it.
§Errors
If Self’s host isn’t a domain, returns the error SetSubdomainError::HostIsNotADomain.
If Self doesn’t have a domain suffix, returns the error SetNotDomainSuffixError::MissingDomainSuffix.
§Examples
use better_url::*;
let mut url = BetterUrl::parse( "https://example.com" ).unwrap(); url.set_not_domain_suffix(None ).unwrap(); assert_eq!(url.host_str(), Some( "com" ));
let mut url = BetterUrl::parse( "https://example.com" ).unwrap(); url.set_not_domain_suffix(Some( "example")).unwrap(); assert_eq!(url.host_str(), Some( "example.com" ));
let mut url = BetterUrl::parse( "https://example.com" ).unwrap(); url.set_not_domain_suffix(Some("www.example")).unwrap(); assert_eq!(url.host_str(), Some("www.example.com" ));
let mut url = BetterUrl::parse("https://www.example.com" ).unwrap(); url.set_not_domain_suffix(None ).unwrap(); assert_eq!(url.host_str(), Some( "com" ));
let mut url = BetterUrl::parse("https://www.example.com" ).unwrap(); url.set_not_domain_suffix(Some( "example")).unwrap(); assert_eq!(url.host_str(), Some( "example.com" ));
let mut url = BetterUrl::parse("https://www.example.com" ).unwrap(); url.set_not_domain_suffix(Some("www.example")).unwrap(); assert_eq!(url.host_str(), Some("www.example.com" ));
let mut url = BetterUrl::parse( "https://example.co.uk" ).unwrap(); url.set_not_domain_suffix(None ).unwrap(); assert_eq!(url.host_str(), Some( "co.uk" ));
let mut url = BetterUrl::parse( "https://example.co.uk" ).unwrap(); url.set_not_domain_suffix(Some( "example")).unwrap(); assert_eq!(url.host_str(), Some( "example.co.uk" ));
let mut url = BetterUrl::parse( "https://example.co.uk" ).unwrap(); url.set_not_domain_suffix(Some("www.example")).unwrap(); assert_eq!(url.host_str(), Some("www.example.co.uk" ));
let mut url = BetterUrl::parse("https://www.example.co.uk" ).unwrap(); url.set_not_domain_suffix(None ).unwrap(); assert_eq!(url.host_str(), Some( "co.uk" ));
let mut url = BetterUrl::parse("https://www.example.co.uk" ).unwrap(); url.set_not_domain_suffix(Some( "example")).unwrap(); assert_eq!(url.host_str(), Some( "example.co.uk" ));
let mut url = BetterUrl::parse("https://www.example.co.uk" ).unwrap(); url.set_not_domain_suffix(Some("www.example")).unwrap(); assert_eq!(url.host_str(), Some("www.example.co.uk" ));
let mut url = BetterUrl::parse( "https://example.com." ).unwrap(); url.set_not_domain_suffix(None ).unwrap(); assert_eq!(url.host_str(), Some( "com." ));
let mut url = BetterUrl::parse( "https://example.com." ).unwrap(); url.set_not_domain_suffix(Some( "example")).unwrap(); assert_eq!(url.host_str(), Some( "example.com." ));
let mut url = BetterUrl::parse( "https://example.com." ).unwrap(); url.set_not_domain_suffix(Some("www.example")).unwrap(); assert_eq!(url.host_str(), Some("www.example.com." ));
let mut url = BetterUrl::parse("https://www.example.com." ).unwrap(); url.set_not_domain_suffix(None ).unwrap(); assert_eq!(url.host_str(), Some( "com." ));
let mut url = BetterUrl::parse("https://www.example.com." ).unwrap(); url.set_not_domain_suffix(Some( "example")).unwrap(); assert_eq!(url.host_str(), Some( "example.com." ));
let mut url = BetterUrl::parse("https://www.example.com." ).unwrap(); url.set_not_domain_suffix(Some("www.example")).unwrap(); assert_eq!(url.host_str(), Some("www.example.com." ));
let mut url = BetterUrl::parse( "https://example.co.uk.").unwrap(); url.set_not_domain_suffix(None ).unwrap(); assert_eq!(url.host_str(), Some( "co.uk."));
let mut url = BetterUrl::parse( "https://example.co.uk.").unwrap(); url.set_not_domain_suffix(Some( "example")).unwrap(); assert_eq!(url.host_str(), Some( "example.co.uk."));
let mut url = BetterUrl::parse( "https://example.co.uk.").unwrap(); url.set_not_domain_suffix(Some("www.example")).unwrap(); assert_eq!(url.host_str(), Some("www.example.co.uk."));
let mut url = BetterUrl::parse("https://www.example.co.uk.").unwrap(); url.set_not_domain_suffix(None ).unwrap(); assert_eq!(url.host_str(), Some( "co.uk."));
let mut url = BetterUrl::parse("https://www.example.co.uk.").unwrap(); url.set_not_domain_suffix(Some( "example")).unwrap(); assert_eq!(url.host_str(), Some( "example.co.uk."));
let mut url = BetterUrl::parse("https://www.example.co.uk.").unwrap(); url.set_not_domain_suffix(Some("www.example")).unwrap(); assert_eq!(url.host_str(), Some("www.example.co.uk."));Sourcepub fn set_domain_middle(
&mut self,
to: Option<&str>,
) -> Result<(), SetDomainMiddleError>
pub fn set_domain_middle( &mut self, to: Option<&str>, ) -> Result<(), SetDomainMiddleError>
Sets the domain middle.
If this function returns Ok, then Self::domain_middle returns the value passed into it.
§Errors
If Self’s host isn’t a domain, returns the error SetSubdomainError::HostIsNotADomain.
If Self doesn’t have a domain suffix, returns the error SetDomainMiddleError::MissingDomainSuffix.
§Examples
use better_url::*;
let mut url = BetterUrl::parse( "https://example.com" ).unwrap(); url.set_domain_middle(None ).unwrap(); assert_eq!(url.host_str(), Some( "com" ));
let mut url = BetterUrl::parse( "https://example.com" ).unwrap(); url.set_domain_middle(Some("example2")).unwrap(); assert_eq!(url.host_str(), Some( "example2.com" ));
let mut url = BetterUrl::parse("https://www.example.com" ).unwrap(); url.set_domain_middle(None ).unwrap(); assert_eq!(url.host_str(), Some( "www.com" ));
let mut url = BetterUrl::parse("https://www.example.com" ).unwrap(); url.set_domain_middle(Some("example2")).unwrap(); assert_eq!(url.host_str(), Some("www.example2.com" ));
let mut url = BetterUrl::parse( "https://example.co.uk" ).unwrap(); url.set_domain_middle(None ).unwrap(); assert_eq!(url.host_str(), Some( "co.uk" ));
let mut url = BetterUrl::parse( "https://example.co.uk" ).unwrap(); url.set_domain_middle(Some("example2")).unwrap(); assert_eq!(url.host_str(), Some( "example2.co.uk" ));
let mut url = BetterUrl::parse("https://www.example.co.uk" ).unwrap(); url.set_domain_middle(None ).unwrap(); assert_eq!(url.host_str(), Some( "www.co.uk" ));
let mut url = BetterUrl::parse("https://www.example.co.uk" ).unwrap(); url.set_domain_middle(Some("example2")).unwrap(); assert_eq!(url.host_str(), Some("www.example2.co.uk" ));
let mut url = BetterUrl::parse( "https://example.com." ).unwrap(); url.set_domain_middle(None ).unwrap(); assert_eq!(url.host_str(), Some( "com." ));
let mut url = BetterUrl::parse( "https://example.com." ).unwrap(); url.set_domain_middle(Some("example2")).unwrap(); assert_eq!(url.host_str(), Some( "example2.com." ));
let mut url = BetterUrl::parse("https://www.example.com." ).unwrap(); url.set_domain_middle(None ).unwrap(); assert_eq!(url.host_str(), Some( "www.com." ));
let mut url = BetterUrl::parse("https://www.example.com." ).unwrap(); url.set_domain_middle(Some("example2")).unwrap(); assert_eq!(url.host_str(), Some("www.example2.com." ));
let mut url = BetterUrl::parse( "https://example.co.uk.").unwrap(); url.set_domain_middle(None ).unwrap(); assert_eq!(url.host_str(), Some( "co.uk."));
let mut url = BetterUrl::parse( "https://example.co.uk.").unwrap(); url.set_domain_middle(Some("example2")).unwrap(); assert_eq!(url.host_str(), Some( "example2.co.uk."));
let mut url = BetterUrl::parse("https://www.example.co.uk.").unwrap(); url.set_domain_middle(None ).unwrap(); assert_eq!(url.host_str(), Some( "www.co.uk."));
let mut url = BetterUrl::parse("https://www.example.co.uk.").unwrap(); url.set_domain_middle(Some("example2")).unwrap(); assert_eq!(url.host_str(), Some("www.example2.co.uk."));Sourcepub fn set_reg_domain(
&mut self,
to: Option<&str>,
) -> Result<(), SetRegDomainError>
pub fn set_reg_domain( &mut self, to: Option<&str>, ) -> Result<(), SetRegDomainError>
Sets the registerable domain.
If this function returns Ok, then Self::reg_domain returns the value passed into it.
§Errors
If Self’s host isn’t a domain, returns the error SetRegDomainError::HostIsNotADomain.
§Examples
use better_url::*;
let mut url = BetterUrl::parse( "https://example.com" ).unwrap(); url.set_reg_domain(None ).unwrap_err();
let mut url = BetterUrl::parse( "https://example.com" ).unwrap(); url.set_reg_domain(Some( "com" )).unwrap(); assert_eq!(url.host_str(), Some( "com" ));
let mut url = BetterUrl::parse( "https://example.com" ).unwrap(); url.set_reg_domain(Some("example.com" )).unwrap(); assert_eq!(url.host_str(), Some( "example.com" ));
let mut url = BetterUrl::parse( "https://example.com" ).unwrap(); url.set_reg_domain(Some( "co.uk")).unwrap(); assert_eq!(url.host_str(), Some( "co.uk" ));
let mut url = BetterUrl::parse( "https://example.com" ).unwrap(); url.set_reg_domain(Some("example.co.uk")).unwrap(); assert_eq!(url.host_str(), Some( "example.co.uk" ));
let mut url = BetterUrl::parse("https://www.example.com" ).unwrap(); url.set_reg_domain(None ).unwrap(); assert_eq!(url.host_str(), Some( "www" ));
let mut url = BetterUrl::parse("https://www.example.com" ).unwrap(); url.set_reg_domain(Some( "com" )).unwrap(); assert_eq!(url.host_str(), Some( "www.com" ));
let mut url = BetterUrl::parse("https://www.example.com" ).unwrap(); url.set_reg_domain(Some("example.com" )).unwrap(); assert_eq!(url.host_str(), Some("www.example.com" ));
let mut url = BetterUrl::parse("https://www.example.com" ).unwrap(); url.set_reg_domain(Some( "co.uk")).unwrap(); assert_eq!(url.host_str(), Some( "www.co.uk" ));
let mut url = BetterUrl::parse("https://www.example.com" ).unwrap(); url.set_reg_domain(Some("example.co.uk")).unwrap(); assert_eq!(url.host_str(), Some("www.example.co.uk" ));
let mut url = BetterUrl::parse( "https://example.co.uk" ).unwrap(); url.set_reg_domain(None ).unwrap_err();
let mut url = BetterUrl::parse( "https://example.co.uk" ).unwrap(); url.set_reg_domain(Some( "com" )).unwrap(); assert_eq!(url.host_str(), Some( "com" ));
let mut url = BetterUrl::parse( "https://example.co.uk" ).unwrap(); url.set_reg_domain(Some("example.com" )).unwrap(); assert_eq!(url.host_str(), Some( "example.com" ));
let mut url = BetterUrl::parse( "https://example.co.uk" ).unwrap(); url.set_reg_domain(Some( "co.uk")).unwrap(); assert_eq!(url.host_str(), Some( "co.uk" ));
let mut url = BetterUrl::parse( "https://example.co.uk" ).unwrap(); url.set_reg_domain(Some("example.co.uk")).unwrap(); assert_eq!(url.host_str(), Some( "example.co.uk" ));
let mut url = BetterUrl::parse("https://www.example.co.uk" ).unwrap(); url.set_reg_domain(None ).unwrap(); assert_eq!(url.host_str(), Some( "www" ));
let mut url = BetterUrl::parse("https://www.example.co.uk" ).unwrap(); url.set_reg_domain(Some( "com" )).unwrap(); assert_eq!(url.host_str(), Some( "www.com" ));
let mut url = BetterUrl::parse("https://www.example.co.uk" ).unwrap(); url.set_reg_domain(Some("example.com" )).unwrap(); assert_eq!(url.host_str(), Some("www.example.com" ));
let mut url = BetterUrl::parse("https://www.example.co.uk" ).unwrap(); url.set_reg_domain(Some( "co.uk")).unwrap(); assert_eq!(url.host_str(), Some( "www.co.uk" ));
let mut url = BetterUrl::parse("https://www.example.co.uk" ).unwrap(); url.set_reg_domain(Some("example.co.uk")).unwrap(); assert_eq!(url.host_str(), Some("www.example.co.uk" ));
let mut url = BetterUrl::parse( "https://example.com." ).unwrap(); url.set_reg_domain(None ).unwrap(); assert_eq!(url.host_str(), Some( "." ));
let mut url = BetterUrl::parse( "https://example.com." ).unwrap(); url.set_reg_domain(Some( "com" )).unwrap(); assert_eq!(url.host_str(), Some( "com." ));
let mut url = BetterUrl::parse( "https://example.com." ).unwrap(); url.set_reg_domain(Some("example.com" )).unwrap(); assert_eq!(url.host_str(), Some( "example.com." ));
let mut url = BetterUrl::parse( "https://example.com." ).unwrap(); url.set_reg_domain(Some( "co.uk")).unwrap(); assert_eq!(url.host_str(), Some( "co.uk."));
let mut url = BetterUrl::parse( "https://example.com." ).unwrap(); url.set_reg_domain(Some("example.co.uk")).unwrap(); assert_eq!(url.host_str(), Some( "example.co.uk."));
let mut url = BetterUrl::parse("https://www.example.com." ).unwrap(); url.set_reg_domain(None ).unwrap(); assert_eq!(url.host_str(), Some( "www." ));
let mut url = BetterUrl::parse("https://www.example.com." ).unwrap(); url.set_reg_domain(Some( "com" )).unwrap(); assert_eq!(url.host_str(), Some( "www.com." ));
let mut url = BetterUrl::parse("https://www.example.com." ).unwrap(); url.set_reg_domain(Some("example.com" )).unwrap(); assert_eq!(url.host_str(), Some("www.example.com." ));
let mut url = BetterUrl::parse("https://www.example.com." ).unwrap(); url.set_reg_domain(Some( "co.uk")).unwrap(); assert_eq!(url.host_str(), Some( "www.co.uk."));
let mut url = BetterUrl::parse("https://www.example.com." ).unwrap(); url.set_reg_domain(Some("example.co.uk")).unwrap(); assert_eq!(url.host_str(), Some("www.example.co.uk."));
let mut url = BetterUrl::parse( "https://example.co.uk.").unwrap(); url.set_reg_domain(None ).unwrap(); assert_eq!(url.host_str(), Some( "." ));
let mut url = BetterUrl::parse( "https://example.co.uk.").unwrap(); url.set_reg_domain(Some( "com" )).unwrap(); assert_eq!(url.host_str(), Some( "com." ));
let mut url = BetterUrl::parse( "https://example.co.uk.").unwrap(); url.set_reg_domain(Some("example.com" )).unwrap(); assert_eq!(url.host_str(), Some( "example.com." ));
let mut url = BetterUrl::parse( "https://example.co.uk.").unwrap(); url.set_reg_domain(Some( "co.uk")).unwrap(); assert_eq!(url.host_str(), Some( "co.uk."));
let mut url = BetterUrl::parse( "https://example.co.uk.").unwrap(); url.set_reg_domain(Some("example.co.uk")).unwrap(); assert_eq!(url.host_str(), Some( "example.co.uk."));
let mut url = BetterUrl::parse("https://www.example.co.uk.").unwrap(); url.set_reg_domain(None ).unwrap(); assert_eq!(url.host_str(), Some( "www." ));
let mut url = BetterUrl::parse("https://www.example.co.uk.").unwrap(); url.set_reg_domain(Some( "com" )).unwrap(); assert_eq!(url.host_str(), Some( "www.com." ));
let mut url = BetterUrl::parse("https://www.example.co.uk.").unwrap(); url.set_reg_domain(Some("example.com" )).unwrap(); assert_eq!(url.host_str(), Some("www.example.com." ));
let mut url = BetterUrl::parse("https://www.example.co.uk.").unwrap(); url.set_reg_domain(Some( "co.uk")).unwrap(); assert_eq!(url.host_str(), Some( "www.co.uk."));
let mut url = BetterUrl::parse("https://www.example.co.uk.").unwrap(); url.set_reg_domain(Some("example.co.uk")).unwrap(); assert_eq!(url.host_str(), Some("www.example.co.uk."));Sourcepub fn set_domain_suffix(
&mut self,
to: Option<&str>,
) -> Result<(), SetDomainSuffixError>
pub fn set_domain_suffix( &mut self, to: Option<&str>, ) -> Result<(), SetDomainSuffixError>
Sets the domain suffix.
If this function returns Ok, then Self::domain_suffix returns the value passed into it.
§Errors
If Self’s host isn’t a domain, returns the error SetDomainSuffixError::HostIsNotADomain.
§Examples
use better_url::*;
let mut url = BetterUrl::parse( "https://example.com" ).unwrap(); url.set_domain_suffix(None ).unwrap(); assert_eq!(url.host_str(), Some( "example" ));
let mut url = BetterUrl::parse( "https://example.com" ).unwrap(); url.set_domain_suffix(Some("com" )).unwrap(); assert_eq!(url.host_str(), Some( "example.com" ));
let mut url = BetterUrl::parse( "https://example.com" ).unwrap(); url.set_domain_suffix(Some("co.uk")).unwrap(); assert_eq!(url.host_str(), Some( "example.co.uk" ));
let mut url = BetterUrl::parse("https://www.example.com" ).unwrap(); url.set_domain_suffix(None ).unwrap(); assert_eq!(url.host_str(), Some("www.example" ));
let mut url = BetterUrl::parse("https://www.example.com" ).unwrap(); url.set_domain_suffix(Some("com" )).unwrap(); assert_eq!(url.host_str(), Some("www.example.com" ));
let mut url = BetterUrl::parse("https://www.example.com" ).unwrap(); url.set_domain_suffix(Some("co.uk")).unwrap(); assert_eq!(url.host_str(), Some("www.example.co.uk" ));
let mut url = BetterUrl::parse( "https://example.co.uk" ).unwrap(); url.set_domain_suffix(None ).unwrap(); assert_eq!(url.host_str(), Some( "example" ));
let mut url = BetterUrl::parse( "https://example.co.uk" ).unwrap(); url.set_domain_suffix(Some("com" )).unwrap(); assert_eq!(url.host_str(), Some( "example.com" ));
let mut url = BetterUrl::parse( "https://example.co.uk" ).unwrap(); url.set_domain_suffix(Some("co.uk")).unwrap(); assert_eq!(url.host_str(), Some( "example.co.uk" ));
let mut url = BetterUrl::parse("https://www.example.co.uk" ).unwrap(); url.set_domain_suffix(None ).unwrap(); assert_eq!(url.host_str(), Some("www.example" ));
let mut url = BetterUrl::parse("https://www.example.co.uk" ).unwrap(); url.set_domain_suffix(Some("com" )).unwrap(); assert_eq!(url.host_str(), Some("www.example.com" ));
let mut url = BetterUrl::parse("https://www.example.co.uk" ).unwrap(); url.set_domain_suffix(Some("co.uk")).unwrap(); assert_eq!(url.host_str(), Some("www.example.co.uk" ));
let mut url = BetterUrl::parse( "https://example.com." ).unwrap(); url.set_domain_suffix(None ).unwrap(); assert_eq!(url.host_str(), Some( "example." ));
let mut url = BetterUrl::parse( "https://example.com." ).unwrap(); url.set_domain_suffix(Some("com" )).unwrap(); assert_eq!(url.host_str(), Some( "example.com." ));
let mut url = BetterUrl::parse( "https://example.com." ).unwrap(); url.set_domain_suffix(Some("co.uk")).unwrap(); assert_eq!(url.host_str(), Some( "example.co.uk."));
let mut url = BetterUrl::parse("https://www.example.com." ).unwrap(); url.set_domain_suffix(None ).unwrap(); assert_eq!(url.host_str(), Some("www.example." ));
let mut url = BetterUrl::parse("https://www.example.com." ).unwrap(); url.set_domain_suffix(Some("com" )).unwrap(); assert_eq!(url.host_str(), Some("www.example.com." ));
let mut url = BetterUrl::parse("https://www.example.com." ).unwrap(); url.set_domain_suffix(Some("co.uk")).unwrap(); assert_eq!(url.host_str(), Some("www.example.co.uk."));
let mut url = BetterUrl::parse( "https://example.co.uk.").unwrap(); url.set_domain_suffix(None ).unwrap(); assert_eq!(url.host_str(), Some( "example." ));
let mut url = BetterUrl::parse( "https://example.co.uk.").unwrap(); url.set_domain_suffix(Some("com" )).unwrap(); assert_eq!(url.host_str(), Some( "example.com." ));
let mut url = BetterUrl::parse( "https://example.co.uk.").unwrap(); url.set_domain_suffix(Some("co.uk")).unwrap(); assert_eq!(url.host_str(), Some( "example.co.uk."));
let mut url = BetterUrl::parse("https://www.example.co.uk.").unwrap(); url.set_domain_suffix(None ).unwrap(); assert_eq!(url.host_str(), Some("www.example." ));
let mut url = BetterUrl::parse("https://www.example.co.uk.").unwrap(); url.set_domain_suffix(Some("com" )).unwrap(); assert_eq!(url.host_str(), Some("www.example.com." ));
let mut url = BetterUrl::parse("https://www.example.co.uk.").unwrap(); url.set_domain_suffix(Some("co.uk")).unwrap(); assert_eq!(url.host_str(), Some("www.example.co.uk."));Sourcepub fn set_fqdn(&mut self, to: bool) -> Result<(), SetFqdnError>
pub fn set_fqdn(&mut self, to: bool) -> Result<(), SetFqdnError>
Sets the fully qualified domain name period.
§Errors
If self doesn’t have a host, returns the error SetFqdnError::NoHost.
If the host isn’t a domain, returns the error SetFqdnError::HostIsNotADomain.
§Examples
use better_url::*;
let mut url = BetterUrl::parse("https://example.com").unwrap();
url.set_fqdn(false).unwrap();
assert_eq!(url.host_str(), Some("example.com"));
url.set_fqdn(true).unwrap();
assert_eq!(url.host_str(), Some("example.com."));
url.set_fqdn(true).unwrap();
assert_eq!(url.host_str(), Some("example.com."));
url.set_fqdn(false).unwrap();
assert_eq!(url.host_str(), Some("example.com"));Source§impl BetterUrl
impl BetterUrl
Sourcepub fn query_pairs_mut(&mut self) -> Serializer<'_, UrlQuery<'_>>
pub fn query_pairs_mut(&mut self) -> Serializer<'_, UrlQuery<'_>>
Sourcepub fn raw_query_pairs(
&self,
) -> Option<impl Iterator<Item = (&str, Option<&str>)>>
pub fn raw_query_pairs( &self, ) -> Option<impl Iterator<Item = (&str, Option<&str>)>>
An iterator over query parameters without percent decoding anything.
§Examples
use better_url::*;
let url = BetterUrl::parse("https://example.com?a=1&%61=2&a=3&b=%41&%62=%42&b=%43").unwrap();
let mut raw_query_pairs = url.raw_query_pairs().unwrap();
assert_eq!(raw_query_pairs.next(), Some(("a" , Some("1"))));
assert_eq!(raw_query_pairs.next(), Some(("%61", Some("2"))));
assert_eq!(raw_query_pairs.next(), Some(("a" , Some("3"))));
assert_eq!(raw_query_pairs.next(), Some(("b" , Some("%41"))));
assert_eq!(raw_query_pairs.next(), Some(("%62", Some("%42"))));
assert_eq!(raw_query_pairs.next(), Some(("b" , Some("%43"))));Sourcepub fn raw_query_param<'a>(
&'a self,
name: &str,
index: usize,
) -> Option<Option<Option<&'a str>>>
pub fn raw_query_param<'a>( &'a self, name: &str, index: usize, ) -> Option<Option<Option<&'a str>>>
Get the selected query parameter without percent decoding the value.
For matching, the names are percent decoded. So a %61=a query parameter is selectable with a name of a.
§Examples
use better_url::*;
let url = BetterUrl::parse("https://example.com?a=1&%61=2&a=3&b=%41&%62=%42&b=%43").unwrap();
assert_eq!(url.raw_query_param("a", 0), Some(Some(Some("1"))));
assert_eq!(url.raw_query_param("a", 1), Some(Some(Some("2"))));
assert_eq!(url.raw_query_param("a", 2), Some(Some(Some("3"))));
assert_eq!(url.raw_query_param("b", 0), Some(Some(Some("%41"))));
assert_eq!(url.raw_query_param("b", 1), Some(Some(Some("%42"))));
assert_eq!(url.raw_query_param("b", 2), Some(Some(Some("%43"))));Sourcepub fn has_raw_query_param(&self, name: &str, index: usize) -> bool
pub fn has_raw_query_param(&self, name: &str, index: usize) -> bool
Return true if Self::raw_query_param would return Some(Some(_)), but doesn’t do any unnecessary computation.
Please note that this returns true even if the query param has no value.
§Examples
use better_url::*;
let url = BetterUrl::parse("https://example.com?a=1&%61=2&a&%61=4").unwrap();
assert!( url.has_raw_query_param("a", 0));
assert!( url.has_raw_query_param("a", 1));
assert!(!url.has_raw_query_param("a", 2));
assert!(!url.has_raw_query_param("a", 3));
assert!(!url.has_raw_query_param("a", 4));Sourcepub fn has_query_param(&self, name: &str, index: usize) -> bool
pub fn has_query_param(&self, name: &str, index: usize) -> bool
Return true if Self::query_param would return Some(Some(_)), but doesn’t do any unnecessary computation.
For matching, the names are percent decoded. So a %61=a query parameter is selectable with a name of a.
Please note that this returns true even if the query param has no value.
§Examples
use better_url::*;
let url = BetterUrl::parse("https://example.com?a=1&%61=2&a&%61=4").unwrap();
assert!( url.has_query_param("a", 0));
assert!( url.has_query_param("a", 1));
assert!( url.has_query_param("a", 2));
assert!( url.has_query_param("a", 3));
assert!(!url.has_query_param("a", 4));Sourcepub fn query_param<'a>(
&'a self,
name: &str,
index: usize,
) -> Option<Option<Option<Cow<'a, str>>>>
pub fn query_param<'a>( &'a self, name: &str, index: usize, ) -> Option<Option<Option<Cow<'a, str>>>>
Get the selected query parameter.
For matching, the names are percent decoded. So a %61=a query parameter is selectable with a name of a.
First Option is if there’s a query.
Second Option is if there’s a query parameter with the specified name.
Third Option is if it has a value.
§Examples
use better_url::*;
let url = BetterUrl::parse("https://example.com?a=2&b=3&a=4&c").unwrap();
assert_eq!(url.query_param("a", 0), Some(Some(Some("2".into()))));
assert_eq!(url.query_param("a", 1), Some(Some(Some("4".into()))));
assert_eq!(url.query_param("a", 2), Some(None));
assert_eq!(url.query_param("b", 0), Some(Some(Some("3".into()))));
assert_eq!(url.query_param("b", 1), Some(None));
assert_eq!(url.query_param("c", 0), Some(Some(None)));
assert_eq!(url.query_param("c", 1), Some(None));
let url = BetterUrl::parse("https://example.com").unwrap();
assert_eq!(url.query_param("a", 0), None);
assert_eq!(url.query_param("a", 1), None);
let url = BetterUrl::parse("https://example.com?a=1&%61=2&a=3&b=%41&%62=%42&b=%43").unwrap();
assert_eq!(url.query_param("a", 0), Some(Some(Some("1".into()))));
assert_eq!(url.query_param("a", 1), Some(Some(Some("2".into()))));
assert_eq!(url.query_param("a", 2), Some(Some(Some("3".into()))));
assert_eq!(url.query_param("b", 0), Some(Some(Some("A".into()))));
assert_eq!(url.query_param("b", 1), Some(Some(Some("B".into()))));
assert_eq!(url.query_param("b", 2), Some(Some(Some("C".into()))));Sourcepub fn set_query_param(
&mut self,
name: &str,
index: usize,
to: Option<Option<&str>>,
) -> Result<(), SetQueryParamError>
pub fn set_query_param( &mut self, name: &str, index: usize, to: Option<Option<&str>>, ) -> Result<(), SetQueryParamError>
Set the selected query parameter.
For matching, the names are percent decoded. So a %61=a query parameter is selectable with a name of a.
If there are N query parameters named name and index is N, appends a new query parameter to the end.
For performance reasons, resulting empty queries are replaced with None.
§Errors
If index is above the number of matched query params, returns the error SetQueryParamError::QueryParamIndexNotFound.
§Examples
use better_url::*;
let mut url = BetterUrl::parse("https://example.com").unwrap();
url.set_query_param("a", 0, None).unwrap();
assert_eq!(url.query(), None);
url.set_query_param("a", 0, Some(Some("2"))).unwrap();
assert_eq!(url.query(), Some("a=2"));
url.set_query_param("a", 0, Some(Some("3"))).unwrap();
assert_eq!(url.query(), Some("a=3"));
url.set_query_param("a", 1, Some(Some("4"))).unwrap();
assert_eq!(url.query(), Some("a=3&a=4"));
url.set_query_param("a", 3, Some(Some("5"))).unwrap_err();
assert_eq!(url.query(), Some("a=3&a=4"));
url.set_query_param("a", 0, Some(None)).unwrap();
assert_eq!(url.query(), Some("a&a=4"));
url.set_query_param("a", 0, None).unwrap();
assert_eq!(url.query(), Some("a=4"));
url.set_query_param("a", 0, None).unwrap();
assert_eq!(url.query(), None);
// Inserting adjacent query params
url.set_query_param("a", 0, Some(Some("2&b=3"))).unwrap();
assert_eq!(url.query(), Some("a=2%26b%3D3"));
// Setting the fragment
url.set_query_param("a", 0, Some(Some("2#123"))).unwrap();
assert_eq!(url.query(), Some("a=2%23123"));
assert_eq!(url.fragment(), None);
url.set_query_param("a", 0, Some(Some("3"))).unwrap();
assert_eq!(url.query(), Some("a=3"));
assert_eq!(url.fragment(), None);
// Empty query optimization.
let mut url = BetterUrl::parse("https://example.com?").unwrap();
assert_eq!(url.query(), Some(""));
url.set_query_param("a", 0, None).unwrap();
assert_eq!(url.query(), None);Sourcepub fn set_raw_query_param(
&mut self,
name: &str,
index: usize,
to: Option<Option<&str>>,
) -> Result<(), SetQueryParamError>
pub fn set_raw_query_param( &mut self, name: &str, index: usize, to: Option<Option<&str>>, ) -> Result<(), SetQueryParamError>
Sets the selected query parameter, without ensuring either the name or the value are valid.
For matching, the names are percent decoded. So a %61=a query parameter is selectable with a name of a.
If there are N query parameters named name and index is N, appends a new query parameter to the end.
For performance reasons, resulting empty queries are replaced with None.
Useful in combination with Self::raw_query_param for transplanting values without decoding then re-encoding them.
PLEASE note that if name and/or value contain special characters like =, &, etc. this will give incoherent results! ONLY use this for directly transplanting from and to query params.
§Errors
If index is above the number of matched query params, returns the error SetQueryParamError::QueryParamIndexNotFound.
§Examples
use better_url::*;
let mut url = BetterUrl::parse("https://example.com").unwrap();
// Normal use
url.set_raw_query_param("a", 0, None).unwrap();
assert_eq!(url.query(), None);
url.set_raw_query_param("a", 0, Some(Some("2"))).unwrap();
assert_eq!(url.query(), Some("a=2"));
url.set_raw_query_param("a", 0, Some(Some("3"))).unwrap();
assert_eq!(url.query(), Some("a=3"));
url.set_raw_query_param("a", 1, Some(Some("4"))).unwrap();
assert_eq!(url.query(), Some("a=3&a=4"));
url.set_raw_query_param("a", 3, Some(Some("5"))).unwrap_err();
assert_eq!(url.query(), Some("a=3&a=4"));
url.set_raw_query_param("a", 0, Some(None)).unwrap();
assert_eq!(url.query(), Some("a&a=4"));
url.set_raw_query_param("a", 0, None).unwrap();
assert_eq!(url.query(), Some("a=4"));
url.set_raw_query_param("a", 0, None).unwrap();
assert_eq!(url.query(), None);
// Inserting adjacent query params
url.set_raw_query_param("a", 0, Some(Some("2&b=3"))).unwrap();
assert_eq!(url.query(), Some("a=2&b=3"));
// Failing to set the fragment because [`Url::set_query`] refuses to.
url.set_raw_query_param("a", 0, Some(Some("2#123"))).unwrap();
assert_eq!(url.query(), Some("a=2%23123&b=3"));
assert_eq!(url.fragment(), None);
url.set_raw_query_param("a", 0, Some(Some("3"))).unwrap();
assert_eq!(url.query(), Some("a=3&b=3"));
assert_eq!(url.fragment(), None);
// Empty query optimization.
let mut url = BetterUrl::parse("https://example.com?").unwrap();
assert_eq!(url.query(), Some(""));
url.set_raw_query_param("a", 0, None).unwrap();
assert_eq!(url.query(), None);Sourcepub fn rename_query_param(
&mut self,
name: &str,
index: usize,
to: &str,
) -> Result<(), RenameQueryParamError>
pub fn rename_query_param( &mut self, name: &str, index: usize, to: &str, ) -> Result<(), RenameQueryParamError>
Rename the specified query parameter to the specified name.
§Errors
If to contains a &, =, or #, returns the error RenameQueryParamError::InvalidName.
If Url::query is None, returns the error RenameQueryParamError::NoQuery.
If the specified query param isn’t found, returns the error RenameQueryParamError::QueryParamNotFound.
§Examples
use better_url::*;
let mut url = BetterUrl::parse("https://example.com?a=1&%61=2&a=3").unwrap();
url.rename_query_param("a", 1, "b").unwrap();
assert_eq!(url.query(), Some("a=1&b=2&a=3"));
url.rename_query_param("a", 1, "b").unwrap();
assert_eq!(url.query(), Some("a=1&b=2&b=3"));
url.rename_query_param("a", 1, "b").unwrap_err();
assert_eq!(url.query(), Some("a=1&b=2&b=3"));Source§impl BetterUrl
impl BetterUrl
Sourcepub fn parse(value: &str) -> Result<BetterUrl, <BetterUrl as FromStr>::Err>
pub fn parse(value: &str) -> Result<BetterUrl, <BetterUrl as FromStr>::Err>
Parse a URL.
§Errors
If the call to Url::parse returns an error, that error is returned.
§Examples
use better_url::*;
let url = BetterUrl::parse("https://example.com").unwrap();Sourcepub fn host_details(&self) -> Option<&HostDetails>
pub fn host_details(&self) -> Option<&HostDetails>
Get the contained HostDetails.
§Examples
use better_url::*;
let url = BetterUrl::parse("https://example.com").unwrap();
assert_eq!(url.host_details(), Some(&HostDetails::Domain(DomainDetails {middle_start: Some(0), suffix_start: Some(8), fqdn_period: None})));
let url = BetterUrl::parse("https://127.0.0.1").unwrap();
assert_eq!(url.host_details(), Some(&HostDetails::Ipv4(Ipv4Details {})));
let url = BetterUrl::parse("https://[::1]").unwrap();
assert_eq!(url.host_details(), Some(&HostDetails::Ipv6(Ipv6Details {})));Sourcepub fn domain_details(&self) -> Option<&DomainDetails>
pub fn domain_details(&self) -> Option<&DomainDetails>
If Self::host_details returns HostDetails::Domain, return it.
use better_url::*;
let url = BetterUrl::parse("https://example.com").unwrap();
assert_eq!(url.domain_details(), Some(&DomainDetails {middle_start: Some(0), suffix_start: Some(8), fqdn_period: None}));
assert_eq!(url.ipv4_details (), None);
assert_eq!(url.ipv6_details (), None);Sourcepub fn ipv4_details(&self) -> Option<&Ipv4Details>
pub fn ipv4_details(&self) -> Option<&Ipv4Details>
If Self::host_details returns HostDetails::Ipv4, return it.
use better_url::*;
let url = BetterUrl::parse("https://127.0.0.1").unwrap();
assert_eq!(url.domain_details(), None);
assert_eq!(url.ipv4_details (), Some(&Ipv4Details {}));
assert_eq!(url.ipv6_details (), None);Sourcepub fn ipv6_details(&self) -> Option<&Ipv6Details>
pub fn ipv6_details(&self) -> Option<&Ipv6Details>
If Self::host_details returns HostDetails::Ipv6, return it.
use better_url::*;
let url = BetterUrl::parse("https://[::1]").unwrap();
assert_eq!(url.domain_details(), None);
assert_eq!(url.ipv4_details (), None);
assert_eq!(url.ipv6_details (), Some(&Ipv6Details {}));Sourcepub fn normalized_host(&self) -> Option<&str>
pub fn normalized_host(&self) -> Option<&str>
Url::host_str with any www. prefix and . suffix removed.
Sourcepub fn set_scheme(&mut self, scheme: &str) -> Result<(), SetSchemeError>
pub fn set_scheme(&mut self, scheme: &str) -> Result<(), SetSchemeError>
§Errors
If the call to Url::set_scheme returns an error, returns the error SetSchemeError.
Sourcepub fn set_username(&mut self, username: &str) -> Result<(), SetUsernameError>
pub fn set_username(&mut self, username: &str) -> Result<(), SetUsernameError>
§Errors
If the call to Url::set_username returns an error, returns the error SetUsernameError.
Sourcepub fn set_password(
&mut self,
password: Option<&str>,
) -> Result<(), SetPasswordError>
pub fn set_password( &mut self, password: Option<&str>, ) -> Result<(), SetPasswordError>
§Errors
If the call to Url::set_password returns an error, returns the error SetPasswordError.
Sourcepub fn set_ip_host(&mut self, address: IpAddr) -> Result<(), SetIpHostError>
pub fn set_ip_host(&mut self, address: IpAddr) -> Result<(), SetIpHostError>
§Errors
If the call to Url::set_ip_host returns an error, returns the error SetIpHostError.
Sourcepub fn set_port(&mut self, port: Option<u16>) -> Result<(), SetPortError>
pub fn set_port(&mut self, port: Option<u16>) -> Result<(), SetPortError>
§Errors
If the call to Url::set_port returns an error, returns the error SetPortError.
Sourcepub fn set_fragment(&mut self, fragment: Option<&str>)
pub fn set_fragment(&mut self, fragment: Option<&str>)
Methods from Deref<Target = Url>§
Sourcepub fn join(&self, input: &str) -> Result<Url, ParseError>
pub fn join(&self, input: &str) -> Result<Url, ParseError>
Parse a string as an URL, with this URL as the base URL.
The inverse of this is make_relative.
§Notes
- A trailing slash is significant. Without it, the last path component is considered to be a “file” name to be removed to get at the “directory” that is used as the base.
- A scheme relative special URL as input replaces everything in the base URL after the scheme.
- An absolute URL (with a scheme) as input replaces the whole base URL (even the scheme).
§Examples
use url::Url;
// Base without a trailing slash
let base = Url::parse("https://example.net/a/b.html")?;
let url = base.join("c.png")?;
assert_eq!(url.as_str(), "https://example.net/a/c.png"); // Not /a/b.html/c.png
// Base with a trailing slash
let base = Url::parse("https://example.net/a/b/")?;
let url = base.join("c.png")?;
assert_eq!(url.as_str(), "https://example.net/a/b/c.png");
// Input as scheme relative special URL
let base = Url::parse("https://alice.com/a")?;
let url = base.join("//eve.com/b")?;
assert_eq!(url.as_str(), "https://eve.com/b");
// Input as base url relative special URL
let base = Url::parse("https://alice.com/a")?;
let url = base.join("/v1/meta")?;
assert_eq!(url.as_str(), "https://alice.com/v1/meta");
// Input as absolute URL
let base = Url::parse("https://alice.com/a")?;
let url = base.join("http://eve.com/b")?;
assert_eq!(url.as_str(), "http://eve.com/b"); // http instead of https
§Errors
If the function can not parse an URL from the given string
with this URL as the base URL, a ParseError variant will be returned.
Sourcepub fn make_relative(&self, url: &Url) -> Option<String>
pub fn make_relative(&self, url: &Url) -> Option<String>
Creates a relative URL if possible, with this URL as the base URL.
This is the inverse of join.
§Examples
use url::Url;
let base = Url::parse("https://example.net/a/b.html")?;
let url = Url::parse("https://example.net/a/c.png")?;
let relative = base.make_relative(&url);
assert_eq!(relative.as_ref().map(|s| s.as_str()), Some("c.png"));
let base = Url::parse("https://example.net/a/b/")?;
let url = Url::parse("https://example.net/a/b/c.png")?;
let relative = base.make_relative(&url);
assert_eq!(relative.as_ref().map(|s| s.as_str()), Some("c.png"));
let base = Url::parse("https://example.net/a/b/")?;
let url = Url::parse("https://example.net/a/d/c.png")?;
let relative = base.make_relative(&url);
assert_eq!(relative.as_ref().map(|s| s.as_str()), Some("../d/c.png"));
let base = Url::parse("https://example.net/a/b.html?c=d")?;
let url = Url::parse("https://example.net/a/b.html?e=f")?;
let relative = base.make_relative(&url);
assert_eq!(relative.as_ref().map(|s| s.as_str()), Some("?e=f"));§Errors
If this URL can’t be a base for the given URL, None is returned.
This is for example the case if the scheme, host or port are not the same.
Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Return the serialization of this URL.
This is fast since that serialization is already stored in the Url struct.
§Examples
use url::Url;
let url_str = "https://example.net/";
let url = Url::parse(url_str)?;
assert_eq!(url.as_str(), url_str);Sourcepub fn origin(&self) -> Origin
pub fn origin(&self) -> Origin
Return the origin of this URL (https://url.spec.whatwg.org/#origin)
Note: this returns an opaque origin for file: URLs, which causes
url.origin() != url.origin().
§Examples
URL with ftp scheme:
use url::{Host, Origin, Url};
let url = Url::parse("ftp://example.com/foo")?;
assert_eq!(url.origin(),
Origin::Tuple("ftp".into(),
Host::Domain("example.com".into()),
21));URL with blob scheme:
use url::{Host, Origin, Url};
let url = Url::parse("blob:https://example.com/foo")?;
assert_eq!(url.origin(),
Origin::Tuple("https".into(),
Host::Domain("example.com".into()),
443));URL with file scheme:
use url::{Host, Origin, Url};
let url = Url::parse("file:///tmp/foo")?;
assert!(!url.origin().is_tuple());
let other_url = Url::parse("file:///tmp/foo")?;
assert!(url.origin() != other_url.origin());URL with other scheme:
use url::{Host, Origin, Url};
let url = Url::parse("foo:bar")?;
assert!(!url.origin().is_tuple());Sourcepub fn scheme(&self) -> &str
pub fn scheme(&self) -> &str
Return the scheme of this URL, lower-cased, as an ASCII string without the ‘:’ delimiter.
§Examples
use url::Url;
let url = Url::parse("file:///tmp/foo")?;
assert_eq!(url.scheme(), "file");Sourcepub fn is_special(&self) -> bool
pub fn is_special(&self) -> bool
Return whether the URL is special (has a special scheme)
§Examples
use url::Url;
assert!(Url::parse("http:///tmp/foo")?.is_special());
assert!(Url::parse("file:///tmp/foo")?.is_special());
assert!(!Url::parse("moz:///tmp/foo")?.is_special());Return whether the URL has an ‘authority’, which can contain a username, password, host, and port number.
URLs that do not are either path-only like unix:/run/foo.socket
or cannot-be-a-base like data:text/plain,Stuff.
See also the authority method.
§Examples
use url::Url;
let url = Url::parse("ftp://rms@example.com")?;
assert!(url.has_authority());
let url = Url::parse("unix:/run/foo.socket")?;
assert!(!url.has_authority());
let url = Url::parse("data:text/plain,Stuff")?;
assert!(!url.has_authority());Return the authority of this URL as an ASCII string.
Non-ASCII domains are punycode-encoded per IDNA if this is the host
of a special URL, or percent encoded for non-special URLs.
IPv6 addresses are given between [ and ] brackets.
Ports are omitted if they match the well known port of a special URL.
Username and password are percent-encoded.
See also the has_authority method.
§Examples
use url::Url;
let url = Url::parse("unix:/run/foo.socket")?;
assert_eq!(url.authority(), "");
let url = Url::parse("file:///tmp/foo")?;
assert_eq!(url.authority(), "");
let url = Url::parse("https://user:password@example.com/tmp/foo")?;
assert_eq!(url.authority(), "user:password@example.com");
let url = Url::parse("irc://àlex.рф.example.com:6667/foo")?;
assert_eq!(url.authority(), "%C3%A0lex.%D1%80%D1%84.example.com:6667");
let url = Url::parse("http://àlex.рф.example.com:80/foo")?;
assert_eq!(url.authority(), "xn--lex-8ka.xn--p1ai.example.com");Sourcepub fn cannot_be_a_base(&self) -> bool
pub fn cannot_be_a_base(&self) -> bool
Return whether this URL is a cannot-be-a-base URL, meaning that parsing a relative URL string with this URL as the base will return an error.
This is the case if the scheme and : delimiter are not followed by a / slash,
as is typically the case of data: and mailto: URLs.
§Examples
use url::Url;
let url = Url::parse("ftp://rms@example.com")?;
assert!(!url.cannot_be_a_base());
let url = Url::parse("unix:/run/foo.socket")?;
assert!(!url.cannot_be_a_base());
let url = Url::parse("data:text/plain,Stuff")?;
assert!(url.cannot_be_a_base());Sourcepub fn username(&self) -> &str
pub fn username(&self) -> &str
Return the username for this URL (typically the empty string) as a percent-encoded ASCII string.
§Examples
use url::Url;
let url = Url::parse("ftp://rms@example.com")?;
assert_eq!(url.username(), "rms");
let url = Url::parse("ftp://:secret123@example.com")?;
assert_eq!(url.username(), "");
let url = Url::parse("https://example.com")?;
assert_eq!(url.username(), "");Sourcepub fn password(&self) -> Option<&str>
pub fn password(&self) -> Option<&str>
Return the password for this URL, if any, as a percent-encoded ASCII string.
§Examples
use url::Url;
let url = Url::parse("ftp://rms:secret123@example.com")?;
assert_eq!(url.password(), Some("secret123"));
let url = Url::parse("ftp://:secret123@example.com")?;
assert_eq!(url.password(), Some("secret123"));
let url = Url::parse("ftp://rms@example.com")?;
assert_eq!(url.password(), None);
let url = Url::parse("https://example.com")?;
assert_eq!(url.password(), None);Sourcepub fn has_host(&self) -> bool
pub fn has_host(&self) -> bool
Equivalent to url.host().is_some().
§Examples
use url::Url;
let url = Url::parse("ftp://rms@example.com")?;
assert!(url.has_host());
let url = Url::parse("unix:/run/foo.socket")?;
assert!(!url.has_host());
let url = Url::parse("data:text/plain,Stuff")?;
assert!(!url.has_host());Sourcepub fn host_str(&self) -> Option<&str>
pub fn host_str(&self) -> Option<&str>
Return the string representation of the host (domain or IP address) for this URL, if any.
Non-ASCII domains are punycode-encoded per IDNA if this is the host
of a special URL, or percent encoded for non-special URLs.
IPv6 addresses are given between [ and ] brackets.
Cannot-be-a-base URLs (typical of data: and mailto:) and some file: URLs
don’t have a host.
See also the host method.
§Examples
use url::Url;
let url = Url::parse("https://127.0.0.1/index.html")?;
assert_eq!(url.host_str(), Some("127.0.0.1"));
let url = Url::parse("https://subdomain.example.com")?;
assert_eq!(url.host_str(), Some("subdomain.example.com"));
let url = Url::parse("ftp://rms@example.com")?;
assert_eq!(url.host_str(), Some("example.com"));
let url = Url::parse("unix:/run/foo.socket")?;
assert_eq!(url.host_str(), None);
let url = Url::parse("data:text/plain,Stuff")?;
assert_eq!(url.host_str(), None);Sourcepub fn host(&self) -> Option<Host<&str>>
pub fn host(&self) -> Option<Host<&str>>
Return the parsed representation of the host for this URL. Non-ASCII domain labels are punycode-encoded per IDNA if this is the host of a special URL, or percent encoded for non-special URLs.
Cannot-be-a-base URLs (typical of data: and mailto:) and some file: URLs
don’t have a host.
See also the host_str method.
§Examples
use url::Url;
let url = Url::parse("https://127.0.0.1/index.html")?;
assert!(url.host().is_some());
let url = Url::parse("ftp://rms@example.com")?;
assert!(url.host().is_some());
let url = Url::parse("unix:/run/foo.socket")?;
assert!(url.host().is_none());
let url = Url::parse("data:text/plain,Stuff")?;
assert!(url.host().is_none());Sourcepub fn domain(&self) -> Option<&str>
pub fn domain(&self) -> Option<&str>
If this URL has a host and it is a domain name (not an IP address), return it. Non-ASCII domains are punycode-encoded per IDNA if this is the host of a special URL, or percent encoded for non-special URLs.
§Examples
use url::Url;
let url = Url::parse("https://127.0.0.1/")?;
assert_eq!(url.domain(), None);
let url = Url::parse("mailto:rms@example.net")?;
assert_eq!(url.domain(), None);
let url = Url::parse("https://example.com/")?;
assert_eq!(url.domain(), Some("example.com"));
let url = Url::parse("https://subdomain.example.com/")?;
assert_eq!(url.domain(), Some("subdomain.example.com"));
Sourcepub fn port(&self) -> Option<u16>
pub fn port(&self) -> Option<u16>
Return the port number for this URL, if any.
Note that default port numbers are never reflected by the serialization,
use the port_or_known_default() method if you want a default port number returned.
§Examples
use url::Url;
let url = Url::parse("https://example.com")?;
assert_eq!(url.port(), None);
let url = Url::parse("https://example.com:443/")?;
assert_eq!(url.port(), None);
let url = Url::parse("ssh://example.com:22")?;
assert_eq!(url.port(), Some(22));Sourcepub fn port_or_known_default(&self) -> Option<u16>
pub fn port_or_known_default(&self) -> Option<u16>
Return the port number for this URL, or the default port number if it is known.
This method only knows the default port number
of the http, https, ws, wss and ftp schemes.
For URLs in these schemes, this method always returns Some(_).
For other schemes, it is the same as Url::port().
§Examples
use url::Url;
let url = Url::parse("foo://example.com")?;
assert_eq!(url.port_or_known_default(), None);
let url = Url::parse("foo://example.com:1456")?;
assert_eq!(url.port_or_known_default(), Some(1456));
let url = Url::parse("https://example.com")?;
assert_eq!(url.port_or_known_default(), Some(443));Sourcepub fn socket_addrs(
&self,
default_port_number: impl Fn() -> Option<u16>,
) -> Result<Vec<SocketAddr>, Error>
pub fn socket_addrs( &self, default_port_number: impl Fn() -> Option<u16>, ) -> Result<Vec<SocketAddr>, Error>
Resolve a URL’s host and port number to SocketAddr.
If the URL has the default port number of a scheme that is unknown to this library,
default_port_number provides an opportunity to provide the actual port number.
In non-example code this should be implemented either simply as || None,
or by matching on the URL’s .scheme().
If the host is a domain, it is resolved using the standard library’s DNS support.
§Examples
let url = url::Url::parse("https://example.net/").unwrap();
let addrs = url.socket_addrs(|| None).unwrap();
std::net::TcpStream::connect(&*addrs)/// With application-specific known default port numbers
fn socket_addrs(url: url::Url) -> std::io::Result<Vec<std::net::SocketAddr>> {
url.socket_addrs(|| match url.scheme() {
"socks5" | "socks5h" => Some(1080),
_ => None,
})
}Sourcepub fn path(&self) -> &str
pub fn path(&self) -> &str
Return the path for this URL, as a percent-encoded ASCII string. For cannot-be-a-base URLs, this is an arbitrary string that doesn’t start with ‘/’. For other URLs, this starts with a ‘/’ slash and continues with slash-separated path segments.
§Examples
use url::{Url, ParseError};
let url = Url::parse("https://example.com/api/versions?page=2")?;
assert_eq!(url.path(), "/api/versions");
let url = Url::parse("https://example.com")?;
assert_eq!(url.path(), "/");
let url = Url::parse("https://example.com/countries/việt nam")?;
assert_eq!(url.path(), "/countries/vi%E1%BB%87t%20nam");Sourcepub fn path_segments(&self) -> Option<Split<'_, char>>
pub fn path_segments(&self) -> Option<Split<'_, char>>
Unless this URL is cannot-be-a-base, return an iterator of ‘/’ slash-separated path segments, each as a percent-encoded ASCII string.
Return None for cannot-be-a-base URLs.
When Some is returned, the iterator always contains at least one string
(which may be empty).
§Examples
use url::Url;
let url = Url::parse("https://example.com/foo/bar")?;
let mut path_segments = url.path_segments().ok_or_else(|| "cannot be base")?;
assert_eq!(path_segments.next(), Some("foo"));
assert_eq!(path_segments.next(), Some("bar"));
assert_eq!(path_segments.next(), None);
let url = Url::parse("https://example.com")?;
let mut path_segments = url.path_segments().ok_or_else(|| "cannot be base")?;
assert_eq!(path_segments.next(), Some(""));
assert_eq!(path_segments.next(), None);
let url = Url::parse("data:text/plain,HelloWorld")?;
assert!(url.path_segments().is_none());
let url = Url::parse("https://example.com/countries/việt nam")?;
let mut path_segments = url.path_segments().ok_or_else(|| "cannot be base")?;
assert_eq!(path_segments.next(), Some("countries"));
assert_eq!(path_segments.next(), Some("vi%E1%BB%87t%20nam"));Sourcepub fn query(&self) -> Option<&str>
pub fn query(&self) -> Option<&str>
Return this URL’s query string, if any, as a percent-encoded ASCII string.
§Examples
use url::Url;
fn run() -> Result<(), ParseError> {
let url = Url::parse("https://example.com/products?page=2")?;
let query = url.query();
assert_eq!(query, Some("page=2"));
let url = Url::parse("https://example.com/products")?;
let query = url.query();
assert!(query.is_none());
let url = Url::parse("https://example.com/?country=español")?;
let query = url.query();
assert_eq!(query, Some("country=espa%C3%B1ol"));Sourcepub fn query_pairs(&self) -> Parse<'_>
pub fn query_pairs(&self) -> Parse<'_>
Parse the URL’s query string, if any, as application/x-www-form-urlencoded
and return an iterator of (key, value) pairs.
§Examples
use std::borrow::Cow;
use url::Url;
let url = Url::parse("https://example.com/products?page=2&sort=desc")?;
let mut pairs = url.query_pairs();
assert_eq!(pairs.count(), 2);
assert_eq!(pairs.next(), Some((Cow::Borrowed("page"), Cow::Borrowed("2"))));
assert_eq!(pairs.next(), Some((Cow::Borrowed("sort"), Cow::Borrowed("desc"))));Sourcepub fn fragment(&self) -> Option<&str>
pub fn fragment(&self) -> Option<&str>
Return this URL’s fragment identifier, if any.
A fragment is the part of the URL after the # symbol.
The fragment is optional and, if present, contains a fragment identifier
that identifies a secondary resource, such as a section heading
of a document.
In HTML, the fragment identifier is usually the id attribute of a an element that is scrolled to on load. Browsers typically will not send the fragment portion of a URL to the server.
Note: the parser did not percent-encode this component, but the input may have been percent-encoded already.
§Examples
use url::Url;
let url = Url::parse("https://example.com/data.csv#row=4")?;
assert_eq!(url.fragment(), Some("row=4"));
let url = Url::parse("https://example.com/data.csv#cell=4,1-6,2")?;
assert_eq!(url.fragment(), Some("cell=4,1-6,2"));Sourcepub fn serialize_internal<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
pub fn serialize_internal<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
Serialize with Serde using the internal representation of the Url struct.
The corresponding deserialize_internal method sacrifices some invariant-checking
for speed, compared to the Deserialize trait impl.
This method is only available if the serde Cargo feature is enabled.
Sourcepub fn to_file_path(&self) -> Result<PathBuf, ()>
pub fn to_file_path(&self) -> Result<PathBuf, ()>
Assuming the URL is in the file scheme or similar,
convert its path to an absolute std::path::Path.
Note: This does not actually check the URL’s scheme,
and may give nonsensical results for other schemes.
It is the user’s responsibility to check the URL’s scheme before calling this.
let path = url.to_file_path();Returns Err if the host is neither empty nor "localhost" (except on Windows, where
file: URLs may have a non-local host),
or if Path::new_opt() returns None.
(That is, if the percent-decoded path contains a NUL byte or,
for a Windows path, is not UTF-8.)
This method is only available if the std Cargo feature is enabled.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for BetterUrl
impl<'de> Deserialize<'de> for BetterUrl
Source§fn deserialize<D>(
deserializer: D,
) -> Result<BetterUrl, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<BetterUrl, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl From<BetterUrl> for LazyTaskConfig<'_>
impl From<BetterUrl> for LazyTaskConfig<'_>
Source§impl From<BetterUrl> for TaskConfig
impl From<BetterUrl> for TaskConfig
Source§fn from(value: BetterUrl) -> Self
fn from(value: BetterUrl) -> Self
Defaults Self::context.
Source§impl Ord for BetterUrl
impl Ord for BetterUrl
Source§impl PartialOrd for BetterUrl
impl PartialOrd for BetterUrl
Source§impl Serialize for BetterUrl
impl Serialize for BetterUrl
Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
impl Eq for BetterUrl
Auto Trait Implementations§
impl Freeze for BetterUrl
impl RefUnwindSafe for BetterUrl
impl Send for BetterUrl
impl Sync for BetterUrl
impl Unpin for BetterUrl
impl UnwindSafe for BetterUrl
Blanket Implementations§
Source§impl<T> AggregateExpressionMethods for T
impl<T> AggregateExpressionMethods for T
Source§fn aggregate_distinct(self) -> Self::Outputwhere
Self: DistinctDsl,
fn aggregate_distinct(self) -> Self::Outputwhere
Self: DistinctDsl,
DISTINCT modifier for aggregate functions Read moreSource§fn aggregate_all(self) -> Self::Outputwhere
Self: AllDsl,
fn aggregate_all(self) -> Self::Outputwhere
Self: AllDsl,
ALL modifier for aggregate functions Read moreSource§fn aggregate_filter<P>(self, f: P) -> Self::Output
fn aggregate_filter<P>(self, f: P) -> Self::Output
Source§fn aggregate_order<O>(self, o: O) -> Self::Outputwhere
Self: OrderAggregateDsl<O>,
fn aggregate_order<O>(self, o: O) -> Self::Outputwhere
Self: OrderAggregateDsl<O>,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoSql for T
impl<T> IntoSql for T
Source§fn into_sql<T>(self) -> Self::Expression
fn into_sql<T>(self) -> Self::Expression
self to an expression for Diesel’s query builder. Read moreSource§fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
&self to an expression for Diesel’s query builder. Read moreSource§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.