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    /// Input data or a data-bound visual option is invalid.
13    #[error("data error: {0}")]
14    Data(Diagnostic),
15    /// Layout, backend, font, or output rendering failed.
16    #[error("render error: {0}")]
17    Render(Diagnostic),
18    /// A failure with a separately observable external effect.
19    #[error(transparent)]
20    Operation(Box<OperationFailure>),
21    /// The requested operation was cancelled before completion.
22    #[error("operation cancelled")]
23    Cancelled,
24}
25
26impl Error {
27    pub(crate) fn with_prefix(self, prefix: &str) -> Self {
28        match self {
29            Self::Data(diagnostic) => Self::Data(diagnostic.with_prefix(prefix)),
30            Self::Render(diagnostic) => Self::Render(diagnostic.with_prefix(prefix)),
31            // The original OS error, cancellation and external effect retain
32            // their own identity and error chain.
33            other => other,
34        }
35    }
36
37    /// Inspect the original diagnostic of a data or rendering error.
38    pub fn diagnostic(&self) -> Option<&Diagnostic> {
39        match self {
40            Self::Data(diagnostic) | Self::Render(diagnostic) => Some(diagnostic),
41            Self::Operation(failure) => failure.cause().diagnostic(),
42            _ => None,
43        }
44    }
45}
46
47impl From<std::convert::Infallible> for Error {
48    fn from(value: std::convert::Infallible) -> Self {
49        match value {}
50    }
51}
52
53/// Result returned by plotting operations.
54pub type Result<T> = std::result::Result<T, Error>;