use std::fmt;
use rustorb_giop::{CompletionStatus, SystemExceptionBody};
use crate::stream::InStream;
pub const OMG_VMCID: u32 = 0x4f4d_0000;
macro_rules! system_exceptions {
($($variant:ident => $name:literal,)*) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[allow(non_camel_case_types, clippy::upper_case_acronyms)]
pub enum SystemExceptionKind {
$(
#[doc = concat!("`CORBA::", $name, "`.")]
$variant,
)*
}
impl SystemExceptionKind {
pub fn name(self) -> &'static str {
match self {
$(SystemExceptionKind::$variant => $name,)*
}
}
pub fn repository_id(self) -> &'static str {
match self {
$(SystemExceptionKind::$variant => concat!("IDL:omg.org/CORBA/", $name, ":1.0"),)*
}
}
pub fn from_repository_id(id: &str) -> Option<SystemExceptionKind> {
match id {
$(concat!("IDL:omg.org/CORBA/", $name, ":1.0") => Some(SystemExceptionKind::$variant),)*
_ => None,
}
}
}
};
}
system_exceptions! {
UNKNOWN => "UNKNOWN",
BAD_PARAM => "BAD_PARAM",
NO_MEMORY => "NO_MEMORY",
IMP_LIMIT => "IMP_LIMIT",
COMM_FAILURE => "COMM_FAILURE",
INV_OBJREF => "INV_OBJREF",
NO_PERMISSION => "NO_PERMISSION",
INTERNAL => "INTERNAL",
MARSHAL => "MARSHAL",
INITIALIZE => "INITIALIZE",
NO_IMPLEMENT => "NO_IMPLEMENT",
BAD_TYPECODE => "BAD_TYPECODE",
BAD_OPERATION => "BAD_OPERATION",
NO_RESOURCES => "NO_RESOURCES",
NO_RESPONSE => "NO_RESPONSE",
PERSIST_STORE => "PERSIST_STORE",
BAD_INV_ORDER => "BAD_INV_ORDER",
TRANSIENT => "TRANSIENT",
FREE_MEM => "FREE_MEM",
INV_IDENT => "INV_IDENT",
INV_FLAG => "INV_FLAG",
INTF_REPOS => "INTF_REPOS",
BAD_CONTEXT => "BAD_CONTEXT",
OBJ_ADAPTER => "OBJ_ADAPTER",
DATA_CONVERSION => "DATA_CONVERSION",
OBJECT_NOT_EXIST => "OBJECT_NOT_EXIST",
TRANSACTION_REQUIRED => "TRANSACTION_REQUIRED",
TRANSACTION_ROLLEDBACK => "TRANSACTION_ROLLEDBACK",
INVALID_TRANSACTION => "INVALID_TRANSACTION",
INV_POLICY => "INV_POLICY",
CODESET_INCOMPATIBLE => "CODESET_INCOMPATIBLE",
REBIND => "REBIND",
TIMEOUT => "TIMEOUT",
TRANSACTION_UNAVAILABLE => "TRANSACTION_UNAVAILABLE",
TRANSACTION_MODE => "TRANSACTION_MODE",
BAD_QOS => "BAD_QOS",
INVALID_ACTIVITY => "INVALID_ACTIVITY",
ACTIVITY_COMPLETED => "ACTIVITY_COMPLETED",
ACTIVITY_REQUIRED => "ACTIVITY_REQUIRED",
THREAD_CANCELLED => "THREAD_CANCELLED",
}
impl fmt::Display for SystemExceptionKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
pub mod minor {
use super::OMG_VMCID;
pub const BAD_SCHEME_NAME: u32 = OMG_VMCID | 7;
pub const BAD_ADDRESS: u32 = OMG_VMCID | 8;
pub const BAD_SCHEME_SPECIFIC_PART: u32 = OMG_VMCID | 9;
pub const STRING_TO_OBJECT_OTHER: u32 = OMG_VMCID | 10;
pub const WCHAR_TCS_NOT_KNOWN: u32 = OMG_VMCID | 23;
pub const UNLISTED_USER_EXCEPTION: u32 = OMG_VMCID | 1;
pub const ORB_SHUT_DOWN: u32 = OMG_VMCID | 4;
pub const NO_USABLE_PROFILE: u32 = OMG_VMCID | 2;
pub const REQUEST_CANCELLED: u32 = OMG_VMCID | 3;
pub const UNKNOWN_OPERATION: u32 = OMG_VMCID | 2;
pub const WOULD_DEADLOCK: u32 = OMG_VMCID | 3;
pub const NO_SUCH_OBJECT: u32 = OMG_VMCID | 1;
pub const POA_DISCARDING: u32 = OMG_VMCID | 1;
pub const POA_INACTIVE: u32 = OMG_VMCID | 1;
pub const SERVANT_ALREADY_ACTIVE: u32 = OMG_VMCID | 2;
pub const NO_DEFAULT_SERVANT: u32 = OMG_VMCID | 3;
pub const NO_SERVANT_MANAGER: u32 = OMG_VMCID | 4;
pub const DUPLICATE_POLICY: u32 = OMG_VMCID | 30;
pub const NIL_INITIAL_REFERENCE: u32 = OMG_VMCID | 27;
pub const SERVANT_MANAGER_ALREADY_SET: u32 = OMG_VMCID | 6;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SystemException {
pub repository_id: String,
pub minor: u32,
pub completed: CompletionStatus,
}
impl SystemException {
pub fn new(kind: SystemExceptionKind, minor: u32, completed: CompletionStatus) -> Self {
SystemException {
repository_id: kind.repository_id().to_owned(),
minor,
completed,
}
}
pub fn kind(&self) -> Option<SystemExceptionKind> {
SystemExceptionKind::from_repository_id(&self.repository_id)
}
pub fn is(&self, kind: SystemExceptionKind) -> bool {
self.kind() == Some(kind)
}
}
impl From<SystemExceptionBody> for SystemException {
fn from(body: SystemExceptionBody) -> Self {
SystemException {
repository_id: body.exception_id,
minor: body.minor_code_value,
completed: body.completion_status,
}
}
}
impl From<&SystemException> for SystemExceptionBody {
fn from(e: &SystemException) -> Self {
SystemExceptionBody {
exception_id: e.repository_id.clone(),
minor_code_value: e.minor,
completion_status: e.completed,
}
}
}
impl From<rustorb_cdr::Error> for SystemException {
fn from(_: rustorb_cdr::Error) -> Self {
SystemException::new(SystemExceptionKind::MARSHAL, 0, CompletionStatus::No)
}
}
impl fmt::Display for SystemException {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = match self.kind() {
Some(kind) => kind.name(),
None => &self.repository_id,
};
let completed = match self.completed {
CompletionStatus::Yes => "COMPLETED_YES",
CompletionStatus::No => "COMPLETED_NO",
CompletionStatus::Maybe => "COMPLETED_MAYBE",
};
write!(f, "CORBA::{name} (minor {:#010x}, {completed})", self.minor)
}
}
impl std::error::Error for SystemException {}
#[derive(Debug, Clone)]
pub struct UserException {
pub(crate) repository_id: String,
pub(crate) reply: crate::Reply,
}
impl UserException {
pub fn repository_id(&self) -> &str {
&self.repository_id
}
pub fn input(&self) -> Result<InStream<'_>, SystemException> {
let mut input = self.reply.input();
input.read_string()?;
Ok(input)
}
}
impl fmt::Display for UserException {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "user exception {}", self.repository_id)
}
}
#[derive(Debug, Clone)]
pub enum Exception {
System(SystemException),
User(UserException),
}
impl Exception {
pub fn as_system(&self) -> Option<&SystemException> {
match self {
Exception::System(e) => Some(e),
Exception::User(_) => None,
}
}
pub fn as_user(&self) -> Option<&UserException> {
match self {
Exception::User(e) => Some(e),
Exception::System(_) => None,
}
}
pub fn repository_id(&self) -> &str {
match self {
Exception::System(e) => &e.repository_id,
Exception::User(e) => &e.repository_id,
}
}
}
impl From<SystemException> for Exception {
fn from(e: SystemException) -> Self {
Exception::System(e)
}
}
impl fmt::Display for Exception {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Exception::System(e) => e.fmt(f),
Exception::User(e) => e.fmt(f),
}
}
}
impl std::error::Error for Exception {}