cargo_lock/
error.rs

1//! Error types
2
3use std::{fmt, io};
4
5/// Result type with the `cargo-lock` crate's [`Error`] type.
6pub type Result<T> = core::result::Result<T, Error>;
7
8/// Error type.
9#[derive(Debug)]
10#[non_exhaustive]
11pub enum Error {
12    /// An error occurred performing an I/O operation (e.g. network, file)
13    Io(io::ErrorKind),
14
15    /// Couldn't parse response data
16    Parse(String),
17
18    /// Errors related to versions
19    Version(semver::Error),
20
21    /// Errors related to graph resolution
22    Resolution(String),
23}
24
25impl fmt::Display for Error {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            Error::Io(kind) => write!(f, "I/O operation failed: {kind}"),
29            Error::Parse(s) => write!(f, "parse error: {s}"),
30            Error::Version(err) => write!(f, "version error: {err}"),
31            Error::Resolution(err) => write!(f, "resolution error: {err}"),
32        }
33    }
34}
35
36impl From<io::Error> for Error {
37    fn from(err: io::Error) -> Self {
38        Error::Io(err.kind())
39    }
40}
41
42impl From<semver::Error> for Error {
43    fn from(err: semver::Error) -> Self {
44        Error::Version(err)
45    }
46}
47
48impl From<std::num::ParseIntError> for Error {
49    fn from(err: std::num::ParseIntError) -> Self {
50        Error::Parse(err.to_string())
51    }
52}
53
54impl std::error::Error for Error {}