imap_types/
error.rs

1//! Error-related types.
2
3use std::fmt::{Display, Formatter};
4
5use thiserror::Error;
6
7/// A validation error.
8///
9/// This error can be returned during validation of a value, e.g., a tag, atom, etc.
10#[derive(Clone, Debug, Eq, Error, Hash, Ord, PartialEq, PartialOrd)]
11pub struct ValidationError {
12    kind: ValidationErrorKind,
13}
14
15impl Display for ValidationError {
16    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
17        write!(f, "Validation failed: {}", self.kind)
18    }
19}
20
21#[derive(Clone, Debug, Eq, Error, Hash, Ord, PartialEq, PartialOrd)]
22pub(crate) enum ValidationErrorKind {
23    #[error("Must not be empty")]
24    Empty,
25    #[error("Invalid value")]
26    Invalid,
27    #[error("Invalid byte b'\\x{byte:02x}' at index {at}")]
28    InvalidByteAt { byte: u8, at: usize },
29}
30
31impl ValidationError {
32    pub(crate) fn new(kind: ValidationErrorKind) -> Self {
33        Self { kind }
34    }
35}