piet/
error.rs

1// Copyright 2019 the Piet Authors
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4//! The common error type for piet operations.
5
6use std::fmt;
7
8/// An error that can occur while rendering 2D graphics.
9#[derive(Debug)]
10#[non_exhaustive]
11pub enum Error {
12    /// A function was passed an invalid input.
13    InvalidInput,
14    /// Something is impossible on the current platform.
15    NotSupported,
16    /// Something is possible, but not yet implemented.
17    Unimplemented,
18    /// Piet was compiled without a required feature.
19    MissingFeature(&'static str),
20    /// A stack pop failed.
21    StackUnbalance,
22    /// The backend failed unexpectedly.
23    BackendError(Box<dyn std::error::Error>),
24    /// A font could not be found.
25    MissingFont,
26    /// Font data could not be loaded.
27    FontLoadingFailed,
28    /// The arguments provided to the CLI were invalid.
29    #[cfg(feature = "samples")]
30    InvalidSampleArgs,
31}
32
33impl fmt::Display for Error {
34    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35        match self {
36            Error::InvalidInput => write!(f, "Invalid input"),
37            Error::NotSupported => write!(f, "Not supported on the current backend"),
38            Error::StackUnbalance => write!(f, "Stack unbalanced"),
39            Error::MissingFont => write!(f, "A font could not be found"),
40            Error::FontLoadingFailed => write!(f, "A font could not be loaded"),
41            Error::Unimplemented => write!(
42                f,
43                "This functionality is not yet implemented for this backend"
44            ),
45            Error::MissingFeature(feature) => write!(f, "Missing feature '{feature}'"),
46            Error::BackendError(e) => {
47                write!(f, "Backend error: ")?;
48                e.fmt(f)
49            }
50            #[cfg(feature = "samples")]
51            Error::InvalidSampleArgs => write!(f, "Must pass either --all or a number"),
52        }
53    }
54}
55
56impl std::error::Error for Error {}
57
58impl From<Box<dyn std::error::Error>> for Error {
59    fn from(e: Box<dyn std::error::Error>) -> Error {
60        Error::BackendError(e)
61    }
62}