cyclonedds_sys/
dds_error.rs

1/*
2    Copyright 2020 Sojan James
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15*/
16
17use std::{error::Error, fmt};
18
19use crate::dds_return_t;
20
21#[derive(Debug, PartialEq, Clone)]
22pub enum DDSError {
23    DdsOk,
24    DdsError,
25    Unsupported,
26    BadParameter,
27    PreconditionNotMet,
28    OutOfResources,
29    NotEnabled,
30    ImmutablePolicy,
31    InconsistentPolicy,
32    AlreadyDeleted,
33    Timeout,
34    NoData,
35    IllegalOperation,
36    NotAllowedBySecurity,
37}
38
39impl Error for DDSError {}
40
41impl fmt::Display for DDSError {
42    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43        match self {
44            DDSError::DdsOk => write!(f, "OK"),
45            DDSError::DdsError => write!(f, "Unspecified Error"),
46            DDSError::Unsupported => write!(f, "Unsupported"),
47            DDSError::BadParameter => write!(f, "Bad parameter"),
48            DDSError::PreconditionNotMet => write!(f, "Preconditions not met"),
49            DDSError::OutOfResources => write!(f, "Out of resources"),
50            DDSError::NotEnabled => write!(f, "Not enabled"),
51            DDSError::ImmutablePolicy => write!(f, "Immutable policy"),
52            DDSError::InconsistentPolicy => write!(f, "Inconsistent polocy"),
53            DDSError::AlreadyDeleted => write!(f, "Already deleted"),
54            DDSError::Timeout => write!(f, "Timeout"),
55            DDSError::NoData => write!(f, "No Data"),
56            DDSError::IllegalOperation => write!(f, "Illegal operation"),
57            DDSError::NotAllowedBySecurity => write!(f, "Not allowed by security"),
58        }
59    }
60}
61
62/// These constants are defined in ddsrt/retcode.h. bindgen doesn't see these macros
63/// and hence they are redefined here.DDSError
64/// Bad things will happen if these go out of sync
65impl From<dds_return_t> for DDSError {
66    fn from(entity: dds_return_t) -> Self {
67        match Some(entity) {
68            Some(0) => DDSError::DdsOk,
69            Some(-1) => DDSError::DdsError,
70            Some(-2) => DDSError::Unsupported,
71            Some(-3) => DDSError::BadParameter,
72            Some(-4) => DDSError::PreconditionNotMet,
73            Some(-5) => DDSError::OutOfResources,
74            Some(-6) => DDSError::NotEnabled,
75            Some(-7) => DDSError::ImmutablePolicy,
76            Some(-8) => DDSError::InconsistentPolicy,
77            Some(-9) => DDSError::AlreadyDeleted,
78            Some(-10) => DDSError::Timeout,
79            Some(-11) => DDSError::NoData,
80            Some(-12) => DDSError::IllegalOperation,
81            Some(-13) => DDSError::NotAllowedBySecurity,
82            Some(x) if x > 0 => DDSError::DdsOk,
83            Some(x) if x < -13 => DDSError::DdsError,
84            None => DDSError::DdsError,
85            _ => DDSError::DdsError,
86        }
87    }
88}