use std::{io, path::PathBuf};
use thiserror::Error;
#[derive(Error, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ParseNzbError {
#[error(
"Invalid or missing 'groups' element within the 'file' element. Each 'file' element must contain at least one valid 'groups' element."
)]
GroupsElement,
#[error(
"Invalid or missing 'segments' element within the 'file' element. Each 'file' element must contain at least one valid 'segments' element."
)]
SegmentsElement,
#[error(
"Invalid or missing 'file' element in the NZB document. The NZB document must contain at least one valid 'file' element, and each 'file' must have at least one valid 'groups' and 'segments' element."
)]
FileElement,
#[error("Invalid or missing required attribute '{attribute}' in a 'file' element.")]
FileAttribute {
attribute: String,
},
#[error("The NZB document is not valid XML and could not be parsed: {message}")]
XmlSyntax {
message: String,
},
}
impl From<roxmltree::Error> for ParseNzbError {
fn from(error: roxmltree::Error) -> Self {
ParseNzbError::XmlSyntax {
message: error.to_string(),
}
}
}
#[derive(Error, Debug)]
pub enum ParseNzbFileError {
#[error("I/O error while reading file '{file}': {source}")]
Io {
source: io::Error,
file: PathBuf,
},
#[error("Gzip decompression error for file '{file}': {source}")]
Gzip {
source: io::Error,
file: PathBuf,
},
#[error("NZB parsing error: {source}")]
Parse {
source: ParseNzbError,
},
}
impl ParseNzbFileError {
pub(crate) fn from_io_err(source: io::Error, file: impl Into<PathBuf>) -> Self {
ParseNzbFileError::Io {
source,
file: file.into(),
}
}
pub(crate) fn from_gzip_err(source: io::Error, file: impl Into<PathBuf>) -> Self {
ParseNzbFileError::Gzip {
source,
file: file.into(),
}
}
}
impl From<ParseNzbError> for ParseNzbFileError {
fn from(source: ParseNzbError) -> Self {
ParseNzbFileError::Parse { source }
}
}