History

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.

Sebastian Thiel committed 4 years ago

Commit fbe75c91 parent 1b196113 files · +87 −68Browse files

Showing the changes to git-url/src (2 of 3 files). Show all

Unified · Split

2 files
  1. git-url/src/impls.rs +68 −0
  2. git-url/src/lib.rs +6 −67
git-url/src/impls.rs+68−0
@@ -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+6−67
@@ -8,9 +8,6 @@ · 7 unmodified lines
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 { · 26 unmodified lines
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 { · 38 unmodified lines
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 { · 43 unmodified lines
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;