Repository: astral-sh/uv
File: crates/uv-distribution-types/src/file.rs (Rust)

Code before:
            .map(|(path, _)| path)
            .unwrap_or(self.as_ref())
    }

    /// Return the [`UrlString`] (as a [`Cow`]) with any fragments removed.
    #[must_use]
    pub fn without_fragment(&self) -> Cow<'_, Self> {
        self.as_ref()
            .split_once('#')
            .map(|(path, _)| Cow::Owned(UrlString(SmallString::from(path))))
            .unwrap_or(Cow::Borrowed(self))
    }

    /// Return the [`UrlString`] (as a [`Cow`]) with trailing slash removed.
    ///
    /// This matches the semantics of [`Url::pop_if_empty`], which will not trim a trailing slash if
    /// it's the only path segment, e.g., `https://example.com/` would be unchanged.
    #[must_use]
    pub fn without_trailing_slash(&self) -> Cow<'_, Self> {
        self.as_ref()
            .strip_suffix('/')
            .and_then(|path| {
                // Only strip the trailing slash if there's _another_ trailing slash that isn't a
                // part of the scheme.
                path.rfind('/')
                    .is_some_and(|r| r > path.find("://").unwrap_or_default() + 2)
                    .then_some(Cow::Owned(UrlString(SmallString::from(path))))
            })
            .unwrap_or(Cow::Borrowed(self))
    }
}

impl AsRef<str> for UrlString {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl From<DisplaySafeUrl> for UrlString {
    fn from(value: DisplaySafeUrl) -> Self {
        Self(value.as_str().into())
    }
}

impl From<&DisplaySafeUrl> for UrlString {
    fn from(value: &DisplaySafeUrl) -> Self {
        Self(value.as_str().into())
    }
}

impl Display for UrlString {

Review comment (suggestion):
If it doesn't contain `://`, should we return `None` rather than 0? (Again, didn't think about it super hard.)

Code after:
            .map(|(path, _)| path)
            .unwrap_or(self.as_ref())
    }

    /// Return the [`UrlString`] (as a [`Cow`]) with any fragments removed.
    #[must_use]
    pub fn without_fragment(&self) -> Cow<'_, Self> {
        self.as_ref()
            .split_once('#')
            .map(|(path, _)| Cow::Owned(UrlString(SmallString::from(path))))
            .unwrap_or(Cow::Borrowed(self))
    }

    /// Return the [`UrlString`] (as a [`Cow`]) with trailing slash removed.
    ///
    /// This matches the semantics of [`Url::pop_if_empty`], which will not trim a trailing slash if
    /// it's the only path segment, e.g., `https://example.com/` would be unchanged.
    #[must_use]
    pub fn without_trailing_slash(&self) -> Cow<'_, Self> {
        self.as_ref()
            .strip_suffix('/')
            .filter(|path| {
                // Only strip the trailing slash if there's _another_ trailing slash that isn't a
                // part of the scheme.
                path.split_once("://")
                    .map(|(_scheme, rest)| rest)
                    .unwrap_or(path)
                    .contains('/')
            })
            .map(|path| Cow::Owned(UrlString(SmallString::from(path))))
            .unwrap_or(Cow::Borrowed(self))
    }
}

impl AsRef<str> for UrlString {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl From<DisplaySafeUrl> for UrlString {
    fn from(value: DisplaySafeUrl) -> Self {
        Self(value.as_str().into())
    }
}

impl From<&DisplaySafeUrl> for UrlString {
    fn from(value: &DisplaySafeUrl) -> Self {
        Self(value.as_str().into())
    }
}