Skip to main content

lichtung_plot/
error.rs

1//! Plot construction, layout, rendering, and output errors.
2
3use crate::errors::{Diagnostic, OperationFailure};
4
5/// Error returned by plotting operations.
6#[derive(Debug, thiserror::Error)]
7#[non_exhaustive]
8pub enum Error {
9    /// Filesystem input or output failed.
10    #[error("I/O error: {0}")]
11    Io(#[from] std::io::Error),
12    /// An adopted local input failed, retaining its actual path and OS cause.
13    #[error("{context}: I/O error at {}: {source}", path.display())]
14    InputIo {
15        /// Actual declaration context, when known.
16        context: String,
17        /// Local input path.
18        path: std::path::PathBuf,
19        /// Original I/O error, including its OS error number.
20        #[source]
21        source: std::io::Error,
22    },
23    /// Input data or a data-bound visual option is invalid.
24    #[error("data error: {0}")]
25    Data(Diagnostic),
26    /// Layout, backend, font, or output rendering failed.
27    #[error("render error: {0}")]
28    Render(Diagnostic),
29    /// A failure with a separately observable external effect.
30    #[error(transparent)]
31    Operation(Box<OperationFailure>),
32    /// The requested operation was cancelled before completion.
33    #[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            // The original OS error, cancellation and external effect retain
52            // their own identity and error chain.
53            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    /// Inspect the original diagnostic of a data or rendering error.
69    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
84/// Result returned by plotting operations.
85pub type Result<T> = std::result::Result<T, Error>;