1use crate::errors::{Diagnostic, OperationFailure};
4
5#[derive(Debug, thiserror::Error)]
7#[non_exhaustive]
8pub enum Error {
9 #[error("I/O error: {0}")]
11 Io(#[from] std::io::Error),
12 #[error("{context}: I/O error at {}: {source}", path.display())]
14 InputIo {
15 context: String,
17 path: std::path::PathBuf,
19 #[source]
21 source: std::io::Error,
22 },
23 #[error("data error: {0}")]
25 Data(Diagnostic),
26 #[error("render error: {0}")]
28 Render(Diagnostic),
29 #[error(transparent)]
31 Operation(Box<OperationFailure>),
32 #[error("operation cancelled")]
34 Cancelled,
35}
36
37impl Error {
38 pub(crate) fn with_prefix(self, prefix: &str) -> Self {
39 match self {
40 Self::Data(diagnostic) => Self::Data(diagnostic.with_prefix(prefix)),
41 Self::Render(diagnostic) => Self::Render(diagnostic.with_prefix(prefix)),
42 Self::InputIo {
43 context,
44 path,
45 source,
46 } => Self::InputIo {
47 context: format!("{prefix}: {context}"),
48 path,
49 source,
50 },
51 other => other,
54 }
55 }
56
57 pub(crate) fn with_input_path(self, path: &std::path::Path) -> Self {
58 match self {
59 Self::Io(source) => Self::InputIo {
60 context: "reading local input".to_owned(),
61 path: path.to_owned(),
62 source,
63 },
64 other => other.with_prefix(&path.display().to_string()),
65 }
66 }
67
68 pub fn diagnostic(&self) -> Option<&Diagnostic> {
70 match self {
71 Self::Data(diagnostic) | Self::Render(diagnostic) => Some(diagnostic),
72 Self::Operation(failure) => failure.cause().diagnostic(),
73 _ => None,
74 }
75 }
76}
77
78impl From<std::convert::Infallible> for Error {
79 fn from(value: std::convert::Infallible) -> Self {
80 match value {}
81 }
82}
83
84pub type Result<T> = std::result::Result<T, Error>;