1010
1111use bstr:: { BStr , BString } ;
1212use 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 ;
2014
2115///
2216pub mod expand_path;
23- #[ doc( inline) ]
24- pub use expand_path:: expand_path;
2517
2618mod scheme;
2719pub 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+ }
2855
2956/// A URL with support for specialized git related capabilities.
3057///
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_).
3360///
34- /// # Deviation
61+ /// # Security Warning
3562///
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()).
3765#[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone ) ]
3866#[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
3967pub struct Url {
@@ -57,7 +85,7 @@ pub struct Url {
5785 /// the invocation of programs from an attacker controlled URL. See <https://secure.phabricator.com/T12961> for details.
5886 ///
5987 /// 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 ,
6189}
6290
6391/// Instantiation
@@ -90,7 +118,7 @@ impl Url {
90118
91119/// Modification
92120impl 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.
94122 pub fn set_user ( & mut self , user : Option < String > ) -> Option < String > {
95123 let prev = self . user . take ( ) ;
96124 self . user = user;
@@ -230,7 +258,7 @@ impl Url {
230258 }
231259
232260 /// 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 {
234262 let mut buf = Vec :: with_capacity (
235263 ( 5 + 3 )
236264 + self . user . as_ref ( ) . map ( String :: len) . unwrap_or_default ( )
@@ -252,19 +280,18 @@ impl Url {
252280 }
253281}
254282
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
258284/// for testing code. Do not use this module in production! For all intends and purposes the APIs of
259285/// all functions and types exposed by this module are considered unstable and are allowed to break
260286/// even in patch releases!
261287#[ doc( hidden) ]
288+ #[ cfg( debug_assertions) ]
262289pub mod testing {
263290 use bstr:: BString ;
264291
265292 use crate :: { Scheme , Url } ;
266293
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.
268295 pub trait TestUrlExtension {
269296 /// Create a new instance from the given parts without validating them.
270297 ///
@@ -278,7 +305,7 @@ pub mod testing {
278305 port : Option < u16 > ,
279306 path : BString ,
280307 serialize_alternative_form : bool ,
281- ) -> crate :: Url {
308+ ) -> Url {
282309 Url {
283310 scheme,
284311 user,
@@ -291,5 +318,5 @@ pub mod testing {
291318 }
292319 }
293320
294- impl TestUrlExtension for crate :: Url { }
321+ impl TestUrlExtension for Url { }
295322}
0 commit comments