pub struct Url {
pub scheme: Scheme,
pub port: Option<u16>,
pub path: BString,
/* private fields */
}
Expand description
A URL with support for specialized git related capabilities.
Additionally there is support for deserialization and serialization
(see the Display::fmt()
implementation).
Deviation
Note that we do not support passing the password using the URL as it’s likely leading to accidents.
Fields§
§scheme: Scheme
The URL scheme.
port: Option<u16>
The port to use when connecting to a host. If None
, standard ports depending on scheme
will be used.
path: BString
The path portion of the URL, usually the location of the git repository.
Implementations§
source§impl Url
impl Url
Instantiation
source§impl Url
impl Url
Builder
sourcepub fn serialize_alternate_form(self, use_alternate_form: bool) -> Self
pub fn serialize_alternate_form(self, use_alternate_form: bool) -> Self
Enable alternate serialization for this url, e.g. file:///path
becomes /path
.
This is automatically set correctly for parsed URLs, but can be set here for urls created by constructor.
sourcepub fn canonicalize(&mut self) -> Result<(), Error>
pub fn canonicalize(&mut self) -> Result<(), Error>
Turn a file url like file://relative
into file:///root/relative
, hence it assures the url’s path component is absolute.
source§impl Url
impl Url
Access
sourcepub fn path_is_root(&self) -> bool
pub fn path_is_root(&self) -> bool
Returns true if the path portion of the url is /
.
sourcepub fn port_or_default(&self) -> Option<u16>
pub fn port_or_default(&self) -> Option<u16>
Returns the actual or default port for use according to the url scheme. Note that there may be no default port either.
source§impl Url
impl Url
Transformation
sourcepub fn canonicalized(&self) -> Result<Self, Error>
pub fn canonicalized(&self) -> Result<Self, Error>
Turn a file url like file://relative
into file:///root/relative
, hence it assures the url’s path component is absolute.
source§impl Url
impl Url
Serialization
sourcepub fn write_to(&self, out: impl Write) -> Result<()>
pub fn write_to(&self, out: impl Write) -> Result<()>
Write this URL losslessly to out
, ready to be parsed again.
Examples found in repository?
201 202 203 204 205 206 207 208 209 210 211 212
pub fn to_bstring(&self) -> bstr::BString {
let mut buf = Vec::with_capacity(
(5 + 3)
+ self.user.as_ref().map(|n| n.len()).unwrap_or_default()
+ 1
+ self.host.as_ref().map(|h| h.len()).unwrap_or_default()
+ self.port.map(|_| 5).unwrap_or_default()
+ self.path.len(),
);
self.write_to(&mut buf).expect("io cannot fail in memory");
buf.into()
}
sourcepub fn to_bstring(&self) -> BString
pub fn to_bstring(&self) -> BString
Transform ourselves into a binary string, losslessly, or fail if the URL is malformed due to host or user parts being incorrect.
Examples found in repository?
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
pub fn from_parts(
scheme: Scheme,
user: Option<String>,
host: Option<String>,
port: Option<u16>,
path: BString,
) -> Result<Self, parse::Error> {
parse(
Url {
scheme,
user,
host,
port,
path,
serialize_alternative_form: false,
}
.to_bstring()
.as_ref(),
)
}
/// Create a new instance from the given parts, which will be validated by parsing them back from its alternative form.
pub fn from_parts_as_alternative_form(
scheme: Scheme,
user: Option<String>,
host: Option<String>,
port: Option<u16>,
path: BString,
) -> Result<Self, parse::Error> {
parse(
Url {
scheme,
user,
host,
port,
path,
serialize_alternative_form: true,
}
.to_bstring()
.as_ref(),
)
}
source§impl Url
impl Url
Deserialization
sourcepub fn from_bytes(bytes: &BStr) -> Result<Self, Error>
pub fn from_bytes(bytes: &BStr) -> Result<Self, Error>
Parse a URL from bytes
Examples found in repository?
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
fn try_from(value: &str) -> Result<Self, Self::Error> {
Self::from_bytes(value.into())
}
}
impl TryFrom<String> for Url {
type Error = parse::Error;
fn try_from(value: String) -> Result<Self, Self::Error> {
Self::from_bytes(value.as_str().into())
}
}
impl TryFrom<PathBuf> for Url {
type Error = parse::Error;
fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
git_path::into_bstr(value).try_into()
}
}
impl TryFrom<&Path> for Url {
type Error = parse::Error;
fn try_from(value: &Path) -> Result<Self, Self::Error> {
git_path::into_bstr(value).try_into()
}
}
impl TryFrom<&std::ffi::OsStr> for Url {
type Error = parse::Error;
fn try_from(value: &std::ffi::OsStr) -> Result<Self, Self::Error> {
git_path::os_str_into_bstr(value)
.expect("no illformed UTF-8 on Windows")
.try_into()
}
}
impl TryFrom<&BStr> for Url {
type Error = parse::Error;
fn try_from(value: &BStr) -> Result<Self, Self::Error> {
Self::from_bytes(value)
}
Trait Implementations§
source§impl<'de> Deserialize<'de> for Url
impl<'de> Deserialize<'de> for Url
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl Ord for Url
impl Ord for Url
source§impl PartialOrd<Url> for Url
impl PartialOrd<Url> for Url
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more