krates/
errors.rs

1#[cfg(not(feature = "metadata"))]
2use crate::cm::Error as CMErr;
3#[cfg(feature = "metadata")]
4use cargo_metadata::Error as CMErr;
5use std::fmt;
6
7/// Errors that can occur when acquiring metadata to create a graph from
8#[derive(Debug)]
9pub enum Error {
10    /// --no-deps was specified when acquiring metadata
11    NoResolveGraph,
12    /// A [`cargo_metadata::Error`] error occurred
13    Metadata(CMErr),
14    /// A package specification was invalid
15    InvalidPkgSpec(&'static str),
16    /// Due to how the graph was built, all possible root nodes were actually
17    /// filtered out, leaving an empty graph
18    NoRootKrates,
19}
20
21impl fmt::Display for Error {
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        match self {
24            Self::NoResolveGraph => f.write_str("no resolution graph was provided"),
25            Self::Metadata(err) => write!(f, "{err}"),
26            Self::InvalidPkgSpec(err) => write!(f, "package spec was invalid: {err}"),
27            Self::NoRootKrates => f.write_str("no root crates available"),
28        }
29    }
30}
31
32impl std::error::Error for Error {
33    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
34        match self {
35            Self::Metadata(err) => Some(err),
36            _ => None,
37        }
38    }
39}
40
41impl From<CMErr> for Error {
42    fn from(e: CMErr) -> Self {
43        Error::Metadata(e)
44    }
45}