iroh_base/
relay_url.rs

1use std::{fmt, ops::Deref, str::FromStr, sync::Arc};
2
3use n0_error::stack_error;
4use serde::{Deserialize, Serialize};
5use url::Url;
6
7/// A URL identifying a relay server.
8///
9/// It is cheaply clonable, as the underlying type is wrapped into an `Arc`.
10/// The main type under the hood though is [`Url`], with a few custom tweaks:
11///
12/// - A relay URL is never a relative URL, so an implicit `.` is added at the end of the
13///   domain name if missing.
14///
15/// - [`fmt::Debug`] is implemented so it prints the URL rather than the URL struct fields.
16///   Useful when logging e.g. `Option<RelayUrl>`.
17///
18/// To create a [`RelayUrl`] use the `From<Url>` implementation.
19#[derive(
20    Clone, derive_more::Display, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
21)]
22pub struct RelayUrl(Arc<Url>);
23
24impl From<Url> for RelayUrl {
25    fn from(mut url: Url) -> Self {
26        if let Some(domain) = url.domain() {
27            if !domain.ends_with('.') {
28                let domain = String::from(domain) + ".";
29
30                // This can fail, though it is unlikely the resulting URL is usable as a
31                // relay URL, probably it has the wrong scheme or is not a base URL or the
32                // like.  We don't do full URL validation however, so just silently leave
33                // this bad URL in place.  Something will fail later.
34                url.set_host(Some(&domain)).ok();
35            }
36        }
37        Self(Arc::new(url))
38    }
39}
40
41/// Can occur when parsing a string into a [`RelayUrl`].
42#[stack_error(derive, add_meta)]
43#[error("Failed to parse relay URL")]
44pub struct RelayUrlParseError(#[error(std_err)] url::ParseError);
45
46/// Support for parsing strings directly.
47///
48/// If you need more control over the error first create a [`Url`] and use [`RelayUrl::from`]
49/// instead.
50impl FromStr for RelayUrl {
51    type Err = RelayUrlParseError;
52
53    fn from_str(s: &str) -> Result<Self, Self::Err> {
54        let inner = Url::from_str(s).map_err(RelayUrlParseError::new)?;
55        Ok(RelayUrl::from(inner))
56    }
57}
58
59impl From<RelayUrl> for Url {
60    fn from(value: RelayUrl) -> Self {
61        Arc::unwrap_or_clone(value.0)
62    }
63}
64
65/// Dereferences to the wrapped [`Url`].
66///
67/// Note that [`DerefMut`] is not implemented on purpose, so this type has more flexibility
68/// to change the inner later.
69///
70/// [`DerefMut`]: std::ops::DerefMut
71impl Deref for RelayUrl {
72    type Target = Url;
73
74    fn deref(&self) -> &Self::Target {
75        &self.0
76    }
77}
78
79impl fmt::Debug for RelayUrl {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        f.debug_tuple("RelayUrl")
82            .field(&DbgStr(self.0.as_str()))
83            .finish()
84    }
85}
86
87/// Helper struct to format a &str without allocating a String.
88///
89/// Maybe this is entirely unneeded and the compiler would be smart enough to never allocate
90/// the String anyway.  Who knows.  Writing this was faster than checking the assembler
91/// output.
92struct DbgStr<'a>(&'a str);
93
94impl fmt::Debug for DbgStr<'_> {
95    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96        write!(f, r#""{}""#, self.0)
97    }
98}
99
100#[cfg(test)]
101mod tests {
102    use super::*;
103
104    #[test]
105    fn test_relay_url_debug_display() {
106        let url = RelayUrl::from(Url::parse("https://example.com").unwrap());
107
108        assert_eq!(format!("{url:?}"), r#"RelayUrl("https://example.com./")"#);
109
110        assert_eq!(format!("{url}"), "https://example.com./");
111    }
112
113    #[test]
114    fn test_relay_url_absolute() {
115        let url = RelayUrl::from(Url::parse("https://example.com").unwrap());
116
117        assert_eq!(url.domain(), Some("example.com."));
118
119        let url1 = RelayUrl::from(Url::parse("https://example.com.").unwrap());
120        assert_eq!(url, url1);
121
122        let url2 = RelayUrl::from(Url::parse("https://example.com./").unwrap());
123        assert_eq!(url, url2);
124
125        let url3 = RelayUrl::from(Url::parse("https://example.com/").unwrap());
126        assert_eq!(url, url3);
127    }
128}