Struct seed::browser::url::Url

source ·
pub struct Url { /* private fields */ }
Expand description

URL used for routing.

  • It represents relative URL.
  • Two, almost identical, Urls 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§

source§

impl Url

source

pub fn new() -> Self

Creates a new Url with the empty path.

source

pub fn current() -> Url

Creates a new Url from the one that is currently set in the browser.

source

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),
}
source

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),
}
source

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,
}
source

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,
}
source

pub fn add_path_part(self, path_part: impl Into<String>) -> Self

Adds given path part and returns updated Url.

Example
let link_to_blog = url.add_path_part("blog");
source

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");
source

pub fn to_base_url(&self) -> Self

Clone the Url and strip remaining path parts.

source

pub fn to_hash_base_url(&self) -> Self

Clone the Url and strip remaining hash path parts.

source

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
source

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
source

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

Sets search and returns updated Url.

Example
Url::new().set_search(UrlSearch::new(vec![
    ("x", vec!["1"]),
    ("sort_by", vec!["date", "name"]),
])
References
source

pub fn path(&self) -> &[String]

Get path.

References
source

pub fn hash_path(&self) -> &[String]

Get hash path.

source

pub fn hash(&self) -> Option<&String>

Get hash.

References
source

pub const fn search(&self) -> &UrlSearch

Get search.

References
source

pub fn search_mut(&mut self) -> &mut UrlSearch

Get mutable search.

References
source

pub fn go_and_load(&self)

Change the browser URL and trigger a page load.

source

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.

source

pub fn reload()

Trigger a page reload.

source

pub fn reload_and_skip_cache()

Trigger a page reload and force reloading from the server.

source

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.
source

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.
source

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().

source

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.

source

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”.

source

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"
source

pub fn invalid_components(&self) -> &[String]

Get invalid components.

Undecodable / unparsable components are invalid.

source

pub fn invalid_components_mut(&mut self) -> &mut Vec<String>

Get mutable invalid components.

Undecodable / unparsable components are invalid.

Trait Implementations§

source§

impl Clone for Url

source§

fn clone(&self) -> Url

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Url

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Url

source§

fn default() -> Url

Returns the “default value” for a type. Read more
source§

impl Display for Url

All components are automatically encoded.

source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a> From<&'a Url> for Cow<'a, Url>

source§

fn from(url: &'a Url) -> Cow<'a, Url>

Converts to this type from the input type.
source§

impl From<&Url> for Url

source§

fn from(url: &Url) -> Self

Creates a new Url from the browser native url. Url’s components are decoded if possible. When decoding fails, the component is cloned into invalid_components and the original value is used.

source§

impl<'a> From<Url> for Cow<'a, Url>

source§

fn from(url: Url) -> Cow<'a, Url>

Converts to this type from the input type.
source§

impl FromStr for Url

source§

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 Urls invalid_components - see methods Url::invalid_components and Url::invalid_components_mut.

§

type Err = String

The associated error which can be returned from parsing.
source§

impl PartialEq<Url> for Url

source§

fn eq(&self, other: &Url) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Url

source§

impl StructuralEq for Url

source§

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§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V