Skip to main content

rustorb_dds/
error.rs

1//! Errors of the DCPS API.
2
3use std::fmt;
4
5/// Why a DCPS operation failed.
6#[derive(Debug)]
7#[non_exhaustive]
8pub enum Error {
9    /// The network could not be set up, a datagram could not be sent, or
10    /// the file of a PERSISTENT writer could not be read or written.
11    Io(std::io::Error),
12    /// A sample could not be serialized.
13    Serialize(rustorb_typesupport::Error),
14    /// A DynamicData sample could not be serialized, or a DynamicType
15    /// cannot be used for a topic.
16    Dynamic(rustorb_xtypes::Error),
17    /// The participant was deleted.
18    AlreadyDeleted,
19    /// No more entity ids are left in the participant.
20    OutOfResources,
21    /// The operation did not finish in time: a reliable writer had no
22    /// room in its history within `max_blocking_time`, or its readers
23    /// did not acknowledge in time.
24    Timeout,
25    /// The operation is not allowed in this state: unregistering an
26    /// instance that is not registered.
27    PreconditionNotMet,
28    /// Something not implemented or not built in was asked for.
29    Unsupported(&'static str),
30    /// DDS Security refused: the security documents could not be loaded
31    /// or verified, or the permissions do not allow the operation.
32    Security(String),
33}
34
35impl fmt::Display for Error {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match self {
38            Error::Io(e) => write!(f, "I/O error: {e}"),
39            Error::Serialize(e) => write!(f, "cannot serialize the sample: {e}"),
40            Error::Dynamic(e) => write!(f, "dynamic type or data: {e}"),
41            Error::AlreadyDeleted => f.write_str("the participant was deleted"),
42            Error::OutOfResources => f.write_str("no more entity ids"),
43            Error::Timeout => f.write_str("timed out"),
44            Error::PreconditionNotMet => f.write_str("the instance is not registered"),
45            Error::Unsupported(what) => write!(f, "not supported: {what}"),
46            Error::Security(what) => write!(f, "security: {what}"),
47        }
48    }
49}
50
51impl std::error::Error for Error {
52    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
53        match self {
54            Error::Io(e) => Some(e),
55            Error::Serialize(e) => Some(e),
56            Error::Dynamic(e) => Some(e),
57            _ => None,
58        }
59    }
60}
61
62impl From<std::io::Error> for Error {
63    fn from(e: std::io::Error) -> Self {
64        Error::Io(e)
65    }
66}
67
68impl From<rustorb_typesupport::Error> for Error {
69    fn from(e: rustorb_typesupport::Error) -> Self {
70        Error::Serialize(e)
71    }
72}
73
74impl From<rustorb_xtypes::Error> for Error {
75    fn from(e: rustorb_xtypes::Error) -> Self {
76        Error::Dynamic(e)
77    }
78}
79
80/// The result of a DCPS operation.
81pub type Result<T> = std::result::Result<T, Error>;