@@ -78,6 +78,33 @@ impl From<c_uint> for HostType {
78
78
}
79
79
}
80
80
81
+ /// Defines the scheme type of the url.
82
+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
83
+ pub enum SchemeType {
84
+ Http = 0 ,
85
+ NotSpecial = 1 ,
86
+ Https = 2 ,
87
+ Ws = 3 ,
88
+ Ftp = 4 ,
89
+ Wss = 5 ,
90
+ File = 6 ,
91
+ }
92
+
93
+ impl From < c_uint > for SchemeType {
94
+ fn from ( value : c_uint ) -> Self {
95
+ match value {
96
+ 0 => SchemeType :: Http ,
97
+ 1 => SchemeType :: NotSpecial ,
98
+ 2 => SchemeType :: Https ,
99
+ 3 => SchemeType :: Ws ,
100
+ 4 => SchemeType :: Ftp ,
101
+ 5 => SchemeType :: Wss ,
102
+ 6 => SchemeType :: File ,
103
+ _ => SchemeType :: NotSpecial ,
104
+ }
105
+ }
106
+ }
107
+
81
108
/// Components are a serialization-free representation of a URL.
82
109
/// For usages where string serialization has a high cost, you can
83
110
/// use url components with `href` attribute.
@@ -224,6 +251,11 @@ impl Url {
224
251
HostType :: from ( unsafe { ffi:: ada_get_host_type ( self . 0 ) } )
225
252
}
226
253
254
+ /// Returns the type of the scheme such as http, https, etc.
255
+ pub fn scheme_type ( & self ) -> SchemeType {
256
+ SchemeType :: from ( unsafe { ffi:: ada_get_scheme_type ( self . 0 ) } )
257
+ }
258
+
227
259
/// Return the origin of this URL
228
260
///
229
261
/// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-origin)
@@ -907,6 +939,8 @@ mod test {
907
939
"https://username:[email protected] :9090/search?query#hash"
908
940
) ;
909
941
942
+ assert_eq ! ( out. scheme_type( ) , SchemeType :: Https ) ;
943
+
910
944
out. set_username ( Some ( "new-username" ) ) . unwrap ( ) ;
911
945
assert_eq ! ( out. username( ) , "new-username" ) ;
912
946
@@ -937,6 +971,7 @@ mod test {
937
971
938
972
out. set_protocol ( "wss" ) . unwrap ( ) ;
939
973
assert_eq ! ( out. protocol( ) , "wss:" ) ;
974
+ assert_eq ! ( out. scheme_type( ) , SchemeType :: Wss ) ;
940
975
941
976
assert ! ( out. has_credentials( ) ) ;
942
977
assert ! ( out. has_non_empty_username( ) ) ;
@@ -948,6 +983,52 @@ mod test {
948
983
assert_eq ! ( out. host_type( ) , HostType :: Domain ) ;
949
984
}
950
985
986
+ #[ test]
987
+ fn scheme_types ( ) {
988
+ assert_eq ! (
989
+ Url :: parse( "file:///foo/bar" , None )
990
+ . expect( "bad url" )
991
+ . scheme_type( ) ,
992
+ SchemeType :: File
993
+ ) ;
994
+ assert_eq ! (
995
+ Url :: parse( "ws://example.com/ws" , None )
996
+ . expect( "bad url" )
997
+ . scheme_type( ) ,
998
+ SchemeType :: Ws
999
+ ) ;
1000
+ assert_eq ! (
1001
+ Url :: parse( "wss://example.com/wss" , None )
1002
+ . expect( "bad url" )
1003
+ . scheme_type( ) ,
1004
+ SchemeType :: Wss
1005
+ ) ;
1006
+ assert_eq ! (
1007
+ Url :: parse( "ftp://example.com/file.txt" , None )
1008
+ . expect( "bad url" )
1009
+ . scheme_type( ) ,
1010
+ SchemeType :: Ftp
1011
+ ) ;
1012
+ assert_eq ! (
1013
+ Url :: parse( "http://example.com/file.txt" , None )
1014
+ . expect( "bad url" )
1015
+ . scheme_type( ) ,
1016
+ SchemeType :: Http
1017
+ ) ;
1018
+ assert_eq ! (
1019
+ Url :: parse( "https://example.com/file.txt" , None )
1020
+ . expect( "bad url" )
1021
+ . scheme_type( ) ,
1022
+ SchemeType :: Https
1023
+ ) ;
1024
+ assert_eq ! (
1025
+ Url :: parse( "foo://example.com" , None )
1026
+ . expect( "bad url" )
1027
+ . scheme_type( ) ,
1028
+ SchemeType :: NotSpecial
1029
+ ) ;
1030
+ }
1031
+
951
1032
#[ test]
952
1033
fn can_parse_simple_url ( ) {
953
1034
assert ! ( Url :: can_parse( "https://google.com" , None ) ) ;
0 commit comments