Skip to content

Commit e318a4c

Browse files
committed
refactor
1 parent 4b4ac8a commit e318a4c

File tree

4 files changed

+250
-428
lines changed

4 files changed

+250
-428
lines changed

gix-url/src/expand_path.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl From<ForUser> for Option<BString> {
2222
}
2323
}
2424

25-
/// The error used by [`parse()`], [`with()`] and [`expand_path()`].
25+
/// The error used by [`parse()`], [`with()`] and [`expand_path()`](crate::expand_path()).
2626
#[derive(Debug, thiserror::Error)]
2727
#[allow(missing_docs)]
2828
pub enum Error {
@@ -112,16 +112,3 @@ pub fn with(
112112
None => path.into(),
113113
})
114114
}
115-
116-
/// Expand `path` for the given `user`, which can be obtained by [`parse()`], resolving the home directories
117-
/// of `user` automatically.
118-
///
119-
/// If more precise control of the resolution mechanism is needed, then use the [`with()`] function.
120-
pub fn expand_path(user: Option<&ForUser>, path: &BStr) -> Result<PathBuf, Error> {
121-
with(user, path, |user| match user {
122-
ForUser::Current => home::home_dir(),
123-
ForUser::Name(user) => {
124-
home::home_dir().and_then(|home| home.parent().map(|home_dirs| home_dirs.join(user.to_string())))
125-
}
126-
})
127-
}

gix-url/src/lib.rs

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,58 @@
1010

1111
use bstr::{BStr, BString};
1212
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;
2014

2115
///
2216
pub mod expand_path;
23-
#[doc(inline)]
24-
pub use expand_path::expand_path;
2517

2618
mod scheme;
2719
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+
}
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))]
3967
pub 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
92120
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.
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)]
262289
pub 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

Comments
 (0)