freeswitch_rs/
utils.rs

1use freeswitch_sys::switch_status_t;
2use std::{error::Error, fmt::Display};
3
4pub trait FSNewType {
5    type Inner;
6    fn from_ptr(ptr: Self::Inner) -> Self;
7    fn as_ptr(&self) -> Self::Inner;
8}
9
10// Errors
11#[derive(Debug)]
12pub struct FSError(switch_status_t);
13impl From<switch_status_t> for FSError {
14    fn from(value: switch_status_t) -> Self {
15        assert!(value != switch_status_t::SWITCH_STATUS_SUCCESS);
16        Self(value)
17    }
18}
19impl From<FSError> for switch_status_t {
20    fn from(value: FSError) -> Self {
21        value.0
22    }
23}
24
25impl Display for FSError {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        write!(f, "{:?}", self.0)
28    }
29}
30
31impl Error for FSError {}
32
33pub type Result<T> = std::result::Result<T, FSError>;
34
35// ---------
36macro_rules! call_with_meta_suffix {
37     ($func:ident, $($arg:expr),*) => {{
38        let loc = std::panic::Location::caller();
39        let file = CString::new(loc.file()).unwrap();
40        let line = loc.line() as i32;
41        let func = std::ptr::null();
42        $func($($arg),*, file.as_ptr(), func, line)
43     }}
44}
45
46macro_rules! call_with_meta_prefix {
47     ($func:ident, $($arg:expr),*) => {{
48        let loc = std::panic::Location::caller();
49        let file = CString::new(loc.file()).unwrap();
50        let line = loc.line() as i32;
51        let func = std::ptr::null();
52        $func(file.as_ptr(), func, line,$($arg),*)
53     }}
54}
55
56macro_rules! fs_new_type{
57    ($wrapper:ident, $inner:ty) => {
58        fs_new_type!($wrapper, $inner, derive());
59    };
60    ($wrapper:ident, $inner:ty, derive($($derive:path),*)) => {
61
62        #[derive(Debug $(, $derive)*)]
63        pub struct $wrapper($inner);
64
65        #[automatically_derived]
66        impl crate::utils::FSNewType for $wrapper {
67            type Inner = $inner;
68            fn from_ptr(ptr:$inner) -> Self {
69                Self(ptr)
70            }
71            fn as_ptr(&self) -> $inner {
72                self.0
73            }
74        }
75    };
76}
77
78macro_rules! fs_session_owned_type {
79    ($wrapper:ident, $inner:ty) => {
80        fs_session_owned_type!($wrapper, $inner, derive());
81    };
82    ($wrapper:ident, $inner:ty, derive($($derive:path),*)) => {
83
84        #[derive(Debug $(, $derive)*)]
85        pub struct $wrapper<'session>($inner, std::marker::PhantomData<&'session crate::core::Session>);
86
87        #[automatically_derived]
88        impl<'a> crate::utils::FSNewType for $wrapper<'a> {
89            type Inner = $inner;
90            fn from_ptr(ptr:$inner) -> Self {
91                Self(ptr, std::marker::PhantomData)
92            }
93            fn as_ptr(&self) -> $inner {
94                self.0
95            }
96        }
97    };
98}
99
100pub(crate) use call_with_meta_prefix;
101pub(crate) use call_with_meta_suffix;
102pub(crate) use fs_new_type;
103pub(crate) use fs_session_owned_type;