rfc822_like/de/
error.rs

1//! Error types related to deserialization of RFC822-like format.
2
3use std::fmt;
4use std::io;
5
6#[derive(Debug, thiserror::Error)]
7pub(crate) enum ErrorInner {
8    #[error("{0}")]
9    Custom(String),
10    #[error("Line {0} doesn't contain a colon")]
11    MissingColon(usize),
12    #[error("I/O error")]
13    IoError(#[from] io::Error),
14    #[error("The deserialized type is ambiguous and must be explicitly specified. (RFC822 is NOT self-describing.)")]
15    AmbiguousType,
16}
17
18impl serde::de::Error for Error {
19    fn custom<T: fmt::Display>(msg: T) -> Self {
20        ErrorInner::Custom(msg.to_string()).into()
21    }
22}
23
24/// Error that can happen during deserialization.
25///
26/// The error is currently encapsulated and not exposed because it's not yet certain what kind of
27/// information we will store in the error type.
28/// However, it does implement standard error-related traits and has a human-friendly `Display`
29/// implementation.
30#[derive(Debug, thiserror::Error)]
31#[error(transparent)]
32pub struct Error(#[from] ErrorInner);
33
34/// Error returned when opening a file and subsequent deserialization fail.
35#[derive(Debug, thiserror::Error)]
36pub enum ReadFileError {
37    /// Variant returned when a file couldn't be opened.
38    #[error("failed to open file {path} for reading")]
39    Open {
40        /// Path to file that was accessed.
41        path: std::path::PathBuf,
42        /// The reason why opening failed.
43        #[source] error: std::io::Error,
44    },
45    /// Variant returned when read or deserialization fail.
46    #[error("failed to load file {path}")]
47    Load {
48        /// Path to the file that could not be loaded.
49        path: std::path::PathBuf,
50        /// The reason why loading failed.
51        #[source] error: Error,
52    },
53}