@@ -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 ) ]
5490pub 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 {
316359mod 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