use rdsys;
use rdsys::types::*;
use std::ffi::CStr;
use std::slice;
use std::fmt;
pub struct GroupMemberInfo(RDKafkaGroupMemberInfo);
impl GroupMemberInfo {
pub fn id(&self) -> &str {
unsafe {
CStr::from_ptr(self.0.member_id).to_str()
.expect("Member id is not a valid UTF-8 string")
}
}
pub fn client_id(&self) -> &str {
unsafe {
CStr::from_ptr(self.0.client_id).to_str()
.expect("Client id is not a valid UTF-8 string")
}
}
pub fn client_host(&self) -> &str {
unsafe {
CStr::from_ptr(self.0.client_host).to_str()
.expect("Member host is not a valid UTF-8 string")
}
}
}
pub struct GroupInfo(RDKafkaGroupInfo);
impl GroupInfo {
pub fn name(&self) -> &str {
unsafe {
CStr::from_ptr(self.0.group).to_str()
.expect("Group name is not a valid UTF-8 string")
}
}
pub fn members(&self) -> &[GroupMemberInfo] {
unsafe { slice::from_raw_parts(self.0.members as *const GroupMemberInfo, self.0.member_cnt as usize) }
}
pub fn state(&self) -> &str {
unsafe {
CStr::from_ptr(self.0.state).to_str()
.expect("State is not a valid UTF-8 string")
}
}
pub fn protocol(&self) -> &str {
unsafe {
CStr::from_ptr(self.0.protocol).to_str()
.expect("Protocol name is not a valid UTF-8 string")
}
}
pub fn protocol_type(&self) -> &str {
unsafe {
CStr::from_ptr(self.0.protocol_type).to_str()
.expect("Protocol type is not a valid UTF-8 string")
}
}
}
impl fmt::Debug for GroupInfo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.name())
}
}
pub struct GroupList(*const RDKafkaGroupList);
impl GroupList {
pub fn from_ptr(ptr: *const RDKafkaGroupList) -> GroupList {
GroupList(ptr)
}
pub fn groups(&self) -> &[GroupInfo] {
unsafe { slice::from_raw_parts((*self.0).groups as *const GroupInfo, (*self.0).group_cnt as usize) }
}
}
impl Drop for GroupList {
fn drop(&mut self) {
unsafe { rdsys::rd_kafka_group_list_destroy(self.0) };
}
}