1use pyo3::{exceptions::PyRuntimeError, DowncastError, PyErr};
2use serde::{de, ser};
3use std::fmt::{self, Display};
4
5#[derive(Debug)]
7pub struct Error(pub PyErr);
8
9impl From<PyErr> for Error {
10 fn from(err: PyErr) -> Self {
11 Error(err)
12 }
13}
14
15impl From<DowncastError<'_, '_>> for Error {
16 fn from(err: DowncastError) -> Self {
17 let err: PyErr = err.into();
18 Error(err)
19 }
20}
21
22impl From<Error> for PyErr {
23 fn from(err: Error) -> Self {
24 err.0
25 }
26}
27
28impl ser::Error for Error {
29 fn custom<T: Display>(msg: T) -> Self {
30 Error(PyRuntimeError::new_err(msg.to_string()))
31 }
32}
33
34impl de::Error for Error {
35 fn custom<T: Display>(msg: T) -> Self {
36 Error(PyRuntimeError::new_err(msg.to_string()))
37 }
38}
39
40impl Display for Error {
41 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
42 self.0.fmt(formatter)
43 }
44}
45
46impl std::error::Error for Error {}
47
48pub type Result<T> = ::std::result::Result<T, Error>;