@@ -33,8 +33,6 @@ pub type Uri {
33
33
)
34
34
}
35
35
36
- // Special thanks to Elixir for this algorithm
37
- //
38
36
/// Parses a compliant URI string into the `Uri` Type.
39
37
/// If the string is not a valid URI string then an error is returned.
40
38
///
@@ -48,7 +46,11 @@ pub type Uri {
48
46
/// Ok(Uri(scheme: Some("https"), ...))
49
47
/// ```
50
48
///
51
- pub fn parse ( uri_string : String ) -> Uri {
49
+ pub fn parse ( uri_string : String ) -> Result ( Uri , Nil ) {
50
+ do_parse ( uri_string )
51
+ }
52
+
53
+ fn do_parse ( uri_string : String ) -> Result ( Uri , Nil ) {
52
54
// From https://tools.ietf.org/html/rfc3986#appendix-B
53
55
let pattern =
54
56
// 12 3 4 5 6 7 8
@@ -89,31 +91,34 @@ pub fn parse(uri_string: String) -> Uri {
89
91
|> result . map ( pair . second )
90
92
|> option . from_result
91
93
let port = case port {
92
- None ->
93
- case scheme {
94
- Some ( "ftp" ) -> Some ( 21 )
95
- Some ( "sftp" ) -> Some ( 22 )
96
- Some ( "tftp" ) -> Some ( 69 )
97
- Some ( "http" ) -> Some ( 80 )
98
- Some ( "https" ) -> Some ( 443 )
99
- Some ( "ldap" ) -> Some ( 389 )
100
- _ -> None
101
- }
94
+ None -> default_port ( scheme )
102
95
_ -> port
103
96
}
104
97
let scheme =
105
98
scheme
106
99
|> noneify_empty_string
107
100
|> option . map ( string . lowercase )
108
- Uri (
101
+ Ok ( Uri (
109
102
scheme : scheme ,
110
103
userinfo : userinfo ,
111
104
host : host ,
112
105
port : port ,
113
106
path : path ,
114
107
query : query ,
115
108
fragment : fragment ,
116
- )
109
+ ) )
110
+ }
111
+
112
+ fn default_port ( scheme : Option ( String ) ) -> Option ( Int ) {
113
+ case scheme {
114
+ Some ( "ftp" ) -> Some ( 21 )
115
+ Some ( "sftp" ) -> Some ( 22 )
116
+ Some ( "tftp" ) -> Some ( 69 )
117
+ Some ( "http" ) -> Some ( 80 )
118
+ Some ( "https" ) -> Some ( 443 )
119
+ Some ( "ldap" ) -> Some ( 389 )
120
+ _ -> None
121
+ }
117
122
}
118
123
119
124
fn regex_submatches ( pattern : String , string : String ) -> List ( Option ( String ) ) {
0 commit comments