Skip to main content

c4/
error.rs

1//! The crate error type: everything scanning, parsing, merging or
2//! deserializing can fail with.
3
4use std::path::PathBuf;
5
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors produced while scanning, parsing, merging or deserializing.
9#[derive(Debug)]
10pub enum Error {
11    /// Underlying I/O failure.
12    Io(std::io::Error),
13    /// A source path does not exist.
14    NotFound(PathBuf),
15    /// A file failed to parse.
16    Parse { path: PathBuf, message: String },
17    /// A table (csv) cell failed to convert. `row` is 1-based; the header
18    /// row, when enabled, counts as row 1.
19    Table {
20        path: PathBuf,
21        row: usize,
22        message: String,
23    },
24    /// The merged value could not deserialize into the requested type.
25    Deserialize(String),
26    /// An option requires a Cargo feature that is not compiled in.
27    Unsupported(String),
28}
29
30impl std::fmt::Display for Error {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        match self {
33            Error::Io(e) => write!(f, "io error: {e}"),
34            Error::NotFound(p) => write!(f, "source not found: {}", p.display()),
35            Error::Parse { path, message } => write!(f, "{}: {message}", path.display()),
36            Error::Table { path, row, message } => {
37                write!(f, "{} row {row}: {message}", path.display())
38            }
39            Error::Deserialize(m) => write!(f, "deserialize error: {m}"),
40            Error::Unsupported(m) => write!(f, "unsupported: {m}"),
41        }
42    }
43}
44
45impl std::error::Error for Error {}