Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 37459dc

Browse files
committed
[url] first stab at expanding paths with user names
1 parent 50acab7 commit 37459dc

4 files changed

Lines changed: 50 additions & 8 deletions

File tree

git-url/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ pub enum Protocol {
1212

1313
pub mod owned {
1414
use crate::Protocol;
15+
use bstr::ByteSlice;
16+
use std::{
17+
borrow::Cow,
18+
path::{Path, PathBuf},
19+
};
1520

1621
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
1722
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
@@ -43,6 +48,25 @@ pub mod owned {
4348
}
4449
}
4550
}
51+
52+
impl Url {
53+
pub fn expand_path_with(
54+
&self,
55+
home_for_user: impl FnOnce(&UserExpansion) -> Option<PathBuf>,
56+
) -> Option<PathBuf> {
57+
fn make_relative(path: &Path) -> Cow<Path> {
58+
if path.is_relative() {
59+
return path.into();
60+
}
61+
path.components().skip(1).collect::<PathBuf>().into()
62+
}
63+
match self.expand_user.as_ref() {
64+
Some(user) => home_for_user(user)
65+
.and_then(|base| self.path.to_path().ok().map(|path| base.join(make_relative(path)))),
66+
None => self.path.to_path().ok().map(ToOwned::to_owned),
67+
}
68+
}
69+
}
4670
}
4771

4872
#[doc(inline)]

git-url/tests/parse/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
use git_url::{owned::UserExpansion, Protocol};
22

3-
fn assert_url(url: &str, expected: git_url::Owned) -> crate::Result {
3+
fn assert_url_and(url: &str, expected: git_url::Owned) -> Result<git_url::Owned, crate::Error> {
44
assert_eq!(git_url::parse(url.as_bytes())?, expected);
5-
Ok(())
5+
Ok(expected)
6+
}
7+
8+
fn assert_url(url: &str, expected: git_url::Owned) -> crate::Result {
9+
assert_url_and(url, expected).map(|_| ())
610
}
711

812
fn assert_failure(url: &str, expected_err: &str) {

git-url/tests/parse/ssh.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use crate::parse::{assert_url, url};
1+
use crate::parse::{assert_url, assert_url_and, url};
22
use git_url::owned::UserExpansion;
33
use git_url::Protocol;
4+
use std::path::Path;
45

56
#[test]
67
fn without_user_and_without_port() -> crate::Result {
@@ -25,7 +26,7 @@ fn host_is_ipv4() -> crate::Result {
2526

2627
#[test]
2728
fn username_expansion_with_username() -> crate::Result {
28-
assert_url(
29+
let expanded_path = assert_url_and(
2930
"ssh://example.com/~byron/hello",
3031
url(
3132
Protocol::Ssh,
@@ -35,12 +36,18 @@ fn username_expansion_with_username() -> crate::Result {
3536
b"/hello",
3637
UserExpansion::Name("byron".into()),
3738
),
38-
)
39+
)?
40+
.expand_path_with(|user: &UserExpansion| match user {
41+
UserExpansion::Current => unreachable!("we have a name"),
42+
UserExpansion::Name(name) => Some(format!("/home/{}", name).into()),
43+
});
44+
assert_eq!(expanded_path, Some(Path::new("/home/byron/hello").into()));
45+
Ok(())
3946
}
4047

4148
#[test]
4249
fn username_expansion_without_username() -> crate::Result {
43-
assert_url(
50+
let expanded_path = assert_url_and(
4451
"ssh://example.com/~/hello/git",
4552
url(
4653
Protocol::Ssh,
@@ -50,7 +57,13 @@ fn username_expansion_without_username() -> crate::Result {
5057
b"/hello/git",
5158
UserExpansion::Current,
5259
),
53-
)
60+
)?
61+
.expand_path_with(|user: &UserExpansion| match user {
62+
UserExpansion::Current => Some("/home/byron".into()),
63+
UserExpansion::Name(name) => Some(format!("/home/{}", name).into()),
64+
});
65+
assert_eq!(expanded_path, Some(Path::new("/home/byron/hello/git").into()));
66+
Ok(())
5467
}
5568

5669
#[test]

git-url/tests/url.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
pub type Result = std::result::Result<(), Box<dyn std::error::Error>>;
1+
pub type Error = Box<dyn std::error::Error>;
2+
pub type Result = std::result::Result<(), Error>;
23

34
mod parse;

0 commit comments

Comments
 (0)