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
use super::Error;
use crate::diag::{Diagnostic, DiagnosticKind, Formatted, Formatter};
use crate::Parsed;

#[derive(Debug)]
pub struct IoError {
    schema_name: String,
    err: std::io::Error,
}

impl IoError {
    pub(crate) fn new<S>(schema_name: S, err: std::io::Error) -> Self
    where
        S: Into<String>,
    {
        IoError {
            schema_name: schema_name.into(),
            err,
        }
    }

    pub fn io_error(&self) -> &std::io::Error {
        &self.err
    }
}

impl Diagnostic for IoError {
    fn kind(&self) -> DiagnosticKind {
        DiagnosticKind::Error
    }

    fn schema_name(&self) -> &str {
        &self.schema_name
    }

    fn format<'a>(&'a self, parsed: &'a Parsed) -> Formatted<'a> {
        let mut fmt = Formatter::new(self, self.err.to_string());

        if let Some(schema) = parsed.get_schema(&self.schema_name) {
            fmt.note(format!("tried to read `{}`", schema.path().display()));
        }

        fmt.format()
    }
}

impl From<IoError> for Error {
    fn from(e: IoError) -> Self {
        Error::IoError(e)
    }
}