Skip to main content

nntp_proxy/protocol/article/
error.rs

1//! Article parsing errors
2
3use std::fmt;
4
5/// Errors that can occur when parsing article responses
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum ParseError {
8    /// Invalid or unexpected status code
9    InvalidStatusCode(u16),
10
11    /// Missing blank line separator between headers and body
12    MissingSeparator,
13
14    /// Missing multiline terminator (.\r\n)
15    MissingTerminator,
16
17    /// Invalid header format
18    InvalidHeader(String),
19
20    /// HEAD response contains body (invalid)
21    UnexpectedBody,
22
23    /// Invalid yenc structure
24    InvalidYenc(String),
25
26    /// Buffer too short
27    BufferTooShort,
28
29    /// Invalid message-ID format
30    InvalidMessageId(String),
31}
32
33impl fmt::Display for ParseError {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        match self {
36            Self::InvalidStatusCode(code) => write!(f, "Invalid status code: {}", code),
37            Self::MissingSeparator => {
38                write!(f, "Missing blank line separator between headers and body")
39            }
40            Self::MissingTerminator => write!(f, "Missing multiline terminator (CRLF.CRLF)"),
41            Self::InvalidHeader(msg) => write!(f, "Invalid header: {}", msg),
42            Self::UnexpectedBody => write!(f, "HEAD response should not contain body"),
43            Self::InvalidYenc(msg) => write!(f, "Invalid yenc: {}", msg),
44            Self::BufferTooShort => write!(f, "Buffer too short to contain valid response"),
45            Self::InvalidMessageId(msg) => write!(f, "Invalid message-ID: {}", msg),
46        }
47    }
48}
49
50impl std::error::Error for ParseError {}
51
52// Allow conversion from ValidationError to ParseError
53impl From<crate::types::validated::ValidationError> for ParseError {
54    fn from(err: crate::types::validated::ValidationError) -> Self {
55        ParseError::InvalidMessageId(err.to_string())
56    }
57}