Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Url wrapper type #58

Open
Nutomic opened this issue Jul 3, 2023 · 0 comments
Open

Add Url wrapper type #58

Nutomic opened this issue Jul 3, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@Nutomic
Copy link
Member

Nutomic commented Jul 3, 2023

The url::Url type is awkward for our use because it has domain as an optional field, and when logging it prints individual url components instead of the full url as a string. We should add a wrapper type like the following to workaround these issues.

#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Url(url::Url);

impl Deref for Url {
    type Target = url::Url;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Display for Url {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.0.display(f)
    }
}

impl Url {
    pub fn parse(input: &str) -> Result<Url, url::ParseError> {
        url::Url::parse(input).map(Url)
    }
    pub fn domain(&self) -> &str {
        self.0.domain().expect("has domain")
    }
    pub fn into_inner(self) -> Self {
        self
    }
}

Posting this as an issue instead of PR because now is not a good time for breaking changes.

@Nutomic Nutomic added the enhancement New feature or request label Jul 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant