taubyte_sdk/http/event/
path.rs

1use super::{imports, Event};
2use crate::errno::Error;
3
4impl Event {
5    fn path_size_unsafe(&self, size: *mut usize) -> Error {
6        #[allow(unused_unsafe)]
7        unsafe {
8            imports::getHttpEventPathSize(self.event, size)
9        }
10    }
11
12    fn path_unsafe(&self, buf_ptr: *mut u8, buf_size: usize) -> Error {
13        #[allow(unused_unsafe)]
14        unsafe {
15            imports::getHttpEventPath(self.event, buf_ptr, buf_size)
16        }
17    }
18
19    pub fn path(&self) -> Result<String, Box<dyn std::error::Error>> {
20        let mut size: usize = 0;
21        let err0 = self.path_size_unsafe(&mut size);
22        if err0.is_err() {
23            return Err(format!("Getting path size failed with: {}", err0).into());
24        }
25
26        let mut buf = vec![0u8; size];
27        let err0 = self.path_unsafe(buf.as_mut_ptr(), size);
28        if err0.is_err() {
29            Err(format!("Getting path failed with: {}", err0).into())
30        } else {
31            Ok(String::from_utf8(buf)?)
32        }
33    }
34}
35
36#[cfg(test)]
37pub mod test {
38    use crate::http::Event;
39    pub static EXPECTED_ID: u32 = 0;
40    pub static EXPECTED_PATH: &str = "/test/v1";
41
42    #[test]
43    fn test_path() {
44        let event = Event { event: EXPECTED_ID };
45        let path = event.path().unwrap();
46        assert_eq!(path, EXPECTED_PATH);
47    }
48}