Skip to main content

valkey_module/context/
info.rs

1use std::ffi::{CStr, CString};
2use std::ptr::{self, NonNull};
3
4use crate::Context;
5use crate::{raw, ValkeyString};
6
7pub struct ServerInfo {
8    ctx: *mut raw::RedisModuleCtx,
9    pub(crate) inner: *mut raw::RedisModuleServerInfoData,
10}
11
12impl Drop for ServerInfo {
13    fn drop(&mut self) {
14        unsafe { raw::RedisModule_FreeServerInfo.unwrap()(self.ctx, self.inner) };
15    }
16}
17
18impl ServerInfo {
19    /// Creates a new `ServerInfo` without requiring a context.
20    ///
21    /// The underlying Module API permits a NULL context for both
22    /// `GetServerInfo` and `FreeServerInfo`.
23    #[must_use]
24    pub fn new(section: &str) -> Self {
25        let section = CString::new(section).unwrap();
26        let inner =
27            unsafe { raw::RedisModule_GetServerInfo.unwrap()(ptr::null_mut(), section.as_ptr()) };
28        Self {
29            ctx: ptr::null_mut(),
30            inner,
31        }
32    }
33
34    /// Returns a field value as a `ValkeyString`.
35    ///
36    /// This works both with and without a context. When no context is
37    /// available, the returned `ValkeyString` will not be registered with the
38    /// auto memory mechanism, but Rust's `Drop` ensures proper cleanup.
39    pub fn field(&self, field: &str) -> Option<ValkeyString> {
40        let field = CString::new(field).unwrap();
41        let value = unsafe {
42            raw::RedisModule_ServerInfoGetField.unwrap()(self.ctx, self.inner, field.as_ptr())
43        };
44        if value.is_null() {
45            None
46        } else {
47            Some(ValkeyString::new(NonNull::new(self.ctx), value))
48        }
49    }
50
51    /// Returns a field value as a `&str`. Does not require a context.
52    ///
53    /// The returned string borrows from the `ServerInfo` data and is valid
54    /// for the lifetime of this `ServerInfo`.
55    pub fn field_c(&self, field: &str) -> Option<&str> {
56        let field = CString::new(field).unwrap();
57        let value =
58            unsafe { raw::RedisModule_ServerInfoGetFieldC.unwrap()(self.inner, field.as_ptr()) };
59        if value.is_null() {
60            None
61        } else {
62            unsafe { CStr::from_ptr(value) }.to_str().ok()
63        }
64    }
65
66    /// Returns a field value as a signed integer. Does not require a context.
67    pub fn field_signed(&self, field: &str) -> Option<i64> {
68        let field = CString::new(field).unwrap();
69        let mut err: std::os::raw::c_int = 0;
70        let value = unsafe {
71            raw::RedisModule_ServerInfoGetFieldSigned.unwrap()(self.inner, field.as_ptr(), &mut err)
72        };
73        if err != 0 {
74            None
75        } else {
76            Some(value)
77        }
78    }
79
80    /// Returns a field value as an unsigned integer. Does not require a context.
81    pub fn field_unsigned(&self, field: &str) -> Option<u64> {
82        let field = CString::new(field).unwrap();
83        let mut err: std::os::raw::c_int = 0;
84        let value = unsafe {
85            raw::RedisModule_ServerInfoGetFieldUnsigned.unwrap()(
86                self.inner,
87                field.as_ptr(),
88                &mut err,
89            )
90        };
91        if err != 0 {
92            None
93        } else {
94            Some(value)
95        }
96    }
97
98    /// Returns a field value as a double. Does not require a context.
99    pub fn field_double(&self, field: &str) -> Option<f64> {
100        let field = CString::new(field).unwrap();
101        let mut err: std::os::raw::c_int = 0;
102        let value = unsafe {
103            raw::RedisModule_ServerInfoGetFieldDouble.unwrap()(self.inner, field.as_ptr(), &mut err)
104        };
105        if err != 0 {
106            None
107        } else {
108            Some(value)
109        }
110    }
111}
112
113impl Context {
114    #[must_use]
115    pub fn server_info(&self, section: &str) -> ServerInfo {
116        let section = CString::new(section).unwrap();
117        let server_info = unsafe {
118            raw::RedisModule_GetServerInfo.unwrap()(
119                self.ctx,         // ctx
120                section.as_ptr(), // section
121            )
122        };
123
124        ServerInfo {
125            ctx: self.ctx,
126            inner: server_info,
127        }
128    }
129}