1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::fmt;

use serde::{de, ser};

/// Alias for a `Result` with error type [`nvim_oxi::Error`](Error).
pub type Result<T> = std::result::Result<T, Error>;

/// Error returned by `nvim-oxi` functions.
#[derive(thiserror::Error, Debug, Eq, PartialEq)]
pub enum Error {
    #[error(transparent)]
    NvimError(#[from] nvim_types::Error),

    #[error(transparent)]
    FromObjectError(#[from] nvim_types::FromObjectError),

    #[error(transparent)]
    BadUtf8Error(#[from] std::string::FromUtf8Error),

    #[error(transparent)]
    IntError(#[from] std::num::TryFromIntError),

    #[error("{0}")]
    SerializeError(String),

    #[error("{0}")]
    DeserializeError(String),

    #[error("FnMut called recursively")]
    LuaFunMutRecursiveCallback,

    #[error("FnOnce called more than once")]
    LuaFunOnceMoreThanOnce,

    #[error("Lua runtime error: {0}")]
    LuaRuntimeError(String),

    #[error("Lua memory error: {0}")]
    LuaMemoryError(String),

    #[error("{0}")]
    Other(String),
}

impl ser::Error for Error {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        Self::SerializeError(msg.to_string())
    }
}

impl de::Error for Error {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        Self::DeserializeError(msg.to_string())
    }
}

impl Error {
    pub(crate) fn custom(msg: impl fmt::Display) -> Self {
        Self::Other(msg.to_string())
    }
}