10
10
11
11
use bstr:: { BStr , BString } ;
12
12
use std:: borrow:: Cow ;
13
-
14
- ///
15
- #[ path = "parse/mod.rs" ]
16
- // #[path = "parse.rs"]
17
- pub mod parse;
18
- #[ doc( inline) ]
19
- pub use parse:: parse;
13
+ use std:: path:: PathBuf ;
20
14
21
15
///
22
16
pub mod expand_path;
23
- #[ doc( inline) ]
24
- pub use expand_path:: expand_path;
25
17
26
18
mod scheme;
27
19
pub use scheme:: Scheme ;
20
+ mod impls;
21
+
22
+ ///
23
+ pub mod parse;
24
+
25
+ /// Parse the given `bytes` as a [git url](Url).
26
+ ///
27
+ /// # Note
28
+ ///
29
+ /// We cannot and should never have to deal with UTF-16 encoded windows strings, so bytes input is acceptable.
30
+ /// For file-paths, we don't expect UTF8 encoding either.
31
+ pub fn parse ( input : & BStr ) -> Result < Url , parse:: Error > {
32
+ use parse:: InputScheme ;
33
+ match parse:: find_scheme ( input) {
34
+ InputScheme :: Local => parse:: local ( input) ,
35
+ InputScheme :: Url { protocol_end } if input[ ..protocol_end] . eq_ignore_ascii_case ( b"file" ) => {
36
+ parse:: file_url ( input, protocol_end)
37
+ }
38
+ InputScheme :: Url { .. } => parse:: url ( input) ,
39
+ InputScheme :: Scp { colon } => parse:: scp ( input, colon) ,
40
+ }
41
+ }
42
+
43
+ /// Expand `path` for the given `user`, which can be obtained by [`parse()`], resolving the home directories
44
+ /// of `user` automatically.
45
+ ///
46
+ /// If more precise control of the resolution mechanism is needed, then use the [expand_path::with()] function.
47
+ pub fn expand_path ( user : Option < & expand_path:: ForUser > , path : & BStr ) -> Result < PathBuf , expand_path:: Error > {
48
+ expand_path:: with ( user, path, |user| match user {
49
+ expand_path:: ForUser :: Current => home:: home_dir ( ) ,
50
+ expand_path:: ForUser :: Name ( user) => {
51
+ home:: home_dir ( ) . and_then ( |home| home. parent ( ) . map ( |home_dirs| home_dirs. join ( user. to_string ( ) ) ) )
52
+ }
53
+ } )
54
+ }
28
55
29
56
/// A URL with support for specialized git related capabilities.
30
57
///
31
- /// Additionally there is support for [deserialization][ Url::from_bytes()] and serialization
32
- /// (_see the ` Display::fmt()` implementation_).
58
+ /// Additionally there is support for [deserialization]( Url::from_bytes()) and serialization
59
+ /// (_see the [`std::fmt:: Display::fmt()`] implementation_).
33
60
///
34
- /// # Deviation
61
+ /// # Security Warning
35
62
///
36
- /// Note that we do not support passing the password using the URL as it's likely leading to accidents.
63
+ /// URLs may contain passwords and we serialize them when [formatting](std::fmt::Display) or
64
+ /// [serializing losslessly](Url::to_bstring()).
37
65
#[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone ) ]
38
66
#[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
39
67
pub struct Url {
@@ -57,7 +85,7 @@ pub struct Url {
57
85
/// the invocation of programs from an attacker controlled URL. See <https://secure.phabricator.com/T12961> for details.
58
86
///
59
87
/// If this value is going to be used in a command-line application, call [Self::path_argument_safe()] instead.
60
- pub path : bstr :: BString ,
88
+ pub path : BString ,
61
89
}
62
90
63
91
/// Instantiation
@@ -90,7 +118,7 @@ impl Url {
90
118
91
119
/// Modification
92
120
impl Url {
93
- /// Set the given `user`, with `None` unsetting it. Returns the previous value.
121
+ /// Set the given `user`, or unset it with `None`. Return the previous value.
94
122
pub fn set_user ( & mut self , user : Option < String > ) -> Option < String > {
95
123
let prev = self . user . take ( ) ;
96
124
self . user = user;
@@ -230,7 +258,7 @@ impl Url {
230
258
}
231
259
232
260
/// Transform ourselves into a binary string, losslessly, or fail if the URL is malformed due to host or user parts being incorrect.
233
- pub fn to_bstring ( & self ) -> bstr :: BString {
261
+ pub fn to_bstring ( & self ) -> BString {
234
262
let mut buf = Vec :: with_capacity (
235
263
( 5 + 3 )
236
264
+ self . user . as_ref ( ) . map ( String :: len) . unwrap_or_default ( )
@@ -252,19 +280,18 @@ impl Url {
252
280
}
253
281
}
254
282
255
- mod impls;
256
-
257
- /// This module contains extensions to the [crate::Url] struct which are only intended to be used
283
+ /// This module contains extensions to the [Url] struct which are only intended to be used
258
284
/// for testing code. Do not use this module in production! For all intends and purposes the APIs of
259
285
/// all functions and types exposed by this module are considered unstable and are allowed to break
260
286
/// even in patch releases!
261
287
#[ doc( hidden) ]
288
+ #[ cfg( debug_assertions) ]
262
289
pub mod testing {
263
290
use bstr:: BString ;
264
291
265
292
use crate :: { Scheme , Url } ;
266
293
267
- /// Additional functions for [crate:: Url] which are only intended to be used for tests.
294
+ /// Additional functions for [Url] which are only intended to be used for tests.
268
295
pub trait TestUrlExtension {
269
296
/// Create a new instance from the given parts without validating them.
270
297
///
@@ -278,7 +305,7 @@ pub mod testing {
278
305
port : Option < u16 > ,
279
306
path : BString ,
280
307
serialize_alternative_form : bool ,
281
- ) -> crate :: Url {
308
+ ) -> Url {
282
309
Url {
283
310
scheme,
284
311
user,
@@ -291,5 +318,5 @@ pub mod testing {
291
318
}
292
319
}
293
320
294
- impl TestUrlExtension for crate :: Url { }
321
+ impl TestUrlExtension for Url { }
295
322
}
0 commit comments