druid_shell/
error.rs

1// Copyright 2019 The Druid Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Errors at the application shell level.
16
17use std::fmt;
18use std::sync::Arc;
19
20use crate::backend::error as backend;
21
22/// Shell errors.
23#[derive(Debug, Clone)]
24pub enum Error {
25    /// The Application instance has already been created.
26    ApplicationAlreadyExists,
27    /// Tried to use the application after it had been dropped.
28    ApplicationDropped,
29    /// The window has already been destroyed.
30    WindowDropped,
31    /// Platform specific error.
32    Platform(backend::Error),
33    /// Other miscellaneous error.
34    Other(Arc<anyhow::Error>),
35}
36
37impl fmt::Display for Error {
38    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
39        match self {
40            Error::ApplicationAlreadyExists => {
41                write!(f, "An application instance has already been created.")
42            }
43            Error::ApplicationDropped => {
44                write!(
45                    f,
46                    "The application this operation requires has been dropped."
47                )
48            }
49            Error::Platform(err) => fmt::Display::fmt(err, f),
50            Error::WindowDropped => write!(f, "The window has already been destroyed."),
51            Error::Other(s) => write!(f, "{s}"),
52        }
53    }
54}
55
56impl std::error::Error for Error {}
57
58impl From<anyhow::Error> for Error {
59    fn from(src: anyhow::Error) -> Error {
60        Error::Other(Arc::new(src))
61    }
62}
63
64impl From<backend::Error> for Error {
65    fn from(src: backend::Error) -> Error {
66        Error::Platform(src)
67    }
68}