#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ResourceType {
Ftp,
Http,
Https,
BitTorrent,
NotSupported,
Unknown,
}
impl ResourceType {
pub fn as_str(&self) -> &'static str {
match self {
Self::Ftp => "ftp",
Self::Http => "http",
Self::Https => "https",
Self::BitTorrent => "bittorrent",
Self::NotSupported => "not_supported",
Self::Unknown => "unknown",
}
}
pub fn from_url(url: &str) -> Self {
let lower = url.to_lowercase();
if lower.starts_with("http://") {
Self::Http
} else if lower.starts_with("https://") {
Self::Https
} else if lower.starts_with("ftp://") {
Self::Ftp
} else if lower.starts_with("magnet:") || lower.ends_with(".torrent") {
Self::BitTorrent
} else {
Self::Unknown
}
}
pub fn from_v3_type(type_attr: &str) -> Self {
match type_attr.to_lowercase().as_str() {
"ftp" | "sftp" => Self::Ftp,
"http" => Self::Http,
"https" => Self::Https,
"bittorrent" | "torrent" => Self::BitTorrent,
_ => Self::NotSupported,
}
}
pub fn from_url_type_str(type_attr: &str) -> Self {
Self::from_v3_type(type_attr)
}
pub fn is_supported(&self) -> bool {
matches!(
self,
Self::Ftp | Self::Http | Self::Https | Self::BitTorrent
)
}
pub fn is_non_p2p(&self) -> bool {
matches!(self, Self::Ftp | Self::Http | Self::Https)
}
}
#[derive(Debug, Clone)]
pub struct MetalinkResource {
pub url: String,
pub resource_type: ResourceType,
pub priority: i32,
pub preference: i32,
pub location: String,
pub max_connections: i32,
}
pub const LOWEST_PRIORITY: i32 = 999999;
impl MetalinkResource {
pub fn new(url: impl Into<String>) -> Self {
let url_str = url.into();
let resource_type = ResourceType::from_url(&url_str);
Self {
url: url_str,
resource_type,
priority: LOWEST_PRIORITY,
preference: 0,
location: String::new(),
max_connections: -1,
}
}
pub fn with_priority(url: impl Into<String>, priority: i32) -> Self {
Self {
priority,
..Self::new(url)
}
}
pub fn is_preferred_over(&self, other: &MetalinkResource) -> bool {
if self.priority != other.priority {
self.priority < other.priority
} else {
self.preference > other.preference
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_resource_type_from_url() {
assert_eq!(
ResourceType::from_url("http://example.com/file"),
ResourceType::Http
);
assert_eq!(
ResourceType::from_url("https://example.com/file"),
ResourceType::Https
);
assert_eq!(
ResourceType::from_url("ftp://example.com/file"),
ResourceType::Ftp
);
assert_eq!(
ResourceType::from_url("magnet:?xt=urn:btih:abc"),
ResourceType::BitTorrent
);
assert_eq!(
ResourceType::from_url("file.torrent"),
ResourceType::BitTorrent
);
assert_eq!(
ResourceType::from_url("unknown://host"),
ResourceType::Unknown
);
}
#[test]
fn test_resource_type_from_v3_type() {
assert_eq!(ResourceType::from_v3_type("http"), ResourceType::Http);
assert_eq!(ResourceType::from_v3_type("HTTPS"), ResourceType::Https);
assert_eq!(ResourceType::from_v3_type("ftp"), ResourceType::Ftp);
assert_eq!(
ResourceType::from_v3_type("bittorrent"),
ResourceType::BitTorrent
);
assert_eq!(
ResourceType::from_v3_type("unknown"),
ResourceType::NotSupported
);
}
#[test]
fn test_resource_type_is_supported() {
assert!(ResourceType::Http.is_supported());
assert!(ResourceType::Https.is_supported());
assert!(ResourceType::Ftp.is_supported());
assert!(ResourceType::BitTorrent.is_supported());
assert!(!ResourceType::NotSupported.is_supported());
assert!(!ResourceType::Unknown.is_supported());
}
#[test]
fn test_resource_type_is_non_p2p() {
assert!(ResourceType::Http.is_non_p2p());
assert!(ResourceType::Https.is_non_p2p());
assert!(ResourceType::Ftp.is_non_p2p());
assert!(!ResourceType::BitTorrent.is_non_p2p());
}
#[test]
fn test_lowest_priority() {
assert_eq!(LOWEST_PRIORITY, 999999);
}
#[test]
fn test_new_resource_defaults() {
let res = MetalinkResource::new("http://example.com/file");
assert_eq!(res.resource_type, ResourceType::Http);
assert_eq!(res.priority, LOWEST_PRIORITY);
assert_eq!(res.preference, 0);
assert_eq!(res.max_connections, -1);
assert!(res.location.is_empty());
}
}