activitypub-federation-rust icon indicating copy to clipboard operation
activitypub-federation-rust copied to clipboard

Add Url wrapper type

Open Nutomic opened this issue 1 year ago • 0 comments

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 avatar Jul 03 '23 13:07 Nutomic