Skip to main content

saml_rs/model/
endpoint.rs

1use crate::error::SamlError;
2
3/// Absolute HTTP(S) endpoint URL used by typed SAML endpoint wrappers.
4#[derive(Debug, Clone, PartialEq, Eq, Hash)]
5pub struct EndpointUrl(String);
6
7impl EndpointUrl {
8    /// Validate and wrap an absolute HTTP(S) URL.
9    ///
10    /// # Errors
11    ///
12    /// Returns [`SamlError::Invalid`] when the URL is not absolute HTTP(S).
13    pub fn try_new(value: impl Into<String>) -> Result<Self, SamlError> {
14        let value = value.into();
15        let parsed = url::Url::parse(&value).map_err(|err| SamlError::Invalid(err.to_string()))?;
16        if matches!(parsed.scheme(), "http" | "https") && parsed.has_host() {
17            return Ok(Self(value));
18        }
19        Err(SamlError::Invalid(
20            "endpoint URL must be absolute HTTP(S)".into(),
21        ))
22    }
23
24    /// Borrow the URL string.
25    pub fn as_str(&self) -> &str {
26        &self.0
27    }
28}