tracing_flame/
error.rs

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
use std::fmt;
use std::path::PathBuf;

/// The error type for `tracing-flame`
#[derive(Debug)]
pub struct Error(pub(crate) Kind);

impl Error {
    pub(crate) fn report(&self) {
        let current_error: &dyn std::error::Error = self;
        let mut current_error = Some(current_error);
        let mut ind = 0;

        eprintln!("Error:");

        while let Some(error) = current_error {
            eprintln!("    {}: {}", ind, error);
            ind += 1;
            current_error = error.source();
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.0, f)
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match &self.0 {
            Kind::CreateFile { ref source, .. } => Some(source),
            Kind::FlushFile(ref source) => Some(source),
        }
    }
}

#[derive(Debug)]
pub(crate) enum Kind {
    CreateFile {
        source: std::io::Error,
        path: PathBuf,
    },
    FlushFile(std::io::Error),
}

impl fmt::Display for Kind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::CreateFile { path, .. } => {
                write!(f, "cannot create output file. path={}", path.display())
            }
            Self::FlushFile { .. } => write!(f, "cannot flush output buffer"),
        }
    }
}