rustsec_admin/
error.rs

1//! Error types
2
3use crate::prelude::*;
4use abscissa_core::error::{BoxError, Context};
5use std::{
6    fmt::{self, Display},
7    io,
8    ops::Deref,
9};
10use thiserror::Error;
11
12/// Kinds of errors
13#[derive(Copy, Clone, Debug, Eq, Error, PartialEq)]
14pub enum ErrorKind {
15    /// Error in configuration file
16    #[error("config error")]
17    Config,
18
19    /// crates.io index error
20    #[error("crates.io index error")]
21    CratesIo,
22
23    /// Input/output error
24    #[error("I/O error")]
25    Io,
26
27    /// Parsing error
28    #[error("RustSec error")]
29    Parse,
30
31    /// `rustsec` crate errors
32    #[error("RustSec error")]
33    RustSec,
34}
35
36impl ErrorKind {
37    /// Create an error context from this error
38    pub fn context(self, source: impl Into<BoxError>) -> Context<ErrorKind> {
39        Context::new(self, Some(source.into()))
40    }
41}
42
43/// Error type
44#[derive(Debug)]
45pub struct Error(Box<Context<ErrorKind>>);
46
47impl Deref for Error {
48    type Target = Context<ErrorKind>;
49
50    fn deref(&self) -> &Context<ErrorKind> {
51        &self.0
52    }
53}
54
55impl Display for Error {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        self.0.fmt(f)
58    }
59}
60
61impl std::error::Error for Error {
62    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
63        self.0.source()
64    }
65}
66
67impl From<Context<ErrorKind>> for Error {
68    fn from(context: Context<ErrorKind>) -> Self {
69        Error(Box::new(context))
70    }
71}
72
73impl From<tame_index::Error> for Error {
74    fn from(other: tame_index::Error) -> Self {
75        format_err!(ErrorKind::CratesIo, "{}", other).into()
76    }
77}
78
79impl From<io::Error> for Error {
80    fn from(other: io::Error) -> Self {
81        ErrorKind::Io.context(other).into()
82    }
83}
84
85impl From<rustsec::Error> for Error {
86    fn from(other: rustsec::Error) -> Self {
87        ErrorKind::RustSec.context(other).into()
88    }
89}