solace_rs/message/
destination.rs

1use super::{MessageError, Result};
2use enum_primitive::*;
3use solace_rs_sys as ffi;
4use std::convert::From;
5use std::ffi::{CStr, CString};
6
7enum_from_primitive! {
8    #[derive(Debug, PartialEq, Eq, Default)]
9    #[repr(i32)]
10    pub enum DestinationType {
11        #[default]
12        Null=ffi::solClient_destinationType_SOLCLIENT_NULL_DESTINATION,
13        Topic=ffi::solClient_destinationType_SOLCLIENT_TOPIC_DESTINATION,
14        Queue=ffi::solClient_destinationType_SOLCLIENT_QUEUE_DESTINATION,
15        TopicTemp=ffi::solClient_destinationType_SOLCLIENT_TOPIC_TEMP_DESTINATION,
16        QueueTemp=ffi::solClient_destinationType_SOLCLIENT_QUEUE_TEMP_DESTINATION,
17    }
18}
19
20impl DestinationType {
21    pub fn to_i32(&self) -> i32 {
22        match self {
23            Self::Null => ffi::solClient_destinationType_SOLCLIENT_NULL_DESTINATION,
24            Self::Topic => ffi::solClient_destinationType_SOLCLIENT_TOPIC_TEMP_DESTINATION,
25            Self::Queue => ffi::solClient_destinationType_SOLCLIENT_QUEUE_DESTINATION,
26            Self::TopicTemp => ffi::solClient_destinationType_SOLCLIENT_TOPIC_TEMP_DESTINATION,
27            Self::QueueTemp => ffi::solClient_destinationType_SOLCLIENT_QUEUE_TEMP_DESTINATION,
28        }
29    }
30}
31
32// rethink about this api
33// we need to be able to create a new owned MessageDestination
34// then pass that as value to the message builder
35// but then also be able to get a MessageDestination from a message.
36// Right now, it seems the best way to do that is with by copying the meessage destination field.
37#[derive(Debug)]
38pub struct MessageDestination {
39    pub dest_type: DestinationType,
40    pub dest: CString,
41}
42
43impl MessageDestination {
44    pub fn new<T: Into<Vec<u8>>>(dest_type: DestinationType, destination: T) -> Result<Self> {
45        let c_destination = CString::new(destination)
46            .map_err(|_| MessageError::FieldConvertionError("dest_type"))?;
47
48        Ok(MessageDestination {
49            dest_type,
50            dest: c_destination,
51        })
52    }
53}
54
55impl From<ffi::solClient_destination> for MessageDestination {
56    fn from(raw_dest: ffi::solClient_destination) -> Self {
57        let dest_type = DestinationType::from_i32(raw_dest.destType).unwrap_or_default();
58
59        let dest_cstr = unsafe { CStr::from_ptr(raw_dest.dest) };
60        let dest: CString = dest_cstr.into();
61
62        MessageDestination { dest_type, dest }
63    }
64}