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

Skip to content

Commit fbe75c9

Browse files
committed
feat: Url::path_is_root() to determine if the path is /. (#450)
This could also be considered an empty path depending on the context which is what makes it useful.
1 parent 1b19611 commit fbe75c9

3 files changed

Lines changed: 87 additions & 68 deletions

File tree

git-url/src/impls.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use crate::{parse, Scheme, Url};
2+
use bstr::BStr;
3+
use std::convert::TryFrom;
4+
use std::path::PathBuf;
5+
6+
impl Default for Url {
7+
fn default() -> Self {
8+
Url {
9+
scheme: Scheme::Ssh,
10+
user: None,
11+
host: None,
12+
port: None,
13+
path: bstr::BString::default(),
14+
}
15+
}
16+
}
17+
18+
impl TryFrom<&str> for Url {
19+
type Error = parse::Error;
20+
21+
fn try_from(value: &str) -> Result<Self, Self::Error> {
22+
Self::from_bytes(value.into())
23+
}
24+
}
25+
26+
impl TryFrom<String> for Url {
27+
type Error = parse::Error;
28+
29+
fn try_from(value: String) -> Result<Self, Self::Error> {
30+
Self::from_bytes(value.as_str().into())
31+
}
32+
}
33+
34+
impl TryFrom<PathBuf> for Url {
35+
type Error = parse::Error;
36+
37+
fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
38+
use std::convert::TryInto;
39+
git_path::into_bstr(value).try_into()
40+
}
41+
}
42+
43+
impl TryFrom<&std::ffi::OsStr> for Url {
44+
type Error = parse::Error;
45+
46+
fn try_from(value: &std::ffi::OsStr) -> Result<Self, Self::Error> {
47+
use std::convert::TryInto;
48+
git_path::os_str_into_bstr(value)
49+
.expect("no illformed UTF-8 on Windows")
50+
.try_into()
51+
}
52+
}
53+
54+
impl TryFrom<&BStr> for Url {
55+
type Error = parse::Error;
56+
57+
fn try_from(value: &BStr) -> Result<Self, Self::Error> {
58+
Self::from_bytes(value)
59+
}
60+
}
61+
62+
impl<'a> TryFrom<std::borrow::Cow<'a, BStr>> for Url {
63+
type Error = parse::Error;
64+
65+
fn try_from(value: std::borrow::Cow<'a, BStr>) -> Result<Self, Self::Error> {
66+
Self::try_from(&*value)
67+
}
68+
}

git-url/src/lib.rs

Lines changed: 6 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
#![deny(rust_2018_idioms, missing_docs)]
99
#![forbid(unsafe_code)]
1010

11-
use std::convert::TryFrom;
12-
use std::path::PathBuf;
13-
1411
use bstr::{BStr, BString};
1512

1613
///
@@ -43,22 +40,10 @@ pub struct Url {
4340
host: Option<String>,
4441
/// The port to use when connecting to a host. If `None`, standard ports depending on `scheme` will be used.
4542
pub port: Option<u16>,
46-
/// The path portion of the URL, usually the location of the git repository, and at least `/`
43+
/// The path portion of the URL, usually the location of the git repository.
4744
pub path: bstr::BString,
4845
}
4946

50-
impl Default for Url {
51-
fn default() -> Self {
52-
Url {
53-
scheme: Scheme::Ssh,
54-
user: None,
55-
host: None,
56-
port: None,
57-
path: bstr::BString::default(),
58-
}
59-
}
60-
}
61-
6247
/// Instantiation
6348
impl Url {
6449
/// Create a new instance from the given parts, which will be validated by parsing them back.
@@ -103,6 +88,10 @@ impl Url {
10388
pub fn host(&self) -> Option<&str> {
10489
self.host.as_deref()
10590
}
91+
/// Returns true if the path portion of the url is `/`.
92+
pub fn path_is_root(&self) -> bool {
93+
self.path == "/"
94+
}
10695
}
10796

10897
/// Serialization
@@ -152,54 +141,4 @@ impl Url {
152141
}
153142
}
154143

155-
impl TryFrom<&str> for Url {
156-
type Error = parse::Error;
157-
158-
fn try_from(value: &str) -> Result<Self, Self::Error> {
159-
Self::from_bytes(value.into())
160-
}
161-
}
162-
163-
impl TryFrom<String> for Url {
164-
type Error = parse::Error;
165-
166-
fn try_from(value: String) -> Result<Self, Self::Error> {
167-
Self::from_bytes(value.as_str().into())
168-
}
169-
}
170-
171-
impl TryFrom<PathBuf> for Url {
172-
type Error = parse::Error;
173-
174-
fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
175-
use std::convert::TryInto;
176-
git_path::into_bstr(value).try_into()
177-
}
178-
}
179-
180-
impl TryFrom<&std::ffi::OsStr> for Url {
181-
type Error = parse::Error;
182-
183-
fn try_from(value: &std::ffi::OsStr) -> Result<Self, Self::Error> {
184-
use std::convert::TryInto;
185-
git_path::os_str_into_bstr(value)
186-
.expect("no illformed UTF-8 on Windows")
187-
.try_into()
188-
}
189-
}
190-
191-
impl TryFrom<&BStr> for Url {
192-
type Error = parse::Error;
193-
194-
fn try_from(value: &BStr) -> Result<Self, Self::Error> {
195-
Self::from_bytes(value)
196-
}
197-
}
198-
199-
impl<'a> TryFrom<std::borrow::Cow<'a, BStr>> for Url {
200-
type Error = parse::Error;
201-
202-
fn try_from(value: std::borrow::Cow<'a, BStr>) -> Result<Self, Self::Error> {
203-
Self::try_from(&*value)
204-
}
205-
}
144+
mod impls;

git-url/tests/parse/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1+
use bstr::ByteSlice;
12
use git_url::Scheme;
23

34
fn assert_url_and(url: &str, expected: git_url::Url) -> Result<git_url::Url, crate::Error> {
4-
assert_eq!(git_url::parse(url.into())?, expected);
5+
let actual = git_url::parse(url.into())?;
6+
assert_eq!(actual, expected);
7+
if actual.scheme.as_str().starts_with("http") {
8+
assert!(
9+
actual.path.starts_with_str("/"),
10+
"paths are never empty and at least '/': {:?}",
11+
actual.path
12+
);
13+
if actual.path.len() < 2 {
14+
assert!(actual.path_is_root())
15+
}
16+
}
517
Ok(expected)
618
}
719

0 commit comments

Comments
 (0)