Skip to main content

meshopt/
error.rs

1/// A type alias for handling errors throughout meshopt
2pub type Result<T> = std::result::Result<T, Error>;
3
4/// An error that can occur
5#[derive(Debug, thiserror::Error)]
6#[non_exhaustive]
7pub enum Error {
8    /// An error that occurred interfacing with native code through FFI.
9    #[error("native error: {0}")]
10    Native(i32),
11
12    /// An error that occurred while accessing or allocating memory
13    #[error("memory error: {0}")]
14    Memory(std::borrow::Cow<'static, str>),
15
16    /// An error that occurred while parsing a data source
17    #[error("parse error: {0}")]
18    Parse(String),
19
20    /// An error that occurred while working with a file path.
21    #[error("path error: {0}")]
22    Path(std::path::PathBuf),
23
24    /// Generally, these errors correspond to bugs in this library.
25    #[error("BUG: Please report this bug with a backtrace to https://github.com/gwihlidal/meshopt-rs\n{0}")]
26    Bug(String),
27
28    /// An error occurred while reading/writing a configuration
29    #[error("config error: {0}")]
30    Config(String),
31
32    /// An unexpected I/O error occurred.
33    #[error(transparent)]
34    Io(#[from] std::io::Error),
35    // An error occurred while parsing a number in a free-form query.
36    //Number,
37}
38
39impl Error {
40    #[inline]
41    pub(crate) fn memory(msg: &'static str) -> Self {
42        Self::Memory(std::borrow::Cow::Borrowed(msg))
43    }
44
45    #[inline]
46    pub(crate) fn memory_dynamic(msg: String) -> Self {
47        Self::Memory(std::borrow::Cow::Owned(msg))
48    }
49}
50
51#[inline]
52pub(crate) fn error_or<T>(code: i32, ok: T) -> Result<T> {
53    if code == 0 {
54        Ok(ok)
55    } else {
56        Err(Error::Native(code))
57    }
58}