aboutsummaryrefslogtreecommitdiffstats
path: root/src/error.rs
blob: b682e9dd6cb2867de7b51330aba7704081da4ef7 (plain) (blame)
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
/// Errors potentially returned by this crate.
#[derive(Debug)]
pub enum Error {
    /// eof
    EOF,

    /// failed to create ttyrec frame: got N bytes of data but ttyrec frames
    /// can be at most M bytes
    FrameTooBig { input: usize },

    /// failed to create ttyrec frame: got N seconds but ttyrec frames can be
    /// at most M seconds
    FrameTooLong { input: u64 },

    /// failed to read from input
    Read { source: std::io::Error },

    /// failed to write to output
    Write { source: std::io::Error },
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::EOF => write!(f, "eof"),
            Self::FrameTooBig { input } => write!(
                f,
                "failed to create ttyrec frame: got {} bytes of data, but \
                ttyrec frames can be at most {} bytes",
                input,
                u32::max_value()
            ),
            Self::FrameTooLong { input } => write!(
                f,
                "failed to create ttyrec frame: got {} seconds, but ttyrecs \
                can be at most {} seconds",
                input,
                u32::max_value()
            ),
            Self::Read { source } => {
                write!(f, "failed to read from input: {}", source)
            }
            Self::Write { source } => {
                write!(f, "failed to write to output: {}", source)
            }
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Read { source } | Self::Write { source } => Some(source),
            _ => None,
        }
    }
}

pub type Result<T> = std::result::Result<T, Error>;