Skip to content

Commit c37ace8

Browse files
committed
nostr: add RelayUrlScheme enum and RelayUrl::scheme method
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 837df0f commit c37ace8

File tree

2 files changed

+74
-8
lines changed

2 files changed

+74
-8
lines changed

crates/nostr/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
2424
-->
2525

26+
## Unreleased
27+
28+
### Added
29+
30+
- Add `RelayUrlScheme` enum and `RelayUrl::scheme` method (https://github.com/rust-nostr/nostr/pull/1127)
31+
2632
## v0.44.1 - 2025/11/09
2733

2834
### Fixed

crates/nostr/src/types/url.rs

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,46 @@ impl From<ParseError> for Error {
4949
}
5050
}
5151

52+
/// Relay URL scheme
53+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
54+
pub enum RelayUrlScheme {
55+
/// WebSocket (no SSL/TLS)
56+
Ws,
57+
/// WebSocket Secure
58+
Wss,
59+
}
60+
61+
impl RelayUrlScheme {
62+
/// Parse relay URL scheme
63+
#[inline]
64+
pub fn parse(scheme: &str) -> Result<Self, Error> {
65+
match scheme {
66+
"ws" => Ok(Self::Ws),
67+
"wss" => Ok(Self::Wss),
68+
_ => Err(Error::UnsupportedScheme),
69+
}
70+
}
71+
72+
/// Check if the scheme is secure (uses SSL/TLS)
73+
#[inline]
74+
pub fn is_secure(&self) -> bool {
75+
matches!(self, Self::Wss)
76+
}
77+
78+
/// Get as `&str`
79+
#[inline]
80+
pub fn as_str(&self) -> &str {
81+
match self {
82+
Self::Ws => "ws",
83+
Self::Wss => "wss",
84+
}
85+
}
86+
}
87+
5288
/// Relay URL
5389
#[derive(Clone)]
5490
pub struct RelayUrl {
91+
scheme: RelayUrlScheme,
5592
url: Url,
5693
has_trailing_slash: bool,
5794
}
@@ -104,14 +141,20 @@ impl RelayUrl {
104141
// Parse URL
105142
let url: Url = Url::parse(url)?;
106143

107-
// Check scheme
108-
match url.scheme() {
109-
"ws" | "wss" => Ok(Self {
110-
url,
111-
has_trailing_slash,
112-
}),
113-
_ => Err(Error::UnsupportedScheme),
114-
}
144+
// Parse scheme
145+
let scheme: RelayUrlScheme = RelayUrlScheme::parse(url.scheme())?;
146+
147+
Ok(Self {
148+
scheme,
149+
url,
150+
has_trailing_slash,
151+
})
152+
}
153+
154+
/// Get scheme
155+
#[inline]
156+
pub fn scheme(&self) -> RelayUrlScheme {
157+
self.scheme
115158
}
116159

117160
/// Check if the host is a local network address.
@@ -316,6 +359,23 @@ impl TryIntoUrl for &str {
316359
mod tests {
317360
use super::*;
318361

362+
#[test]
363+
fn test_relay_url_scheme_parse() {
364+
// Valid
365+
assert!(RelayUrlScheme::parse("ws").is_ok());
366+
assert!(RelayUrlScheme::parse("wss").is_ok());
367+
368+
// Invalid
369+
assert_eq!(
370+
RelayUrlScheme::parse("http").unwrap_err(),
371+
Error::UnsupportedScheme
372+
);
373+
assert_eq!(
374+
RelayUrlScheme::parse("https").unwrap_err(),
375+
Error::UnsupportedScheme
376+
);
377+
}
378+
319379
#[test]
320380
fn test_relay_url_valid() {
321381
assert!(RelayUrl::parse("ws://127.0.0.1:7777").is_ok());

0 commit comments

Comments
 (0)