backend/
client_exceptions.rs

1//! Map [`dicom_ul::association::client::Error`] to Python exceptions.
2//!
3//! Reference: [Foreign Rust error types | Error handling — PyO3 user guide]
4//!
5//! [`dicom_ul::association::client::Error`]: dicom_ul::association::client::Error
6//! [Foreign Rust error types | Error handling — PyO3 user guide]: https://pyo3.rs/v0.22.2/function/error-handling#foreign-rust-error-types
7
8use dicom_ul::association::client::Error as ClientError;
9
10use pyo3::exceptions::{PyConnectionError, PyValueError};
11use pyo3::PyErr;
12
13/// Use composition to wrap the [`ClientError`] type in a struct that implements [`PyErr`].
14pub struct Error {
15    source: ClientError,
16}
17
18impl From<Error> for PyErr {
19    fn from(err: Error) -> PyErr {
20        match err.source {
21            ClientError::Rejected { .. } => PyValueError::new_err(err.source.to_string()),
22            _ => PyConnectionError::new_err(err.source.to_string()),
23        }
24    }
25}
26
27impl From<ClientError> for Error {
28    fn from(other: ClientError) -> Self {
29        Self { source: other }
30    }
31}
32
33/// This type automatically converts a [`ClientError`] to a Python exception when used by a function.
34pub type Result<T, E = Error> = std::result::Result<T, E>;