shared_files/
errors.rs

1//! Contains commonly used error types.
2
3use std::fmt::{Display, Formatter};
4use tokio::io;
5
6#[derive(Debug)]
7pub enum CompleteWritingError {
8    /// An I/O error occurred.
9    Io(io::Error),
10    /// Writing to the file failed.
11    FileWritingFailed,
12    /// Failed to synchronize the file with the underlying buffer.
13    SyncError,
14}
15
16#[derive(Debug)]
17pub enum WriteError {
18    /// An I/O error occurred.
19    Io(io::Error),
20    /// The file was already closed
21    FileClosed,
22}
23
24#[derive(Debug)]
25pub enum ReadError {
26    /// An I/O error occurred.
27    Io(io::Error),
28    /// The file was already closed
29    FileClosed,
30}
31
32impl Display for CompleteWritingError {
33    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
34        match self {
35            CompleteWritingError::Io(io) => write!(f, "{}", io),
36            CompleteWritingError::FileWritingFailed => write!(f, "Writing to the file failed"),
37            CompleteWritingError::SyncError => write!(
38                f,
39                "Failed to synchronize the file with the underlying buffer"
40            ),
41        }
42    }
43}
44
45impl Display for WriteError {
46    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
47        match self {
48            WriteError::Io(io) => write!(f, "{}", io),
49            WriteError::FileClosed => write!(f, "The file was already closed"),
50        }
51    }
52}
53
54impl Display for ReadError {
55    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
56        match self {
57            ReadError::Io(io) => write!(f, "{}", io),
58            ReadError::FileClosed => write!(f, "The file was already closed"),
59        }
60    }
61}
62
63impl From<io::Error> for CompleteWritingError {
64    fn from(value: io::Error) -> Self {
65        CompleteWritingError::Io(value)
66    }
67}
68
69impl From<io::Error> for WriteError {
70    fn from(value: io::Error) -> Self {
71        WriteError::Io(value)
72    }
73}
74
75impl From<io::Error> for ReadError {
76    fn from(value: io::Error) -> Self {
77        ReadError::Io(value)
78    }
79}
80
81impl std::error::Error for CompleteWritingError {}
82impl std::error::Error for WriteError {}
83impl std::error::Error for ReadError {}