Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow binary field values #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

yu-re-ka
Copy link
Collaborator

@yu-re-ka yu-re-ka commented Jul 5, 2022

No description provided.

@yu-re-ka yu-re-ka mentioned this pull request Jul 5, 2022
@yu-re-ka yu-re-ka force-pushed the feature/binary-field-values branch from a7ed90e to eb262c6 Compare July 5, 2022 13:39
self.fields
.get(field)
.map(|v| str::from_utf8(v))
.transpose()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This transpose is weird to me. It seems weird that I have to check for the Utf8Error before even knowing if the field exists.

self.fields.get(field).map(|v| v.as_slice())
}

pub fn get_field_string(&self, field: &str) -> Result<Option<&str>, str::Utf8Error> {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be get_field_str because it is a borrow.

pub fn get_message(&self) -> Option<&str> {
self.fields.get("MESSAGE").map(|v| v.as_ref())
pub fn get_message(&self) -> Option<Cow<'_, str>> {
self.get_field_string_lossy("MESSAGE")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a huge fan of being lossy by default but up to you. It is supposed to be convenient so i get the desire.

Maybe at least add a warning to the docs?

Comment on lines +68 to 70
self.get_field_string_lossy("_SOURCE_REALTIME_TIMESTAMP")
.and_then(|v| v.parse::<i64>().ok())
.map(|v| JournalEntryTimestamp { timestamp_us: v })
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.get_field_string_lossy("_SOURCE_REALTIME_TIMESTAMP")
.and_then(|v| v.parse::<i64>().ok())
.map(|v| JournalEntryTimestamp { timestamp_us: v })
self.get_field_string("_SOURCE_REALTIME_TIMESTAMP").ok()
.flatten()
.and_then(|v| v.parse::<i64>().ok())
.map(|v| JournalEntryTimestamp { timestamp_us: v })

Minor performance note, if a string isn't valid utf8 it isn't going to be parsable as in integer so no need to allocate the string for the lossy version. (and same with the other time functions below)

Comment on lines +188 to +191
let mut name_value = b.splitn(2, |x| *x == b'=');
let name = String::from_utf8_lossy(name_value.next().unwrap()).into_owned();
let value = name_value.next().unwrap().to_vec();
fields.insert(name, value);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think only the from_raw_parts call is unsafe. It would be nice to move the rest out of the unsafe block to make it clear what the unsafe bit is.

@@ -226,7 +225,7 @@ impl JournalReader {

fields.insert(
"__CURSOR".to_string(),
cursor.to_string_lossy().into_owned(),
cursor.to_string_lossy().into_owned().as_bytes().to_vec(),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to go from CStrStringVec. It should be possible to go directly from CStrVec.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants