1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//! Error-related types.

use std::fmt::{Display, Formatter};

use thiserror::Error;

/// A validation error.
///
/// This error can be returned during validation of a value, e.g., a tag, atom, etc.
#[derive(Clone, Debug, Eq, Error, Hash, Ord, PartialEq, PartialOrd)]
pub struct ValidationError {
    kind: ValidationErrorKind,
}

impl Display for ValidationError {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        write!(f, "Validation failed: {}", self.kind)
    }
}

#[derive(Clone, Debug, Eq, Error, Hash, Ord, PartialEq, PartialOrd)]
pub(crate) enum ValidationErrorKind {
    #[error("Must not be empty")]
    Empty,
    #[error("Invalid value")]
    Invalid,
    #[error("Invalid byte b'\\x{byte:02x}' at index {at}")]
    InvalidByteAt { byte: u8, at: usize },
}

impl ValidationError {
    pub(crate) fn new(kind: ValidationErrorKind) -> Self {
        Self { kind }
    }
}