1use std::fmt;
2
3use rustorb_giop::{CompletionStatus, SystemExceptionBody};
4
5use crate::stream::InStream;
6
7pub const OMG_VMCID: u32 = 0x4f4d_0000;
10
11macro_rules! system_exceptions {
12 ($($variant:ident => $name:literal,)*) => {
13 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15 #[allow(non_camel_case_types, clippy::upper_case_acronyms)]
16 pub enum SystemExceptionKind {
17 $(
18 #[doc = concat!("`CORBA::", $name, "`.")]
19 $variant,
20 )*
21 }
22
23 impl SystemExceptionKind {
24 pub fn name(self) -> &'static str {
26 match self {
27 $(SystemExceptionKind::$variant => $name,)*
28 }
29 }
30
31 pub fn repository_id(self) -> &'static str {
33 match self {
34 $(SystemExceptionKind::$variant => concat!("IDL:omg.org/CORBA/", $name, ":1.0"),)*
35 }
36 }
37
38 pub fn from_repository_id(id: &str) -> Option<SystemExceptionKind> {
40 match id {
41 $(concat!("IDL:omg.org/CORBA/", $name, ":1.0") => Some(SystemExceptionKind::$variant),)*
42 _ => None,
43 }
44 }
45 }
46 };
47}
48
49system_exceptions! {
50 UNKNOWN => "UNKNOWN",
51 BAD_PARAM => "BAD_PARAM",
52 NO_MEMORY => "NO_MEMORY",
53 IMP_LIMIT => "IMP_LIMIT",
54 COMM_FAILURE => "COMM_FAILURE",
55 INV_OBJREF => "INV_OBJREF",
56 NO_PERMISSION => "NO_PERMISSION",
57 INTERNAL => "INTERNAL",
58 MARSHAL => "MARSHAL",
59 INITIALIZE => "INITIALIZE",
60 NO_IMPLEMENT => "NO_IMPLEMENT",
61 BAD_TYPECODE => "BAD_TYPECODE",
62 BAD_OPERATION => "BAD_OPERATION",
63 NO_RESOURCES => "NO_RESOURCES",
64 NO_RESPONSE => "NO_RESPONSE",
65 PERSIST_STORE => "PERSIST_STORE",
66 BAD_INV_ORDER => "BAD_INV_ORDER",
67 TRANSIENT => "TRANSIENT",
68 FREE_MEM => "FREE_MEM",
69 INV_IDENT => "INV_IDENT",
70 INV_FLAG => "INV_FLAG",
71 INTF_REPOS => "INTF_REPOS",
72 BAD_CONTEXT => "BAD_CONTEXT",
73 OBJ_ADAPTER => "OBJ_ADAPTER",
74 DATA_CONVERSION => "DATA_CONVERSION",
75 OBJECT_NOT_EXIST => "OBJECT_NOT_EXIST",
76 TRANSACTION_REQUIRED => "TRANSACTION_REQUIRED",
77 TRANSACTION_ROLLEDBACK => "TRANSACTION_ROLLEDBACK",
78 INVALID_TRANSACTION => "INVALID_TRANSACTION",
79 INV_POLICY => "INV_POLICY",
80 CODESET_INCOMPATIBLE => "CODESET_INCOMPATIBLE",
81 REBIND => "REBIND",
82 TIMEOUT => "TIMEOUT",
83 TRANSACTION_UNAVAILABLE => "TRANSACTION_UNAVAILABLE",
84 TRANSACTION_MODE => "TRANSACTION_MODE",
85 BAD_QOS => "BAD_QOS",
86 INVALID_ACTIVITY => "INVALID_ACTIVITY",
87 ACTIVITY_COMPLETED => "ACTIVITY_COMPLETED",
88 ACTIVITY_REQUIRED => "ACTIVITY_REQUIRED",
89 THREAD_CANCELLED => "THREAD_CANCELLED",
90}
91
92impl fmt::Display for SystemExceptionKind {
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94 f.write_str(self.name())
95 }
96}
97
98pub mod minor {
101 use super::OMG_VMCID;
102
103 pub const BAD_SCHEME_NAME: u32 = OMG_VMCID | 7;
105 pub const BAD_ADDRESS: u32 = OMG_VMCID | 8;
107 pub const BAD_SCHEME_SPECIFIC_PART: u32 = OMG_VMCID | 9;
110 pub const STRING_TO_OBJECT_OTHER: u32 = OMG_VMCID | 10;
112 pub const WCHAR_TCS_NOT_KNOWN: u32 = OMG_VMCID | 23;
115 pub const UNLISTED_USER_EXCEPTION: u32 = OMG_VMCID | 1;
118 pub const ORB_SHUT_DOWN: u32 = OMG_VMCID | 4;
120 pub const NO_USABLE_PROFILE: u32 = OMG_VMCID | 2;
122 pub const REQUEST_CANCELLED: u32 = OMG_VMCID | 3;
124 pub const UNKNOWN_OPERATION: u32 = OMG_VMCID | 2;
127 pub const WOULD_DEADLOCK: u32 = OMG_VMCID | 3;
130 pub const NO_SUCH_OBJECT: u32 = OMG_VMCID | 1;
133 pub const POA_DISCARDING: u32 = OMG_VMCID | 1;
135 pub const POA_INACTIVE: u32 = OMG_VMCID | 1;
137 pub const SERVANT_ALREADY_ACTIVE: u32 = OMG_VMCID | 2;
140 pub const NO_DEFAULT_SERVANT: u32 = OMG_VMCID | 3;
143 pub const NO_SERVANT_MANAGER: u32 = OMG_VMCID | 4;
146 pub const DUPLICATE_POLICY: u32 = OMG_VMCID | 30;
149 pub const NIL_INITIAL_REFERENCE: u32 = OMG_VMCID | 27;
152 pub const SERVANT_MANAGER_ALREADY_SET: u32 = OMG_VMCID | 6;
154}
155
156#[derive(Debug, Clone, PartialEq, Eq)]
159pub struct SystemException {
160 pub repository_id: String,
162 pub minor: u32,
164 pub completed: CompletionStatus,
166}
167
168impl SystemException {
169 pub fn new(kind: SystemExceptionKind, minor: u32, completed: CompletionStatus) -> Self {
171 SystemException {
172 repository_id: kind.repository_id().to_owned(),
173 minor,
174 completed,
175 }
176 }
177
178 pub fn kind(&self) -> Option<SystemExceptionKind> {
180 SystemExceptionKind::from_repository_id(&self.repository_id)
181 }
182
183 pub fn is(&self, kind: SystemExceptionKind) -> bool {
185 self.kind() == Some(kind)
186 }
187}
188
189impl From<SystemExceptionBody> for SystemException {
190 fn from(body: SystemExceptionBody) -> Self {
191 SystemException {
192 repository_id: body.exception_id,
193 minor: body.minor_code_value,
194 completed: body.completion_status,
195 }
196 }
197}
198
199impl From<&SystemException> for SystemExceptionBody {
200 fn from(e: &SystemException) -> Self {
201 SystemExceptionBody {
202 exception_id: e.repository_id.clone(),
203 minor_code_value: e.minor,
204 completion_status: e.completed,
205 }
206 }
207}
208
209impl From<rustorb_cdr::Error> for SystemException {
211 fn from(_: rustorb_cdr::Error) -> Self {
212 SystemException::new(SystemExceptionKind::MARSHAL, 0, CompletionStatus::No)
213 }
214}
215
216impl fmt::Display for SystemException {
217 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
218 let name = match self.kind() {
219 Some(kind) => kind.name(),
220 None => &self.repository_id,
221 };
222 let completed = match self.completed {
223 CompletionStatus::Yes => "COMPLETED_YES",
224 CompletionStatus::No => "COMPLETED_NO",
225 CompletionStatus::Maybe => "COMPLETED_MAYBE",
226 };
227 write!(f, "CORBA::{name} (minor {:#010x}, {completed})", self.minor)
228 }
229}
230
231impl std::error::Error for SystemException {}
232
233#[derive(Debug, Clone)]
236pub struct UserException {
237 pub(crate) repository_id: String,
238 pub(crate) reply: crate::Reply,
239}
240
241impl UserException {
242 pub fn repository_id(&self) -> &str {
244 &self.repository_id
245 }
246
247 pub fn input(&self) -> Result<InStream<'_>, SystemException> {
249 let mut input = self.reply.input();
250 input.read_string()?;
251 Ok(input)
252 }
253}
254
255impl fmt::Display for UserException {
256 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
257 write!(f, "user exception {}", self.repository_id)
258 }
259}
260
261#[derive(Debug, Clone)]
263pub enum Exception {
264 System(SystemException),
266 User(UserException),
268}
269
270impl Exception {
271 pub fn as_system(&self) -> Option<&SystemException> {
273 match self {
274 Exception::System(e) => Some(e),
275 Exception::User(_) => None,
276 }
277 }
278
279 pub fn as_user(&self) -> Option<&UserException> {
281 match self {
282 Exception::User(e) => Some(e),
283 Exception::System(_) => None,
284 }
285 }
286
287 pub fn repository_id(&self) -> &str {
289 match self {
290 Exception::System(e) => &e.repository_id,
291 Exception::User(e) => &e.repository_id,
292 }
293 }
294}
295
296impl From<SystemException> for Exception {
297 fn from(e: SystemException) -> Self {
298 Exception::System(e)
299 }
300}
301
302impl fmt::Display for Exception {
303 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
304 match self {
305 Exception::System(e) => e.fmt(f),
306 Exception::User(e) => e.fmt(f),
307 }
308 }
309}
310
311impl std::error::Error for Exception {}