1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
extern crate rdkafka_sys as rdkafka;
use self::rdkafka::types::*;
use std::collections::HashMap;
use std::ffi::{CStr, CString};
use util::bytes_cstr_to_owned;
use error::{KafkaError, KafkaResult, IsError};
use client::Context;
const ERR_LEN: usize = 256;
unsafe extern "C" fn log_cb(
_: *const rdkafka::rd_kafka_s,
level: i32,
fac: *const i8,
buf: *const i8
) {
let fac_str = CStr::from_ptr(fac).to_string_lossy();
let buf_str = CStr::from_ptr(buf).to_string_lossy();
match level {
0 => error!("rdkafka {}: {}", fac_str, buf_str),
1 => error!("rdkafka {}: {}", fac_str, buf_str),
2 => error!("rdkafka {}: {}", fac_str, buf_str),
3 => error!("rdkafka {}: {}", fac_str, buf_str),
4 => warn!("rdkafka {}: {}", fac_str, buf_str),
5 => info!("rdkafka {}: {}", fac_str, buf_str),
6 => info!("rdkafka {}: {}", fac_str, buf_str),
_ => debug!("rdkafka {}: {}", fac_str, buf_str),
}
}
#[derive(Clone)]
pub struct ClientConfig {
conf_map: HashMap<String, String>,
default_topic_config: Option<TopicConfig>,
}
impl ClientConfig {
pub fn new() -> ClientConfig {
ClientConfig {
conf_map: HashMap::new(),
default_topic_config: None,
}
}
pub fn set<'a>(&'a mut self, key: &str, value: &str) -> &'a mut ClientConfig {
self.conf_map.insert(key.to_string(), value.to_string());
self
}
pub fn set_default_topic_config<'a>(&'a mut self, default_topic_config: TopicConfig) -> &'a mut ClientConfig {
self.default_topic_config = Some(default_topic_config);
self
}
pub fn create_native_config(&self) -> KafkaResult<*mut RDKafkaConf> {
let conf = unsafe { rdkafka::rd_kafka_conf_new() };
let errstr = [0; ERR_LEN];
for (key, value) in &self.conf_map {
let key_c = try!(CString::new(key.to_string()));
let value_c = try!(CString::new(value.to_string()));
let ret = unsafe {
rdkafka::rd_kafka_conf_set(conf, key_c.as_ptr(), value_c.as_ptr(),
errstr.as_ptr() as *mut i8, errstr.len())
};
if ret.is_error() {
let descr = unsafe { bytes_cstr_to_owned(&errstr) };
return Err(KafkaError::ClientConfig((ret, descr, key.to_string(), value.to_string())));
}
}
if let Some(topic_config) = self.create_native_default_topic_config() {
if let Err(e) = topic_config {
return Err(e);
} else {
unsafe { rdkafka::rd_kafka_conf_set_default_topic_conf(conf, topic_config.unwrap()) };
}
}
unsafe { rdkafka::rd_kafka_conf_set_log_cb(conf, Some(log_cb)) };
Ok(conf)
}
fn create_native_default_topic_config(&self) -> Option<KafkaResult<*mut RDKafkaTopicConf>> {
self.default_topic_config.as_ref().map(|c| c.create_native_config())
}
pub fn config_clone(&self) -> ClientConfig {
(*self).clone()
}
pub fn create<T: FromClientConfig>(&self) -> KafkaResult<T> {
T::from_config(self)
}
pub fn create_with_context<C: Context, T: FromClientConfigAndContext<C>>(&self, context: C) -> KafkaResult<T> {
T::from_config_and_context(self, context)
}
}
pub trait FromClientConfig: Sized {
fn from_config(&ClientConfig) -> KafkaResult<Self>;
}
pub trait FromClientConfigAndContext<C: Context>: Sized {
fn from_config_and_context(&ClientConfig, C) -> KafkaResult<Self>;
}
#[derive(Clone)]
pub struct TopicConfig {
conf_map: HashMap<String, String>,
}
impl TopicConfig {
pub fn new() -> TopicConfig {
TopicConfig {
conf_map: HashMap::new(),
}
}
pub fn set(&mut self, key: &str, value: &str) -> &mut TopicConfig {
self.conf_map.insert(key.to_string(), value.to_string());
self
}
pub fn finalize(&self) -> TopicConfig {
TopicConfig { conf_map: self.conf_map.clone() }
}
pub fn create_native_config(&self) -> KafkaResult<*mut RDKafkaTopicConf> {
let config_ptr = unsafe { rdkafka::rd_kafka_topic_conf_new() };
let errstr = [0; ERR_LEN];
for (name, value) in &self.conf_map {
let name_c = try!(CString::new(name.to_string()));
let value_c = try!(CString::new(value.to_string()));
let ret = unsafe {
rdkafka::rd_kafka_topic_conf_set(config_ptr, name_c.as_ptr(), value_c.as_ptr(),
errstr.as_ptr() as *mut i8, errstr.len())
};
if ret.is_error() {
let descr = unsafe { bytes_cstr_to_owned(&errstr) };
return Err(KafkaError::TopicConfig((ret, descr, name.to_string(), value.to_string())));
}
}
Ok(config_ptr)
}
}