use thiserror::Error;
#[derive(Error, Debug, Clone, PartialEq)]
pub enum SxurlError {
#[error("Invalid scheme: only https, http, and ftp are supported")]
InvalidScheme,
#[error("Host must be a valid DNS name, not an IP address")]
HostNotDns,
#[error("Hostname exceeds maximum length of 255 bytes")]
HostTooLong,
#[error("Invalid DNS label: {0}")]
InvalidLabel(String),
#[error("DNS label contains invalid characters")]
InvalidCharacter,
#[error("Invalid port number: must be between 1 and 65535")]
InvalidPort,
#[error("URL parsing failed: {0}")]
ParseError(String),
#[error("URL parsing error: {0}")]
UrlParseError(String),
#[error("Invalid SXURL length: expected 64 hex characters")]
InvalidLength,
#[error("SXURL contains invalid hex characters")]
InvalidHexCharacter,
#[error("Invalid SXURL header format")]
InvalidHeader,
#[error("Unsupported SXURL version: {0}")]
UnsupportedVersion(u16),
#[error("Reserved bit is set in SXURL header")]
ReservedBitSet,
#[error("Port presence flag doesn't match port field")]
PortFlagMismatch,
#[error("SHA-256 hashing failed")]
HashingError,
#[error("Internal error occurred")]
InternalError,
}
impl From<url::ParseError> for SxurlError {
fn from(err: url::ParseError) -> Self {
SxurlError::UrlParseError(err.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
assert_eq!(
SxurlError::InvalidScheme.to_string(),
"Invalid scheme: only https, http, and ftp are supported"
);
assert_eq!(
SxurlError::HostTooLong.to_string(),
"Hostname exceeds maximum length of 255 bytes"
);
}
#[test]
fn test_error_equality() {
assert_eq!(SxurlError::InvalidScheme, SxurlError::InvalidScheme);
assert_ne!(SxurlError::InvalidScheme, SxurlError::HostNotDns);
}
#[test]
fn test_url_parse_error_conversion() {
let url_error = url::ParseError::EmptyHost;
let sxurl_error: SxurlError = url_error.into();
match sxurl_error {
SxurlError::UrlParseError(_) => (),
_ => panic!("Expected UrlParseError variant"),
}
}
}