use crate::core::decoder::decode_hex;
use crate::error::SxurlError;
use crate::types::UrlComponentType;
pub fn sxurl_contains(
sxurl_hex: &str,
component: UrlComponentType,
value: &str,
) -> Result<bool, SxurlError> {
use crate::core::hasher::ComponentHasher;
let decoded = decode_hex(sxurl_hex)?;
match component {
UrlComponentType::Scheme => {
Ok(decoded.header.scheme == value)
}
UrlComponentType::Tld => {
let expected_hash = ComponentHasher::hash_tld(value)?;
Ok(decoded.tld_hash == expected_hash)
}
UrlComponentType::Domain => {
let expected_hash = ComponentHasher::hash_domain(value)?;
Ok(decoded.domain_hash == expected_hash)
}
UrlComponentType::Subdomain => {
if value.is_empty() {
Ok(decoded.subdomain_hash == 0)
} else {
let expected_hash = ComponentHasher::hash_subdomain(value)?;
Ok(decoded.subdomain_hash == expected_hash)
}
}
UrlComponentType::Port => {
if let Ok(port_num) = value.parse::<u16>() {
Ok(decoded.port == port_num)
} else {
Ok(false)
}
}
UrlComponentType::Path => {
let expected_hash = ComponentHasher::hash_path(value)?;
Ok(decoded.path_hash == expected_hash)
}
UrlComponentType::Query => {
if value.is_empty() {
Ok(decoded.params_hash == 0)
} else {
let expected_hash = ComponentHasher::hash_params(value)?;
Ok(decoded.params_hash == expected_hash)
}
}
UrlComponentType::Fragment => {
if value.is_empty() {
Ok(decoded.fragment_hash == 0)
} else {
let expected_hash = ComponentHasher::hash_fragment(value)?;
Ok(decoded.fragment_hash == expected_hash)
}
}
UrlComponentType::Host => {
Err(SxurlError::ParseError("Host matching not supported - use Domain + Subdomain instead".to_string()))
}
UrlComponentType::PathSegments => {
Err(SxurlError::ParseError("PathSegments matching not supported - use Path instead".to_string()))
}
UrlComponentType::Filename => {
Err(SxurlError::ParseError("Filename matching not supported - use Path instead".to_string()))
}
}
}
pub fn sxurl_has_domain(sxurl_hex: &str, domain: &str) -> Result<bool, SxurlError> {
sxurl_contains(sxurl_hex, UrlComponentType::Domain, domain)
}
pub fn sxurl_has_subdomain(sxurl_hex: &str, subdomain: &str) -> Result<bool, SxurlError> {
sxurl_contains(sxurl_hex, UrlComponentType::Subdomain, subdomain)
}
pub fn sxurl_has_tld(sxurl_hex: &str, tld: &str) -> Result<bool, SxurlError> {
sxurl_contains(sxurl_hex, UrlComponentType::Tld, tld)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::encoder::encode_url_to_hex;
#[test]
fn test_sxurl_convenience_functions() {
let test_url = "https://api.github.com/repos?page=1";
let _sxurl = encode_url_to_hex(test_url).unwrap();
}
#[test]
fn test_sxurl_contains_working() {
let test_url = "https://api.github.com/repos?page=1#section";
let sxurl = encode_url_to_hex(test_url).unwrap();
assert!(sxurl_contains(&sxurl, UrlComponentType::Scheme, "https").unwrap());
assert!(sxurl_contains(&sxurl, UrlComponentType::Domain, "github").unwrap());
assert!(sxurl_contains(&sxurl, UrlComponentType::Subdomain, "api").unwrap());
assert!(sxurl_contains(&sxurl, UrlComponentType::Tld, "com").unwrap());
assert!(sxurl_contains(&sxurl, UrlComponentType::Path, "/repos").unwrap());
assert!(sxurl_contains(&sxurl, UrlComponentType::Query, "page=1").unwrap());
assert!(sxurl_contains(&sxurl, UrlComponentType::Fragment, "section").unwrap());
assert!(!sxurl_contains(&sxurl, UrlComponentType::Scheme, "http").unwrap());
assert!(!sxurl_contains(&sxurl, UrlComponentType::Domain, "google").unwrap());
assert!(!sxurl_contains(&sxurl, UrlComponentType::Subdomain, "www").unwrap());
assert!(!sxurl_contains(&sxurl, UrlComponentType::Tld, "org").unwrap());
println!("✓ SXURL component matching works correctly!");
}
}