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 @@
1use crate::{parse, Scheme, Url};
2use bstr::BStr;
3use std::convert::TryFrom;
4use std::path::PathBuf;
5
6impl 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
18impl 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
26impl 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
34impl 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
43impl 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
54impl 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
62impl<'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
8#![deny(rust_2018_idioms, missing_docs)]8#![deny(rust_2018_idioms, missing_docs)]
9#![forbid(unsafe_code)]9#![forbid(unsafe_code)]
10 10
11use std::convert::TryFrom;
12use std::path::PathBuf;
13
14use bstr::{BStr, BString};11use bstr::{BStr, BString};
15 12
16///13///
@@ -43,22 +40,10 @@ pub struct Url { · 26 unmodified lines
43 host: Option<String>,40 host: Option<String>,
44 /// The port to use when connecting to a host. If `None`, standard ports depending on `scheme` will be used.41 /// The port to use when connecting to a host. If `None`, standard ports depending on `scheme` will be used.
45 pub port: Option<u16>,42 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.
47 pub path: bstr::BString,44 pub path: bstr::BString,
48}45}
49 46
50impl 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
62/// Instantiation47/// Instantiation
63impl Url {48impl Url {
64 /// Create a new instance from the given parts, which will be validated by parsing them back.49 /// 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
103 pub fn host(&self) -> Option<&str> {88 pub fn host(&self) -> Option<&str> {
104 self.host.as_deref()89 self.host.as_deref()
105 }90 }
91 /// Returns true if the path portion of the url is `/`.
92 pub fn path_is_root(&self) -> bool {
93 self.path == "/"
94 }
106}95}
107 96
108/// Serialization97/// Serialization
@@ -152,54 +141,4 @@ impl Url { · 43 unmodified lines
152 }141 }
153}142}
154 143
155impl TryFrom<&str> for Url {144mod impls;
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
163impl 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
171impl 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
180impl 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
191impl 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
199impl<'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}