@@ -67,15 +67,6 @@ pub struct GitUrl {
67
67
hint : GitUrlParseHint ,
68
68
}
69
69
70
- /// Build the printable GitUrl from its components
71
- impl fmt:: Display for GitUrl {
72
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
73
- let git_url_str = self . display ( ) ;
74
-
75
- write ! ( f, "{git_url_str}" , )
76
- }
77
- }
78
-
79
70
impl GitUrl {
80
71
/// scheme name (i.e. `scheme://`)
81
72
pub fn scheme ( & self ) -> Option < & str > {
@@ -130,7 +121,7 @@ impl GitUrl {
130
121
}
131
122
132
123
/// This method rebuilds the printable GitUrl from its components.
133
- /// `url_compat` results in output that can be parsed by the `url` crate
124
+ /// `url_compat` results in output that can be parsed by the [ `url`](https://docs.rs/url/latest/url/) crate
134
125
fn build_string ( & self , url_compat : bool ) -> String {
135
126
let scheme = if self . print_scheme ( ) || url_compat {
136
127
if let Some ( scheme) = self . scheme ( ) {
@@ -176,27 +167,7 @@ impl GitUrl {
176
167
let git_url_str = format ! ( "{scheme}{auth_info}{host}{port}{path}" ) ;
177
168
git_url_str
178
169
}
179
- }
180
-
181
- #[ cfg( feature = "url" ) ]
182
- impl TryFrom < & GitUrl > for Url {
183
- type Error = url:: ParseError ;
184
- fn try_from ( value : & GitUrl ) -> Result < Self , Self :: Error > {
185
- // Since we don't fully implement any spec, we'll rely on the url crate
186
- Url :: parse ( & value. url_compat_display ( ) )
187
- }
188
- }
189
-
190
- #[ cfg( feature = "url" ) ]
191
- impl TryFrom < GitUrl > for Url {
192
- type Error = url:: ParseError ;
193
- fn try_from ( value : GitUrl ) -> Result < Self , Self :: Error > {
194
- // Since we don't fully implement any spec, we'll rely on the url crate
195
- Url :: parse ( & value. url_compat_display ( ) )
196
- }
197
- }
198
170
199
- impl GitUrl {
200
171
/// Returns `GitUrl` after removing all user info values
201
172
pub fn trim_auth ( & self ) -> GitUrl {
202
173
let mut new_giturl = self . clone ( ) ;
@@ -219,8 +190,16 @@ impl GitUrl {
219
190
/// # }
220
191
/// ```
221
192
pub fn parse ( input : & str ) -> Result < Self , GitUrlParseError > {
222
- let mut git_url_result = GitUrl :: default ( ) ;
193
+ let git_url = Self :: parse_to_git_url ( input ) ? ;
223
194
195
+ git_url. is_valid ( ) ?;
196
+
197
+ Ok ( git_url)
198
+ }
199
+
200
+ /// Internal parse to `GitUrl` without validation steps
201
+ fn parse_to_git_url ( input : & str ) -> Result < Self , GitUrlParseError > {
202
+ let mut git_url_result = GitUrl :: default ( ) ;
224
203
// Error if there are null bytes within the url
225
204
// https://github.com/tjtelan/git-url-parse-rs/issues/16
226
205
if input. contains ( '\0' ) {
@@ -294,6 +273,31 @@ impl GitUrl {
294
273
Ok ( git_url_result)
295
274
}
296
275
276
+ /// Normalize input into form that can be used by [`Url::parse`](https://docs.rs/url/latest/url/struct.Url.html#method.parse)
277
+ ///
278
+ /// ```
279
+ /// use git_url_parse::GitUrl;
280
+ /// #[cfg(feature = "url")]
281
+ /// use url::Url;
282
+ ///
283
+ /// fn main() -> Result<(), git_url_parse::GitUrlParseError> {
284
+ /// let ssh_url = GitUrl::parse_to_url("[email protected] :tjtelan/git-url-parse-rs.git")?;
285
+ ///
286
+ /// assert_eq!(ssh_url.scheme(), "ssh");
287
+ /// assert_eq!(ssh_url.username(), "git");
288
+ /// assert_eq!(ssh_url.host_str(), Some("github.com"));
289
+ /// assert_eq!(ssh_url.path(), "/tjtelan/git-url-parse-rs.git");
290
+ /// Ok(())
291
+ /// }
292
+ /// ```
293
+ ///
294
+ #[ cfg( feature = "url" ) ]
295
+ pub fn parse_to_url ( input : & str ) -> Result < Url , GitUrlParseError > {
296
+ let git_url = Self :: parse_to_git_url ( input) ?;
297
+
298
+ Ok ( Url :: try_from ( git_url) ?)
299
+ }
300
+
297
301
/// ```
298
302
/// use git_url_parse::GitUrl;
299
303
/// use git_url_parse::types::provider::GenericProvider;
@@ -380,3 +384,46 @@ impl GitUrl {
380
384
Ok ( ( ) )
381
385
}
382
386
}
387
+
388
+ /// Build the printable GitUrl from its components
389
+ impl fmt:: Display for GitUrl {
390
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
391
+ let git_url_str = self . display ( ) ;
392
+
393
+ write ! ( f, "{git_url_str}" , )
394
+ }
395
+ }
396
+
397
+ #[ cfg( feature = "url" ) ]
398
+ impl TryFrom < & GitUrl > for Url {
399
+ type Error = url:: ParseError ;
400
+ fn try_from ( value : & GitUrl ) -> Result < Self , Self :: Error > {
401
+ // Since we don't fully implement any spec, we'll rely on the url crate
402
+ Url :: parse ( & value. url_compat_display ( ) )
403
+ }
404
+ }
405
+
406
+ #[ cfg( feature = "url" ) ]
407
+ impl TryFrom < GitUrl > for Url {
408
+ type Error = url:: ParseError ;
409
+ fn try_from ( value : GitUrl ) -> Result < Self , Self :: Error > {
410
+ // Since we don't fully implement any spec, we'll rely on the url crate
411
+ Url :: parse ( & value. url_compat_display ( ) )
412
+ }
413
+ }
414
+
415
+ #[ cfg( feature = "url" ) ]
416
+ impl TryFrom < & Url > for GitUrl {
417
+ type Error = GitUrlParseError ;
418
+ fn try_from ( value : & Url ) -> Result < Self , Self :: Error > {
419
+ GitUrl :: parse ( value. as_str ( ) )
420
+ }
421
+ }
422
+
423
+ #[ cfg( feature = "url" ) ]
424
+ impl TryFrom < Url > for GitUrl {
425
+ type Error = GitUrlParseError ;
426
+ fn try_from ( value : Url ) -> Result < Self , Self :: Error > {
427
+ GitUrl :: parse ( value. as_str ( ) )
428
+ }
429
+ }
0 commit comments