solace_rs/
lib.rs

1pub mod cache_session;
2pub mod context;
3pub mod message;
4pub mod session;
5pub(crate) mod util;
6
7use enum_primitive::*;
8use solace_rs_sys as ffi;
9use std::fmt::{self, Display};
10use thiserror::Error;
11
12pub use crate::context::Context;
13pub use crate::session::Session;
14
15// Generic error
16#[derive(Debug, Clone)]
17pub struct SolaceError;
18
19impl fmt::Display for SolaceError {
20    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21        write!(f, "Solace Error Occured!")
22    }
23}
24
25enum_from_primitive! {
26    #[derive(Debug, PartialEq, Eq)]
27    #[repr(u32)]
28    pub enum SolaceLogLevel {
29        Critical = ffi::solClient_log_level_SOLCLIENT_LOG_CRITICAL,
30        Error = ffi::solClient_log_level_SOLCLIENT_LOG_ERROR,
31        Warning = ffi::solClient_log_level_SOLCLIENT_LOG_WARNING,
32        Notice = ffi::solClient_log_level_SOLCLIENT_LOG_NOTICE,
33        Info = ffi::solClient_log_level_SOLCLIENT_LOG_INFO,
34        Debug = ffi::solClient_log_level_SOLCLIENT_LOG_DEBUG,
35    }
36}
37
38enum_from_primitive! {
39    #[derive(PartialEq, Eq)]
40    #[repr(i32)]
41    pub enum SolClientReturnCode {
42        Ok=ffi::solClient_returnCode_SOLCLIENT_OK,
43        WouldBlock=ffi::solClient_returnCode_SOLCLIENT_WOULD_BLOCK,
44        InProgress=ffi::solClient_returnCode_SOLCLIENT_IN_PROGRESS,
45        NotReady=ffi::solClient_returnCode_SOLCLIENT_NOT_READY,
46        EndOfStream=ffi::solClient_returnCode_SOLCLIENT_EOS,
47        NotFound=ffi::solClient_returnCode_SOLCLIENT_NOT_FOUND,
48        NoEvent=ffi::solClient_returnCode_SOLCLIENT_NOEVENT,
49        Incomplete=ffi::solClient_returnCode_SOLCLIENT_INCOMPLETE,
50        Rollback=ffi::solClient_returnCode_SOLCLIENT_ROLLBACK,
51        Fail=ffi::solClient_returnCode_SOLCLIENT_FAIL,
52    }
53}
54
55impl std::fmt::Display for SolClientReturnCode {
56    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57        match self {
58            SolClientReturnCode::Ok => write!(f, "Ok - The API call was successful."),
59            SolClientReturnCode::WouldBlock => write!(
60                f,
61                "WouldBlock - The API call would block, but non-blocking was requested."
62            ),
63            SolClientReturnCode::InProgress => write!(
64                f,
65                "InProgress - An API call is in progress (non-blocking mode)."
66            ),
67            SolClientReturnCode::NotReady => write!(f, "NotReady - The API could not complete as an object is not ready (for example, the Session is not connected)."),
68            SolClientReturnCode::EndOfStream => write!(f, "EndOfStream - A getNext on a structured container returned End-of-Stream."),
69            SolClientReturnCode::NotFound => write!(f, "NotFound - A get for a named field in a MAP was not found in the MAP."),
70            SolClientReturnCode::NoEvent => write!(f, "NoEvent - solClient_context_processEventsWait returns this if wait is zero and there is no event to process"),
71            SolClientReturnCode::Incomplete => write!(f, "Incomplete - The API call completed some, but not all, of the requested function."),
72            SolClientReturnCode::Rollback => write!(f, "Rollback - solClient_transactedSession_commit returns this when the transaction has been rolled back."),
73            SolClientReturnCode::Fail => write!(f, "Fail - The API call failed."),
74        }
75    }
76}
77
78impl std::fmt::Debug for SolClientReturnCode {
79    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80        write!(f, "{self}")
81    }
82}
83
84impl SolClientReturnCode {
85    pub(crate) fn from_raw(value: i32) -> Self {
86        match Self::from_i32(value) {
87            Some(rc) => rc,
88            None => Self::Fail,
89        }
90    }
91
92    pub fn is_ok(&self) -> bool {
93        *self == Self::Ok
94    }
95}
96
97#[derive(Debug)]
98pub struct SolClientSubCode {
99    pub subcode: u32,
100    pub error_string: String,
101}
102
103impl Display for SolClientSubCode {
104    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105        write!(f, "subcode: {} string: {}", self.subcode, self.error_string)
106    }
107}
108
109#[derive(Error, Debug)]
110pub enum ContextError {
111    #[error("context thread failed to initialize. SolClient return code: {0:?}")]
112    InitializationFailed(SolClientReturnCode, SolClientSubCode),
113}
114
115#[derive(Error, Debug)]
116pub enum SessionError {
117    #[error("session receieved arguments with null value")]
118    InvalidArgsNulError(#[from] std::ffi::NulError),
119    #[error("session failed to connect. SolClient return code: {0} subcode: {1}")]
120    ConnectionFailure(SolClientReturnCode, SolClientSubCode),
121    #[error("session failed to disconnect. SolClient return code: {0} subcode: {1}")]
122    DisconnectError(SolClientReturnCode, SolClientSubCode),
123    #[error("session failed to initialize. SolClient return code: {0} subcode: {1}")]
124    InitializationFailure(SolClientReturnCode, SolClientSubCode),
125    #[error("session failed to subscribe on topic. SolClient return code: {0} subcode: {1}")]
126    SubscriptionFailure(String, SolClientReturnCode, SolClientSubCode),
127    #[error("session failed to unsubscribe on topic. SolClient return code: {0} subcode: {1}")]
128    UnsubscriptionFailure(String, SolClientReturnCode, SolClientSubCode),
129    #[error("cache request failed")]
130    CacheRequestFailure(SolClientReturnCode, SolClientSubCode),
131    #[error("could not publish message. SolClient return code: {0}")]
132    PublishError(SolClientReturnCode, SolClientSubCode),
133    #[error("could not send request. SolClient return code: {0}")]
134    RequestError(SolClientReturnCode, SolClientSubCode),
135}