1use thiserror::Error;
4
5use crate::manager::FileMode;
6
7use std::convert::Infallible;
8use std::io;
9
10#[derive(Debug, Error)]
12pub enum Error<FE> {
13 #[error("format error: {0}")]
17 Format(FE),
18 #[error(transparent)]
20 Io(#[from] io::Error),
21 #[error(transparent)]
23 Other(#[from] OtherError)
24}
25
26impl From<Error<io::Error>> for io::Error {
27 fn from(err: Error<io::Error>) -> Self {
28 match err {
29 Error::Format(err) | Error::Io(err) => err,
30 Error::Other(err) => io::Error::other(err)
31 }
32 }
33}
34
35#[derive(Debug, Error)]
37pub enum OrUserError<T, U> {
38 #[error(transparent)]
40 Base(T),
41 #[error("user error: {0}")]
43 User(U)
44}
45
46impl<T, U> OrUserError<T, U> {
47 pub fn convert_with<E, F>(self, f: F) -> E
51 where T: Into<E>, F: FnOnce(U) -> E {
52 match self {
53 Self::Base(err) => err.into(),
54 Self::User(err) => f(err)
55 }
56 }
57
58 pub fn convert<E>(self) -> E
61 where T: Into<E>, U: Into<E> {
62 self.convert_with(U::into)
63 }
64}
65
66impl<T> OrUserError<T, Infallible> {
67 pub fn into_base(self) -> T {
69 match self {
70 Self::Base(err) => err,
71 Self::User(i) => match i {}
72 }
73 }
74}
75
76impl<U> OrUserError<Infallible, U> {
77 pub fn into_user(self) -> U {
79 match self {
80 Self::User(err) => err,
81 Self::Base(i) => match i {}
82 }
83 }
84}
85
86impl<T, U> From<T> for OrUserError<T, U> {
87 fn from(err: T) -> Self {
88 OrUserError::Base(err)
89 }
90}
91
92#[non_exhaustive]
97#[derive(Debug, Error)]
98pub enum OtherError {
99 #[error("file mode {0:?} is incompatible with this operation")]
101 IncompatibleFileMode(FileMode)
102}