pub struct Url { /* private fields */ }
Expand description
URL used for routing.
- It represents relative URL.
- Two, almost identical,
Url
s that differ only with differently advanced internal path or hash path iterators (e.g.next_path_part()
was called on one of them) are considered different also during comparison.
(If the features above are problems for you, create an issue)
Implementations
sourceimpl Url
impl Url
sourcepub fn go_and_push(&self)
pub fn go_and_push(&self)
Change the browser URL, but do not trigger a page load.
This will add a new entry to the browser history.
References
sourcepub fn go_and_replace(&self)
pub fn go_and_replace(&self)
Change the browser URL, but do not trigger a page load.
This will NOT add a new entry to the browser history.
References
sourcepub fn next_path_part(&mut self) -> Option<&str>
pub fn next_path_part(&mut self) -> Option<&str>
Advances the internal path iterator and returns the next path part as Option<&str>
.
Example
match url.next_path_part() {
None => Page::Home,
Some("report") => Page::Report(page::report::init(url)),
_ => Page::Unknown(url),
}
sourcepub fn next_hash_path_part(&mut self) -> Option<&str>
pub fn next_hash_path_part(&mut self) -> Option<&str>
Advances the internal hash path iterator and returns the next hash path part as Option<&str>
.
Example
match url.next_hash_path_part() {
None => Page::Home,
Some("report") => Page::Report(page::report::init(url)),
_ => Page::Unknown(url),
}
sourcepub fn remaining_path_parts(&mut self) -> Vec<&str>
pub fn remaining_path_parts(&mut self) -> Vec<&str>
Collects the internal path iterator and returns it as Vec<&str>
.
Example
match url.remaining_path_parts().as_slice() {
[] => Page::Home,
["report", rest @ ..] => {
match rest {
["day"] => Page::ReportDay,
_ => Page::ReportWeek,
}
},
_ => Page::NotFound,
}
sourcepub fn remaining_hash_path_parts(&mut self) -> Vec<&str>
pub fn remaining_hash_path_parts(&mut self) -> Vec<&str>
Collects the internal hash path iterator and returns it as Vec<&str>
.
Example
match url.remaining_hash_path_parts().as_slice() {
[] => Page::Home,
["report", rest @ ..] => {
match rest {
["day"] => Page::ReportDay,
_ => Page::ReportWeek,
}
},
_ => Page::NotFound,
}
sourcepub fn add_path_part(self, path_part: impl Into<String>) -> Self
pub fn add_path_part(self, path_part: impl Into<String>) -> Self
sourcepub fn add_hash_path_part(self, hash_path_part: impl Into<String>) -> Self
pub fn add_hash_path_part(self, hash_path_part: impl Into<String>) -> Self
Adds given hash path part and returns updated Url
.
It also changes hash
.
Example
let link_to_blog = url.add_hash_path_part("blog");
sourcepub fn to_base_url(&self) -> Self
pub fn to_base_url(&self) -> Self
Clone the Url
and strip remaining path parts.
sourcepub fn to_hash_base_url(&self) -> Self
pub fn to_hash_base_url(&self) -> Self
Clone the Url
and strip remaining hash path parts.
sourcepub fn set_path<T: ToString>(
self,
into_path_iterator: impl IntoIterator<Item = T>
) -> Self
pub fn set_path<T: ToString>(
self,
into_path_iterator: impl IntoIterator<Item = T>
) -> Self
Sets path and returns updated Url
. It also resets internal path iterator.
Example
Url::new().set_path(&["my", "path"])
References
sourcepub fn set_hash_path<T: ToString>(
self,
into_hash_path_iterator: impl IntoIterator<Item = T>
) -> Self
pub fn set_hash_path<T: ToString>(
self,
into_hash_path_iterator: impl IntoIterator<Item = T>
) -> Self
Sets hash path and returns updated Url
.
It also resets internal hash path iterator and sets hash
.
Example
Url::new().set_hash_path(&["my", "path"])
References
sourcepub fn set_hash(self, hash: impl Into<String>) -> Self
pub fn set_hash(self, hash: impl Into<String>) -> Self
Sets hash and returns updated Url
.
I also sets hash_path
.
Example
Url::new().set_hash("my_hash")
References
sourcepub fn set_search(self, search: impl Into<UrlSearch>) -> Self
pub fn set_search(self, search: impl Into<UrlSearch>) -> Self
Sets search and returns updated Url
.
Example
Url::new().set_search(UrlSearch::new(vec![
("x", vec!["1"]),
("sort_by", vec!["date", "name"]),
])
References
sourcepub fn search_mut(&mut self) -> &mut UrlSearch
pub fn search_mut(&mut self) -> &mut UrlSearch
sourcepub fn go_and_load(&self)
pub fn go_and_load(&self)
Change the browser URL and trigger a page load.
sourcepub fn go_and_load_with_str(url: impl AsRef<str>)
pub fn go_and_load_with_str(url: impl AsRef<str>)
Change the browser URL and trigger a page load.
Provided url
isn’t checked and it’s passed into location.href
.
sourcepub fn reload_and_skip_cache()
pub fn reload_and_skip_cache()
Trigger a page reload and force reloading from the server.
sourcepub fn go_back(steps: i32)
pub fn go_back(steps: i32)
Move back in History
.
steps: 0
only reloads the current page.- Negative steps move you forward - use rather
Url::go_forward
instead. - If there is no previous page, this call does nothing.
sourcepub fn go_forward(steps: i32)
pub fn go_forward(steps: i32)
Move back in History
.
steps: 0
only reloads the current page.- Negative steps move you back - use rather
Url::go_back
instead. - If there is no next page, this call does nothing.
sourcepub fn skip_base_path(self, path_base: &[String]) -> Self
pub fn skip_base_path(self, path_base: &[String]) -> Self
If the current Url
’s path prefix is equal to path_base
,
then reset the internal path iterator and advance it to skip the prefix (aka path_base
).
It’s used mostly by Seed internals, but it can be useful in combination
with orders.clone_base_path()
.
sourcepub fn skip_hash_base_path(self, hash_path_base: &[String]) -> Self
pub fn skip_hash_base_path(self, hash_path_base: &[String]) -> Self
If the current Url
’s hash path prefix is equal to hash_path_base
,
then reset the internal hash path iterator and advance it to skip the prefix (aka hash_path_base
).
It’s used mostly by Seed internals.
sourcepub fn decode_uri_component(
component: impl AsRef<str>
) -> Result<String, JsValue>
pub fn decode_uri_component(
component: impl AsRef<str>
) -> Result<String, JsValue>
Decodes a Uniform Resource Identifier (URI) component. Aka percent-decoding.
Note: All components are automatically decoded when it’s possible.
You can find undecodable components in the vector
returned from methods invalid_components
or invalid_components_mut
.
Example
Url::decode_uri_component("Hello%20G%C3%BCnter"); // => "Hello Günter"
Errors
Returns error when decoding fails - e.g. “Error: malformed URI sequence”.
sourcepub fn encode_uri_component(component: impl AsRef<str>) -> String
pub fn encode_uri_component(component: impl AsRef<str>) -> String
Encode to a Uniform Resource Identifier (URI) component. Aka percent-encoding.
Note: All components are automatically encoded
in Display
implementation for Url
- i.e. url.to_string()
returns encoded url.
Example
Url::encode_uri_component("Hello Günter"); // => "Hello%20G%C3%BCnter"
sourcepub fn invalid_components(&self) -> &[String]
pub fn invalid_components(&self) -> &[String]
Get invalid components.
Undecodable / unparsable components are invalid.
sourcepub fn invalid_components_mut(&mut self) -> &mut Vec<String>
pub fn invalid_components_mut(&mut self) -> &mut Vec<String>
Get mutable invalid components.
Undecodable / unparsable components are invalid.
Trait Implementations
sourceimpl<'de> Deserialize<'de> for Url
impl<'de> Deserialize<'de> for Url
sourcefn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl FromStr for Url
impl FromStr for Url
sourcefn from_str(str_url: &str) -> Result<Self, Self::Err>
fn from_str(str_url: &str) -> Result<Self, Self::Err>
Creates a new Url
from &str
.
Errors
Returns error when url
cannot be parsed.
Note: When only some components are undecodable, no error is returned -
that components are saved into the Url
s invalid_components
- see methods
Url::invalid_components
and Url::invalid_components_mut
.
impl StructuralPartialEq for Url
Auto Trait Implementations
impl RefUnwindSafe for Url
impl Send for Url
impl Sync for Url
impl Unpin for Url
impl UnwindSafe for Url
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more