Skip to main content

mesa3d_util/
error.rs

1// Copyright 2025 Google
2// SPDX-License-Identifier: MIT
3
4use std::ffi::NulError;
5use std::io::Error as IoError;
6use std::num::ParseIntError;
7use std::num::TryFromIntError;
8use std::str::Utf8Error;
9
10use remain::sorted;
11use rustix::io::Errno as RustixError;
12use thiserror::Error;
13
14/// An error generated while using this crate.
15#[sorted]
16#[derive(Error, Debug)]
17pub enum MesaError {
18    /// An error with the MesaHandle
19    #[error("invalid Mesa handle")]
20    InvalidMesaHandle,
21    /// An input/output error occurred.
22    #[error("an input/output error occurred: {0}")]
23    IoError(IoError),
24    /// Nul crate error.
25    #[error("Nul Error occurred {0}")]
26    NulError(NulError),
27    /// An attempted integer parsing failed.
28    #[error("int parsing failed: {0}")]
29    ParseIntError(ParseIntError),
30    /// Rustix crate error.
31    #[error("The errno is {0}")]
32    RustixError(RustixError),
33    /// An attempted integer conversion failed.
34    #[error("int conversion failed: {0}")]
35    TryFromIntError(TryFromIntError),
36    /// The command is unsupported.
37    #[error("the requested function is not implemented")]
38    Unsupported,
39    /// Utf8 error.
40    #[error("an utf8 error occurred: {0}")]
41    Utf8Error(Utf8Error),
42    /// An error with a free form context, similar to anyhow
43    #[error("operation failed: {0}")]
44    WithContext(&'static str),
45}
46
47#[cfg(any(target_os = "android", target_os = "linux"))]
48impl From<RustixError> for MesaError {
49    fn from(e: RustixError) -> MesaError {
50        MesaError::RustixError(e)
51    }
52}
53
54impl From<NulError> for MesaError {
55    fn from(e: NulError) -> MesaError {
56        MesaError::NulError(e)
57    }
58}
59
60impl From<IoError> for MesaError {
61    fn from(e: IoError) -> MesaError {
62        MesaError::IoError(e)
63    }
64}
65
66impl From<ParseIntError> for MesaError {
67    fn from(e: ParseIntError) -> MesaError {
68        MesaError::ParseIntError(e)
69    }
70}
71
72impl From<TryFromIntError> for MesaError {
73    fn from(e: TryFromIntError) -> MesaError {
74        MesaError::TryFromIntError(e)
75    }
76}
77
78impl From<Utf8Error> for MesaError {
79    fn from(e: Utf8Error) -> MesaError {
80        MesaError::Utf8Error(e)
81    }
82}
83
84/// The result of an operation in this crate.
85pub type MesaResult<T> = std::result::Result<T, MesaError>;