1use abscissa_core::error::{BoxError, Context};
4use std::{
5 fmt::{self, Display},
6 io,
7 ops::Deref,
8};
9use thiserror::Error;
10
11#[derive(Copy, Clone, Debug, Eq, Error, PartialEq)]
13pub enum ErrorKind {
14 #[error("config error")]
16 Config,
17
18 #[error("I/O error")]
20 Io,
21
22 #[error("Cargo file not exists. Please specify path manually")]
24 MissingCargoFile,
25
26 #[error("Incorrect project Cargo.toml - make sure to select package Cargo.toml. Workspace toml is not allowed")]
27 IncorrectCargoFile,
28}
29
30impl ErrorKind {
31 pub fn context(self, source: impl Into<BoxError>) -> Context<ErrorKind> {
33 Context::new(self, Some(source.into()))
34 }
35}
36
37#[derive(Debug)]
39pub struct Error(Box<Context<ErrorKind>>);
40
41impl Deref for Error {
42 type Target = Context<ErrorKind>;
43
44 fn deref(&self) -> &Context<ErrorKind> {
45 &self.0
46 }
47}
48
49impl Display for Error {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 self.0.fmt(f)
52 }
53}
54
55impl std::error::Error for Error {
56 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
57 self.0.source()
58 }
59}
60
61impl From<ErrorKind> for Error {
62 fn from(kind: ErrorKind) -> Self {
63 Context::new(kind, None).into()
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<io::Error> for Error {
74 fn from(err: io::Error) -> Self {
75 ErrorKind::Io.context(err).into()
76 }
77}