| @@ -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 | |
| 11 | use std::convert::TryFrom; | | |
| 12 | use std::path::PathBuf; | | |
| 13 | | | |
| 14 | use bstr::{BStr, BString}; | 11 | use 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 | |
| 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 | /// Instantiation | 47 | /// Instantiation |
| 63 | impl Url { | 48 | impl 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 | /// Serialization | 97 | /// Serialization |
| @@ -152,54 +141,4 @@ impl Url { · 43 unmodified lines |
| 152 | } | 141 | } |
| 153 | } | 142 | } |
| 154 | | 143 | |
| 155 | impl TryFrom<&str> for Url { | 144 | mod 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 | | | |
| 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 | } | | |