const DEFAULT_HOST: &str = "localhost";
const DEFAULT_PORT: u16 = 6379;
pub struct Url(url::Url);
impl Url {
pub fn parse(url: &str) -> crate::Result<Self> {
Ok(Url(url::Url::parse(url)?))
}
#[inline]
pub(crate) fn scheme(&self) -> &str {
self.0.scheme()
}
#[inline]
pub fn host(&self) -> &str {
let host = self.0.host_str().unwrap_or(DEFAULT_HOST);
if host.is_empty() {
DEFAULT_HOST
} else {
host
}
}
#[inline]
pub fn port(&self) -> u16 {
self.0.port().unwrap_or(DEFAULT_PORT)
}
#[inline]
pub fn database(&self) -> &str {
if self.0.path() == "/" {
"0"
} else {
&self.0.path()[1..]
}
}
#[inline]
pub fn password(&self) -> Option<&str> {
self.0.password()
}
}