diff --git a/src/responders/location.rs b/src/responders/location.rs index 8ec740b..39b5a7d 100644 --- a/src/responders/location.rs +++ b/src/responders/location.rs @@ -27,6 +27,7 @@ pub struct HxLocation { } impl HxLocation { + /// Creates location from [`Uri`] without any options. pub fn from_uri(uri: Uri) -> Self { Self { #[cfg(feature = "serde")] @@ -35,12 +36,36 @@ impl HxLocation { } } + /// Creates location from [`Uri`] and options. #[cfg(feature = "serde")] #[cfg_attr(feature = "unstable", doc(cfg(feature = "serde")))] pub fn from_uri_with_options(uri: Uri, options: LocationOptions) -> Self { Self { uri, options } } + /// Parses `uri` and sets it as location. + #[allow(clippy::should_implement_trait)] + pub fn from_str(uri: impl AsRef) -> Result { + Ok(Self { + #[cfg(feature = "serde")] + options: LocationOptions::default(), + uri: uri.as_ref().parse::()?, + }) + } + + /// Parses `uri` and sets it as location with additional options. + #[cfg(feature = "serde")] + #[cfg_attr(feature = "unstable", doc(cfg(feature = "serde")))] + pub fn from_str_with_options( + uri: impl AsRef, + options: LocationOptions, + ) -> Result { + Ok(Self { + options, + uri: uri.as_ref().parse::()?, + }) + } + #[cfg(feature = "serde")] fn into_header_with_options(self) -> Result { if self.options.is_default() { @@ -62,11 +87,35 @@ impl HxLocation { } } +impl From for HxLocation { + fn from(uri: Uri) -> Self { + Self::from_uri(uri) + } +} + +#[cfg(feature = "serde")] +#[cfg_attr(feature = "unstable", doc(cfg(feature = "serde")))] +impl From<(Uri, LocationOptions)> for HxLocation { + fn from((uri, options): (Uri, LocationOptions)) -> Self { + Self::from_uri_with_options(uri, options) + } +} + impl<'a> TryFrom<&'a str> for HxLocation { type Error = ::Err; - fn try_from(value: &'a str) -> Result { - Ok(Self::from_uri(Uri::from_str(value)?)) + fn try_from(uri: &'a str) -> Result { + Self::from_str(uri) + } +} + +#[cfg(feature = "serde")] +#[cfg_attr(feature = "unstable", doc(cfg(feature = "serde")))] +impl<'a> TryFrom<(&'a str, LocationOptions)> for HxLocation { + type Error = ::Err; + + fn try_from((uri, options): (&'a str, LocationOptions)) -> Result { + Self::from_str_with_options(uri, options) } }