Skip to main content

BaseUrl

Struct BaseUrl 

Source
pub struct BaseUrl { /* private fields */ }
Expand description

The absolute URL an application is publicly reachable at, like https://example.com.

Relative URLs work anywhere within the site, but rendered content that leaves it (e.g. links and images in emails, feeds, or sitemaps) needs the absolute form, resolved against this base. A base URL is an http or https URL with a host and an optional path prefix (for applications mounted under one, like https://example.com/app), and no query or fragment. The string is parsed at construction, so every value of this type holds a well-formed base.

Register one on the router builder with .base_url(...), read it back with base_url or try_base_url, and resolve paths against it with join:

use topcoat::context::BaseUrl;

let base = BaseUrl::new("https://example.com")?;
assert_eq!(
    base.join("/assets/logo.png"),
    "https://example.com/assets/logo.png"
);

Implementations§

Source§

impl BaseUrl

Source

pub fn new(url: impl AsRef<str>) -> Result<BaseUrl, BaseUrlError>

Parses a base URL.

The scheme and any trailing slash are normalized, so https://example.com/app/ and https://example.com/app are the same base.

§Errors

Returns BaseUrlError if the string is not an absolute http or https URL, or carries a query string or fragment.

Source

pub fn join(&self, path: &str) -> String

Resolves a root-relative path into an absolute URL.

The path is taken as relative to the application root whether or not it starts with a slash: base.join("/assets/logo.png") and base.join("assets/logo.png") produce the same URL. A query string on the path is carried through.

Source

pub fn as_str(&self) -> &str

The base URL as a string, without a trailing slash.

Trait Implementations§

Source§

impl Clone for BaseUrl

Source§

fn clone(&self) -> BaseUrl

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for BaseUrl

Source§

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

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

impl Display for BaseUrl

Source§

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

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

impl Eq for BaseUrl

Source§

impl From<&BaseUrl> for BaseUrl

Source§

fn from(base_url: &BaseUrl) -> BaseUrl

Converts to this type from the input type.
Source§

impl FromStr for BaseUrl

Source§

type Err = BaseUrlError

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

fn from_str(s: &str) -> Result<BaseUrl, BaseUrlError>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq for BaseUrl

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for BaseUrl

Source§

impl TryFrom<&String> for BaseUrl

Source§

type Error = BaseUrlError

The type returned in the event of a conversion error.
Source§

fn try_from(url: &String) -> Result<BaseUrl, BaseUrlError>

Performs the conversion.
Source§

impl TryFrom<&str> for BaseUrl

Source§

type Error = BaseUrlError

The type returned in the event of a conversion error.
Source§

fn try_from(url: &str) -> Result<BaseUrl, BaseUrlError>

Performs the conversion.
Source§

impl TryFrom<String> for BaseUrl

Source§

type Error = BaseUrlError

The type returned in the event of a conversion error.
Source§

fn try_from(url: String) -> Result<BaseUrl, BaseUrlError>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneAny for T
where T: Any + Clone,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where 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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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 T
where T: Clone,

Source§

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 T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

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

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.