use std::fmt;
use thiserror::Error;
macro_rules! opaque_error {
($(#[$meta:meta])* $name:ident($inner:ty)) => {
$(#[$meta])*
#[derive(Debug)]
pub struct $name($inner);
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl std::error::Error for $name {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.0)
}
}
};
}
opaque_error! {
XmlParseError(quick_xml::Error)
}
#[cfg(feature = "http")]
opaque_error! {
UrlParseError(url::ParseError)
}
impl XmlParseError {
pub(crate) const fn new(inner: quick_xml::Error) -> Self {
Self(inner)
}
}
#[cfg(feature = "http")]
impl Clone for UrlParseError {
fn clone(&self) -> Self {
Self(self.0)
}
}
#[cfg(feature = "http")]
impl UrlParseError {
pub(crate) const fn new(inner: url::ParseError) -> Self {
Self(inner)
}
}
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum FeedError {
#[error("XML parsing error: {0}")]
XmlError(#[source] XmlParseError),
#[error("IO error: {0}")]
IoError(#[from] std::io::Error),
#[error("Invalid feed format: {0}")]
InvalidFormat(String),
#[error("Encoding error: {0}")]
EncodingError(String),
#[cfg(feature = "http")]
#[error("HTTP error: {0}")]
Fetch(#[from] crate::http::FetchError),
#[error("Unknown error: {0}")]
Unknown(String),
}
impl FeedError {
pub(crate) const fn xml(error: quick_xml::Error) -> Self {
Self::XmlError(XmlParseError::new(error))
}
}
pub type Result<T> = std::result::Result<T, FeedError>;
#[cfg(test)]
mod tests {
use super::*;
use std::assert_matches;
use std::error::Error as StdError;
#[test]
fn test_error_display() {
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "test");
let inner = io_err.to_string();
let err = FeedError::xml(quick_xml::Error::Io(std::sync::Arc::new(io_err)));
assert!(err.to_string().starts_with("XML parsing error: "));
assert!(err.to_string().contains(&inner));
assert_matches!(err, FeedError::XmlError(_));
}
#[test]
fn test_error_from_io() {
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
let feed_err = FeedError::from(io_err);
assert_matches!(feed_err, FeedError::IoError(_));
}
#[test]
fn test_result_type() {
#[expect(
clippy::unnecessary_wraps,
reason = "the test exercises the `Result` alias itself"
)]
fn get_result() -> Result<i32> {
Ok(42)
}
let result = get_result();
assert!(result.is_ok());
assert_eq!(result.expect("should be ok"), 42);
let error: Result<i32> = Err(FeedError::Unknown("test".to_string()));
assert!(error.is_err());
}
#[test]
fn typed_variants_expose_downcastable_source() {
let xml_err = FeedError::xml(quick_xml::Error::Io(std::sync::Arc::new(
std::io::Error::other("xml io failure"),
)));
let xml_source = StdError::source(&xml_err).expect("XmlError must carry a source");
let inner = xml_source
.source()
.expect("XmlParseError must expose the reader error");
assert!(inner.downcast_ref::<quick_xml::Error>().is_some());
let io_err = FeedError::from(std::io::Error::other("io failure"));
let io_source = StdError::source(&io_err).expect("IoError must carry a source");
assert!(io_source.downcast_ref::<std::io::Error>().is_some());
}
}