#[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
impl Origin
Sourcepub fn from_static(s: &'static str) -> Self
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");Sourcepub fn from_parts(scheme: Scheme, authority: Authority) -> Self
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.
Sourcepub fn try_from_parts(
scheme: impl TryInto<Scheme, Error: Into<Error>>,
authority: impl TryInto<Authority, Error: Into<Error>>,
) -> Result<Self, UriError>
pub fn try_from_parts( scheme: impl TryInto<Scheme, Error: Into<Error>>, authority: impl TryInto<Authority, Error: Into<Error>>, ) -> Result<Self, UriError>
Returns a reference to the authority.
Sourcepub fn into_parts(self) -> (Scheme, Authority)
pub fn into_parts(self) -> (Scheme, Authority)
Consumes the origin and returns the scheme and authority.
Sourcepub fn port(&self) -> Option<u16>
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.
Sourcepub fn effective_port(&self) -> Option<u16>
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:
80forhttp443forhttps
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));Sourcepub fn with_port(self, port: u16) -> Self
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");Sourcepub fn is_https(&self) -> bool
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<'de> Deserialize<'de> for Origin
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Origin
serde only.Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Source§impl Display for Origin
impl Display for Origin
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
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");