1use crate::metadata::Metadata;
2use crate::{reader, writer};
3use std::borrow::Cow;
4use std::io;
5use std::num::ParseIntError;
6use std::sync::{PoisonError, RwLockReadGuard, RwLockWriteGuard};
7use thiserror::Error;
8use tokio::sync::oneshot;
9use tokio::task;
10
11#[derive(Debug, Error)]
12pub enum Error {
13 #[error("DecodeError: {0}")]
14 Decode(String),
15 #[error("Generic: {0}")]
16 Generic(Cow<'static, str>),
17 #[error("EncodeError: {0}")]
18 Encode(String),
19 #[error("FileCorrupted: {0}")]
20 FileCorrupted(Cow<'static, str>),
21 #[error("Integrity: {0}")]
22 Integrity(Cow<'static, str>),
23 #[error("Internal: {0}")]
24 Internal(Cow<'static, str>),
25 #[error("InvalidPath: {0}")]
26 InvalidPath(&'static str),
27 #[error("InvalidFileName")]
28 InvalidFileName,
29 #[error("IOError: {0}")]
30 IO(#[from] io::Error),
31 #[error("Locked: {0}")]
32 Locked(&'static str),
33 #[error("ParseError: {0}")]
34 Parse(&'static str),
35}
36
37impl From<task::JoinError> for Error {
38 fn from(err: task::JoinError) -> Self {
39 Self::Generic(err.to_string().into())
40 }
41}
42
43impl From<bincode::error::DecodeError> for Error {
44 fn from(err: bincode::error::DecodeError) -> Self {
45 Self::Decode(err.to_string())
46 }
47}
48
49impl From<bincode::error::EncodeError> for Error {
50 fn from(err: bincode::error::EncodeError) -> Self {
51 Self::Encode(err.to_string())
52 }
53}
54
55impl From<ParseIntError> for Error {
56 fn from(_: ParseIntError) -> Self {
57 Self::Parse("Cannot parse value as integer")
58 }
59}
60
61impl From<PoisonError<RwLockReadGuard<'_, Metadata>>> for Error {
62 fn from(err: PoisonError<RwLockReadGuard<Metadata>>) -> Self {
63 Self::Generic(err.to_string().into())
64 }
65}
66
67impl From<PoisonError<RwLockWriteGuard<'_, Metadata>>> for Error {
68 fn from(err: PoisonError<RwLockWriteGuard<Metadata>>) -> Self {
69 Self::Generic(err.to_string().into())
70 }
71}
72
73impl From<flume::SendError<writer::Action>> for Error {
74 fn from(err: flume::SendError<writer::Action>) -> Self {
75 Self::Internal(err.to_string().into())
76 }
77}
78
79impl From<flume::SendError<reader::Action>> for Error {
80 fn from(err: flume::SendError<reader::Action>) -> Self {
81 Self::Internal(err.to_string().into())
82 }
83}
84
85impl From<oneshot::error::RecvError> for Error {
86 fn from(err: oneshot::error::RecvError) -> Self {
87 Self::Generic(err.to_string().into())
88 }
89}