Skip to main content

tao/
error.rs

1// Copyright 2014-2021 The winit contributors
2// Copyright 2021-2023 Tauri Programme within The Commons Conservancy
3// SPDX-License-Identifier: Apache-2.0
4
5//! The `Error` struct and associated types.
6use std::{error, fmt};
7
8use crate::platform_impl;
9
10/// An error whose cause it outside Tao's control.
11#[non_exhaustive]
12#[derive(Debug)]
13pub enum ExternalError {
14  /// The operation is not supported by the backend.
15  NotSupported(NotSupportedError),
16  /// The OS cannot perform the operation.
17  Os(OsError),
18}
19
20/// The error type for when the requested operation is not supported by the backend.
21#[derive(Clone)]
22pub struct NotSupportedError {
23  _marker: (),
24}
25
26/// The error type for when the OS cannot perform the requested operation.
27#[derive(Debug)]
28pub struct OsError {
29  line: u32,
30  file: &'static str,
31  error: platform_impl::OsError,
32}
33
34impl NotSupportedError {
35  #[inline]
36  #[allow(dead_code)]
37  pub(crate) fn new() -> NotSupportedError {
38    NotSupportedError { _marker: () }
39  }
40}
41
42impl OsError {
43  #[allow(dead_code)]
44  pub(crate) fn new(line: u32, file: &'static str, error: platform_impl::OsError) -> OsError {
45    OsError { line, file, error }
46  }
47}
48
49#[allow(unused_macros)]
50macro_rules! os_error {
51  ($error:expr) => {{
52    crate::error::OsError::new(line!(), file!(), $error)
53  }};
54}
55
56impl fmt::Display for OsError {
57  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
58    f.pad(&format!(
59      "os error at {}:{}: {}",
60      self.file, self.line, self.error
61    ))
62  }
63}
64
65impl fmt::Display for ExternalError {
66  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
67    match self {
68      ExternalError::NotSupported(e) => e.fmt(f),
69      ExternalError::Os(e) => e.fmt(f),
70    }
71  }
72}
73
74impl fmt::Debug for NotSupportedError {
75  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
76    f.debug_struct("NotSupportedError").finish()
77  }
78}
79
80impl fmt::Display for NotSupportedError {
81  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
82    f.pad("the requested operation is not supported by Tao")
83  }
84}
85
86impl error::Error for OsError {}
87impl error::Error for ExternalError {}
88impl error::Error for NotSupportedError {}