This could also be considered an empty path depending on the context which is what makes it useful.
Showing the changes to git-url/src (2 of 3 files). Show all
Unified · Split
| @@ -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 | +} | |
| @@ -8,9 +8,6 @@ · 7 unmodified lines | ||
| 8 | 8 | #![deny(rust_2018_idioms, missing_docs)] |
| 9 | 9 | #![forbid(unsafe_code)] |
| 10 | 10 | |
| 11 | −use std::convert::TryFrom; | |
| 12 | −use std::path::PathBuf; | |
| 13 | − | |
| 14 | 11 | use bstr::{BStr, BString}; |
| 15 | 12 | |
| 16 | 13 | /// |
| @@ -43,22 +40,10 @@ pub struct Url { · 26 unmodified lines | ||
| 43 | 40 | host: Option<String>, |
| 44 | 41 | /// The port to use when connecting to a host. If `None`, standard ports depending on `scheme` will be used. |
| 45 | 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 | 44 | pub path: bstr::BString, |
| 48 | 45 | } |
| 49 | 46 | |
| 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 | − | |
| 62 | 47 | /// Instantiation |
| 63 | 48 | impl Url { |
| 64 | 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 | 88 | pub fn host(&self) -> Option<&str> { |
| 104 | 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 | 97 | /// Serialization |
| @@ -152,54 +141,4 @@ impl Url { · 43 unmodified lines | ||
| 152 | 141 | } |
| 153 | 142 | } |
| 154 | 143 | |
| 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; | |