cargo_toml/
error.rs

1use std::path::PathBuf;
2use std::error::Error as StdErr;
3use std::{fmt, io};
4
5/// In this crate's `Result`s.
6#[derive(Debug)]
7#[non_exhaustive]
8pub enum Error {
9    /// TOML parsing errors
10    Parse(Box<toml::de::Error>),
11    /// Filesystem access errors
12    Io(io::Error),
13    /// Manifest uses workspace inheritance, and the workspace failed to load
14    Workspace(Box<(Error, Option<PathBuf>)>),
15    /// Manifest uses workspace inheritance, and the data hasn't been inherited yet
16    InheritedUnknownValue,
17    /// Manifest uses workspace inheritance, but the root workspace is missing data
18    WorkspaceIntegrity(String),
19    /// ???
20    Other(&'static str),
21}
22
23impl StdErr for Error {
24    fn source(&self) -> Option<&(dyn StdErr + 'static)> {
25        match self {
26            Error::Parse(err) => Some(err),
27            Error::Io(err) => Some(err),
28            Error::Workspace(err) => Some(&err.0),
29            Error::Other(_) | Error::InheritedUnknownValue | Error::WorkspaceIntegrity(_) => None,
30        }
31    }
32}
33
34impl fmt::Display for Error {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            Error::Parse(err) => err.fmt(f),
38            Error::Io(err) => err.fmt(f),
39            Error::Other(msg) => f.write_str(msg),
40            Error::WorkspaceIntegrity(s) => f.write_str(s),
41            Error::Workspace(err_path) => {
42                f.write_str("can't load root workspace")?;
43                if let Some(path) = &err_path.1 {
44                    write!(f, " at {}", path.display())?
45                }
46                f.write_str(": ")?;
47                err_path.0.fmt(f)
48            }
49            Error::InheritedUnknownValue => f.write_str("value from workspace hasn't been set"),
50        }
51    }
52}
53
54impl Clone for Error {
55    fn clone(&self) -> Self {
56        match self {
57            Error::Parse(err) => Error::Parse(err.clone()),
58            Error::Io(err) => Error::Io(io::Error::new(err.kind(), err.to_string())),
59            Error::Other(msg) => Error::Other(msg),
60            Error::WorkspaceIntegrity(msg) => Error::WorkspaceIntegrity(msg.clone()),
61            Error::Workspace(e) => Error::Workspace(e.clone()),
62            Error::InheritedUnknownValue => Error::InheritedUnknownValue,
63        }
64    }
65}
66
67impl From<toml::de::Error> for Error {
68    fn from(o: toml::de::Error) -> Self {
69        Error::Parse(Box::new(o))
70    }
71}
72
73impl From<io::Error> for Error {
74    fn from(o: io::Error) -> Self {
75        Error::Io(o)
76    }
77}