redis_module/context/
info.rs

1use std::ffi::CString;
2use std::ptr::NonNull;
3
4use crate::Context;
5use crate::{raw, RedisString};
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    pub fn field(&self, field: &str) -> Option<RedisString> {
20        let field = CString::new(field).unwrap();
21        let value = unsafe {
22            raw::RedisModule_ServerInfoGetField.unwrap()(self.ctx, self.inner, field.as_ptr())
23        };
24        if value.is_null() {
25            None
26        } else {
27            Some(RedisString::new(NonNull::new(self.ctx), value))
28        }
29    }
30}
31
32impl Context {
33    #[must_use]
34    pub fn server_info(&self, section: &str) -> ServerInfo {
35        let section = CString::new(section).unwrap();
36        let server_info = unsafe {
37            raw::RedisModule_GetServerInfo.unwrap()(
38                self.ctx,         // ctx
39                section.as_ptr(), // section
40            )
41        };
42
43        ServerInfo {
44            ctx: self.ctx,
45            inner: server_info,
46        }
47    }
48}