use std::{io, path::PathBuf};
use thiserror::Error;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum FileAttributeKind {
Poster,
Date,
Subject,
}
impl std::fmt::Display for FileAttributeKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
Self::Poster => write!(f, "poster"),
Self::Date => write!(f, "date"),
Self::Subject => write!(f, "subject"),
}
}
}
#[derive(Error, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ParseNzbError {
#[error(
"Invalid or missing 'groups' element within a 'file' element. \
Each 'file' element must contain at least one valid 'groups' element."
)]
GroupsElement,
#[error(
"Invalid or missing 'segments' element within a '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."
)]
FileElement,
#[error(
"The NZB document contains only `.par2` files. \
It must include at least one non-`.par2` file."
)]
OnlyPar2Files,
#[error("Invalid or missing required attribute '{attribute}' in a 'file' element.")]
FileAttribute {
attribute: FileAttributeKind,
},
#[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 }
}
}