1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//! Contains commonly used error types.

use std::fmt::{Display, Formatter};
use tokio::io;

#[derive(Debug)]
pub enum CompleteWritingError {
    /// An I/O error occurred.
    Io(io::Error),
    /// Writing to the file failed.
    FileWritingFailed,
    /// Failed to synchronize the file with the underlying buffer.
    SyncError,
}

#[derive(Debug)]
pub enum WriteError {
    /// An I/O error occurred.
    Io(io::Error),
    /// The file was already closed
    FileClosed,
}

#[derive(Debug)]
pub enum ReadError {
    /// An I/O error occurred.
    Io(io::Error),
    /// The file was already closed
    FileClosed,
}

impl Display for CompleteWritingError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            CompleteWritingError::Io(io) => write!(f, "{}", io),
            CompleteWritingError::FileWritingFailed => write!(f, "Writing to the file failed"),
            CompleteWritingError::SyncError => write!(
                f,
                "Failed to synchronize the file with the underlying buffer"
            ),
        }
    }
}

impl Display for WriteError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            WriteError::Io(io) => write!(f, "{}", io),
            WriteError::FileClosed => write!(f, "The file was already closed"),
        }
    }
}

impl Display for ReadError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            ReadError::Io(io) => write!(f, "{}", io),
            ReadError::FileClosed => write!(f, "The file was already closed"),
        }
    }
}

impl From<io::Error> for CompleteWritingError {
    fn from(value: io::Error) -> Self {
        CompleteWritingError::Io(value)
    }
}

impl From<io::Error> for WriteError {
    fn from(value: io::Error) -> Self {
        WriteError::Io(value)
    }
}

impl From<io::Error> for ReadError {
    fn from(value: io::Error) -> Self {
        ReadError::Io(value)
    }
}

impl std::error::Error for CompleteWritingError {}
impl std::error::Error for WriteError {}
impl std::error::Error for ReadError {}