use std::{fmt::Display, io}; use pyo3::Python; #[derive(Debug)] #[allow(clippy::module_name_repetitions)] pub enum YtDlpError { ResponseParseError { error: serde_json::error::Error, }, PythonError { error: Box, kind: String, }, IoError { error: io::Error, }, } impl std::error::Error for YtDlpError {} impl Display for YtDlpError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { YtDlpError::ResponseParseError { error } => write!( f, include_str!("./python_json_decode_failed.error_msg"), error ), YtDlpError::PythonError { error, kind: _ } => write!(f, "Python error: {error}"), YtDlpError::IoError { error } => write!(f, "Io error: {error}"), } } } impl From for YtDlpError { fn from(value: serde_json::error::Error) -> Self { Self::ResponseParseError { error: value } } } impl From for YtDlpError { fn from(value: pyo3::PyErr) -> Self { Python::with_gil(|py| { let kind = value.get_type(py).to_string(); Self::PythonError { error: Box::new(value), kind, } }) } } impl From for YtDlpError { fn from(value: io::Error) -> Self { Self::IoError { error: value } } }