taubyte_sdk/pubsub/node/
subscribe.rs

1use super::{imports, Channel};
2use crate::errno::Error;
3
4impl Channel {
5    fn subscribe_unsafe(&self) -> Error {
6        #[allow(unused_unsafe)]
7        unsafe {
8            imports::setSubscriptionChannel(self.name.as_ptr(), self.name.len())
9        }
10    }
11
12    pub fn subscribe(&self) -> Result<(), Box<dyn std::error::Error>> {
13        let err0 = self.subscribe_unsafe();
14        if err0.is_err() {
15            Err(format!(
16                "Subscribing to channel: `{}` failed with: {}",
17                self.name, err0
18            )
19            .into())
20        } else {
21            Ok(())
22        }
23    }
24}
25
26#[cfg(test)]
27pub mod test {
28    use crate::pubsub::Channel;
29    pub static NAME: &str = "testChannel";
30
31    #[test]
32    fn subscribe() {
33        let channel = Channel::new(NAME.to_string()).unwrap();
34        channel.subscribe().unwrap();
35    }
36}
37
38#[cfg(test)]
39#[allow(non_snake_case)]
40pub mod mock {
41    use super::test;
42    use crate::{
43        errno::{Errno, Error},
44        utils::test as utils,
45    };
46
47    pub fn setSubscriptionChannel(name_ptr: *const u8, name_size: usize) -> Error {
48        let name = utils::read_string(name_ptr, name_size);
49
50        if name != test::NAME {
51            Errno::ErrorCap.error()
52        } else {
53            Errno::ErrorNone.error()
54        }
55    }
56}