Skip to content

Commit

Permalink
Merge pull request #10 from ihrwein/small-refacts
Browse files Browse the repository at this point in the history
Small refacts
  • Loading branch information
ihrwein committed Jan 24, 2016
2 parents c6e3003 + 062d949 commit 0e98f31
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 26 deletions.
4 changes: 2 additions & 2 deletions syslog-ng-common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "syslog-ng-common"
version = "0.4.0"
version = "0.5.0"
authors = ["Tibor Benke <[email protected]>"]
homepage = "https://github.com/ihrwein/syslog-ng-rs"
repository = "https://github.com/ihrwein/syslog-ng-rs"
Expand All @@ -10,4 +10,4 @@ license = "MIT/Apache-2.0"

[dependencies]
log = "0.3"
syslog-ng-sys = "0.2"
syslog-ng-sys = "^0.2.1"
38 changes: 18 additions & 20 deletions syslog-ng-common/src/logmsg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

use syslog_ng_sys::{LogTagId, NVHandle, logmsg};
use syslog_ng_sys::{LogTagId, logmsg};
use syslog_ng_sys::types::*;

use std::collections::BTreeMap;
Expand All @@ -18,6 +18,7 @@ use std::ffi::{CStr, CString};
#[cfg(test)]
mod test;

pub struct NVHandle(logmsg::NVHandle);
pub struct LogMessage(*mut logmsg::LogMessage);

impl Drop for LogMessage {
Expand Down Expand Up @@ -47,35 +48,24 @@ impl LogMessage {
pub fn get_value_handle(value_name: &str) -> NVHandle {
unsafe {
let name = CString::new(value_name).unwrap();
logmsg::log_msg_get_value_handle(name.as_ptr())
NVHandle(logmsg::log_msg_get_value_handle(name.as_ptr()))
}
}

pub fn get_value_by_name(&self, value_name: &str) -> &str {
unsafe {
let name = CString::new(value_name).unwrap();
let mut size: ssize_t = 0;
let value = logmsg::__log_msg_get_value_by_name(self.0, name.as_ptr(), &mut size);
LogMessage::c_char_to_str(value, size)
}
}

pub fn get_value(&self, handle: NVHandle) -> &str {
pub fn get<K: Into<NVHandle>>(&self, key: K) -> &str {
let handle = key.into();
unsafe {
let mut size: ssize_t = 0;
let value = logmsg::__log_msg_get_value(self.0, handle, &mut size);
let value = logmsg::__log_msg_get_value(self.0, handle.0, &mut size);
LogMessage::c_char_to_str(value, size)
}
}

pub fn set_value(&mut self, key: &str, value: &str) {
pub fn insert<K: Into<NVHandle>>(&mut self, key: K, value: &str) {
let handle = key.into();
unsafe {
let c_key = CString::new(key).unwrap();
let c_value = CString::new(value).unwrap();
logmsg::__log_msg_set_value_by_name(self.0,
c_key.as_ptr(),
c_value.as_ptr(),
value.len() as isize);
logmsg::log_msg_set_value(self.0, handle.0, c_value.as_ptr(), value.len() as isize);
}
}

Expand Down Expand Up @@ -125,7 +115,7 @@ extern "C" fn insert_tag_to_vec(_: *const logmsg::LogMessage,
false
}

extern "C" fn insert_kvpair_to_map(_: NVHandle,
extern "C" fn insert_kvpair_to_map(_: logmsg::NVHandle,
name: *const c_char,
value: *const c_char,
value_len: ssize_t,
Expand All @@ -139,3 +129,11 @@ extern "C" fn insert_kvpair_to_map(_: NVHandle,
}
false
}

impl<'a> Into<NVHandle> for &'a str {
fn into(self) -> NVHandle {
let name = CString::new(self).unwrap();
let handle = unsafe { logmsg::log_msg_get_value_handle(name.as_ptr()) };
NVHandle(handle)
}
}
4 changes: 2 additions & 2 deletions syslog-ng-common/src/logmsg/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn test_given_empty_log_msg_when_values_are_inserted_then_we_can_get_them_back()
values
};

logmsg.set_value("foo", "bar");
logmsg.set_value("qux", "baz");
logmsg.insert("foo", "bar");
logmsg.insert("qux", "baz");
assert_eq!(&expected_values, &logmsg.values());
}
4 changes: 2 additions & 2 deletions syslog-ng-common/tests/dummy-parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl ParserBuilder for DummyParserBuilder {
impl Parser for DummyParser {
fn parse(&mut self, message: &mut LogMessage, input: &str) -> bool {
debug!("Processing input in Rust Parser: {}", input);
message.set_value("input", input);
message.insert("input", input);
true
}
}
Expand All @@ -63,5 +63,5 @@ fn test_given_parser_implementation_when_it_receives_a_message_then_it_adds_a_sp
let input = "The quick brown ...";
let result = parser.parse(&mut msg, input);
assert!(result);
assert_eq!(msg.get_value_by_name("input"), input);
assert_eq!(msg.get("input"), input);
}

0 comments on commit 0e98f31

Please sign in to comment.