Skip to main content

Origin

Struct Origin 

Source
#[non_exhaustive]
pub struct Origin { /* private fields */ }
Expand description

Scheme and authority components of a URI.

Use Origin when you need to work with the base parts of a URI without the path, query, or fragment components.

Origin accepts any valid URI scheme. The port is only reported when it is explicitly present in the authority; default ports are not inferred from the scheme.

Implementations§

Source§

impl Origin

Source

pub fn from_static(s: &'static str) -> Self

Creates a new Origin by parsing a static string.

§Panics

Panics if the string is not a valid origin (missing scheme or invalid authority). Intended for use with compile-time-known constants; use Origin::from_str for fallible parsing.

§Examples
let origin = Origin::from_static("https://example.com:8443");
assert_eq!(origin.scheme().as_str(), "https");
assert_eq!(origin.authority().as_str(), "example.com:8443");
Source

pub fn from_parts(scheme: Scheme, authority: Authority) -> Self

Creates a new Origin from the given scheme and authority.

Both components are already validated by their respective types, so this constructor is infallible.

Source

pub fn try_from_parts( scheme: impl TryInto<Scheme, Error: Into<Error>>, authority: impl TryInto<Authority, Error: Into<Error>>, ) -> Result<Self, UriError>

Creates a new Origin from values convertible into a Scheme and Authority.

For pre-typed Scheme and Authority values, prefer the infallible Origin::from_parts.

§Errors

Returns a UriError if either conversion fails.

Source

pub const fn scheme(&self) -> &Scheme

Returns a reference to the scheme.

Source

pub const fn authority(&self) -> &Authority

Returns a reference to the authority.

Source

pub fn into_parts(self) -> (Scheme, Authority)

Consumes the origin and returns the scheme and authority.

Source

pub fn port(&self) -> Option<u16>

Returns the explicit port of this origin, if any.

Returns the explicit port from the authority when present, or None when the authority does not specify a port. Default ports are not inferred from the scheme; use Origin::effective_port for that.

Source

pub fn effective_port(&self) -> Option<u16>

Returns the effective port of this origin, falling back to well-known scheme defaults when no explicit port is present.

Returns the explicit port from the authority when present. Otherwise, returns the IANA-registered default port for the scheme:

  • 80 for http
  • 443 for https

Returns None when no explicit port is present and the scheme has no well-known default known to this crate.

§Examples
// Explicit port is returned as-is.
let origin = Origin::from_static("https://example.com:8443");
assert_eq!(origin.effective_port(), Some(8443));

// HTTPS default is used when no port is specified.
let origin = Origin::from_static("https://example.com");
assert_eq!(origin.effective_port(), Some(443));

// HTTP default is used when no port is specified.
let origin = Origin::from_static("http://example.com");
assert_eq!(origin.effective_port(), Some(80));
Source

pub fn with_port(self, port: u16) -> Self

Set port for this Origin instance.

§Examples
let origin = Origin::from_static("https://example.com");
let origin_with_port = origin.with_port(8443);
assert_eq!(origin_with_port.port(), Some(8443));
assert_eq!(format!("{}", origin_with_port), "https://example.com:8443");
Source

pub fn is_https(&self) -> bool

Checks if this origin uses the HTTPS scheme.

This method returns true if the scheme is HTTPS, false otherwise. For more details, see BaseUri::is_https.

Trait Implementations§

Source§

impl Clone for Origin

Source§

fn clone(&self) -> Origin

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 Origin

Source§

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

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

impl<'de> Deserialize<'de> for Origin

Available on crate feature serde only.
Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Origin

Source§

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

Formats the Origin as a string in the form scheme://authority.

Default ports (80 for HTTP, 443 for HTTPS) are omitted from the display string. Custom ports are included when present.

§Examples
let origin = Origin::from_static("https://example.com:443");
assert_eq!(format!("{}", origin), "https://example.com");

let custom_port = Origin::from_static("https://example.com:8443");
assert_eq!(format!("{}", custom_port), "https://example.com:8443");
Source§

impl From<Origin> for BaseUri

Source§

fn from(origin: Origin) -> Self

Converts an Origin into a BaseUri with a root path (/).

Source§

impl FromStr for Origin

Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses an Origin from a string in the form scheme://authority.

§Errors

Returns a UriError if the string is not a valid origin (missing scheme or invalid authority).

Source§

type Err = UriError

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

impl Hash for Origin

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Origin

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Origin

Available on crate feature serde only.
Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for Origin

Source§

impl StructuralPartialEq for Origin

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> 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<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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,