1use alloc::string::String;
4use core::fmt;
5use std::io;
6
7use crate::MANIFEST_DIR;
8
9#[derive(Debug)]
11pub struct TomlError {
12 pub(super) error: toml::de::Error,
13}
14
15impl fmt::Display for TomlError {
16 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17 self.error.fmt(f)
18 }
19}
20
21impl std::error::Error for TomlError {
22 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
23 Some(&self.error)
24 }
25}
26
27#[derive(Debug)]
29#[non_exhaustive]
30pub enum Error {
31 NotFoundManifestDir,
35
36 InvalidManifest(String),
38
39 NotFound,
43
44 Io(io::Error),
46
47 Toml(TomlError),
49}
50
51impl fmt::Display for Error {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 match self {
54 Error::NotFoundManifestDir => {
55 write!(f, "`{MANIFEST_DIR}` environment variable not found")
56 }
57 Error::InvalidManifest(reason) => {
58 write!(f, "The manifest is invalid because: {reason}")
59 }
60 Error::NotFound => {
61 f.write_str("the crate with the specified name not found in dependencies")
62 }
63 Error::Io(e) => write!(f, "an error occurred while to open or to read: {e}"),
64 Error::Toml(e) => write!(f, "an error occurred while parsing the manifest file: {e}"),
65 }
66 }
67}
68
69impl std::error::Error for Error {
70 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
71 match self {
72 Error::Io(e) => Some(e),
73 Error::Toml(e) => Some(e),
74 _ => None,
75 }
76 }
77}
78
79impl From<io::Error> for Error {
80 fn from(e: io::Error) -> Self {
81 Error::Io(e)
82 }
83}