#[derive(Debug, Eq, Clone, Copy, PartialEq, Hash)]
pub enum UriType {
Uri,
UriNoAuthority,
UriCannotBeABase,
NetworkPath,
AbsolutePath,
RelativePath,
Query,
Fragment,
}
impl UriType {
pub fn can_borrow_as_uri(&self) -> bool {
match self {
UriType::Uri
| UriType::UriNoAuthority
| UriType::NetworkPath
| UriType::UriCannotBeABase => true,
_ => false,
}
}
pub fn can_borrow_as_rel_ref(&self) -> bool {
!self.can_borrow_as_uri()
}
pub fn has_absolute_path(&self) -> bool {
match self {
UriType::Uri
| UriType::UriNoAuthority
| UriType::AbsolutePath
| UriType::NetworkPath => true,
_ => false,
}
}
pub fn is_ietf_rfc3986_relative_reference(&self) -> bool {
match self {
UriType::Uri | UriType::UriNoAuthority | UriType::UriCannotBeABase => false,
_ => true,
}
}
pub fn cannot_be_a_base(&self) -> bool {
match self {
UriType::UriCannotBeABase | UriType::Fragment | UriType::Query => true,
_ => false,
}
}
}
#[cfg(test)]
mod test {
use crate::{UriType, *};
#[test]
fn uri_type_uri() {
assert_eq!(
iuri_ref!("http://example.com/abcd").uri_type(),
UriType::Uri
);
assert_eq!(iuri!("http://example.com/abcd").uri_type(), UriType::Uri);
assert_eq!(
iuri_ref!("http://example.com/abcd").components().uri_type(),
UriType::Uri
);
}
#[test]
fn uri_type_uri_cannot_be_a_base() {
assert_eq!(
iuri_ref!("tel:+1-555-867-5309").uri_type(),
UriType::UriCannotBeABase
);
assert_eq!(
iuri!("tel:+1-555-867-5309").uri_type(),
UriType::UriCannotBeABase
);
assert_eq!(
iuri_ref!("tel:+1-555-867-5309").components().uri_type(),
UriType::UriCannotBeABase
);
}
#[test]
fn uri_type_uri_no_authority() {
assert_eq!(
iuri_ref!("unix:/run/foo.socket").uri_type(),
UriType::UriNoAuthority
);
assert_eq!(
iuri!("unix:/run/foo.socket").uri_type(),
UriType::UriNoAuthority
);
assert_eq!(
iuri_ref!("unix:/run/foo.socket").components().uri_type(),
UriType::UriNoAuthority
);
}
#[test]
fn uri_type_network_path() {
assert_eq!(
iuri_ref!("//example.com/foo/bar").uri_type(),
UriType::NetworkPath
);
assert_eq!(
iuri!("//example.com/foo/bar").uri_type(),
UriType::NetworkPath
);
assert_eq!(
iuri_ref!("//example.com/foo/bar").components().uri_type(),
UriType::NetworkPath,
"{:?}",
iuri_ref!("//example.com/foo/bar").components()
);
}
#[test]
fn uri_type_absolute_path() {
assert_eq!(iuri_ref!("/foo/bar?q").uri_type(), UriType::AbsolutePath);
assert_eq!(irel_ref!("/foo/bar?q").uri_type(), UriType::AbsolutePath);
assert_eq!(
iuri_ref!("/foo/bar?q").components().uri_type(),
UriType::AbsolutePath
);
}
#[test]
fn uri_type_relative_path() {
assert_eq!(iuri_ref!("foo/bar?q").uri_type(), UriType::RelativePath);
assert_eq!(irel_ref!("foo/bar?q").uri_type(), UriType::RelativePath);
assert_eq!(
iuri_ref!("foo/bar?q").components().uri_type(),
UriType::RelativePath
);
}
#[test]
fn uri_type_query() {
assert_eq!(iuri_ref!("?q#frag").uri_type(), UriType::Query);
assert_eq!(irel_ref!("?q#frag").uri_type(), UriType::Query);
assert_eq!(iuri_ref!("?q#frag").components().uri_type(), UriType::Query);
}
#[test]
fn uri_type_fragment() {
assert_eq!(iuri_ref!("#foo-bar").uri_type(), UriType::Fragment);
assert_eq!(irel_ref!("#foo-bar").uri_type(), UriType::Fragment);
assert_eq!(
iuri_ref!("#foo-bar").components().uri_type(),
UriType::Fragment
);
}
}