Skip to content

Commit

Permalink
add decode extra function
Browse files Browse the repository at this point in the history
  • Loading branch information
s1rius committed Dec 6, 2023
1 parent 4873857 commit c570f2a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ezlog-core/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,20 @@ pub fn decode_with_writer(
.map_err(|e| LogError::Parse(format!("{}", e)))
}

pub fn decode_header_and_extra(cursor: &mut Cursor<Vec<u8>>) -> Result<(Header, Option<String>)> {
let header = Header::decode(cursor)?;
let mut extra: Option<String> = None;
if header.has_extra() {
decode_with_fn(cursor, &None, &None, &header, |v, _| {
extra = String::from_utf8(v.clone())
.map(|x| Some(x))
.unwrap_or(None);
None
})
}
Ok((header, extra))
}

#[cfg(test)]
mod tests {
use std::fs;
Expand Down
4 changes: 4 additions & 0 deletions ezlog-core/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,10 @@ impl Header {
self.recorder_position > (self.length() + self.extra_len(config)) as u32
}

pub fn has_extra(&self) -> bool {
self.flag.contains(Flags::HAS_EXTRA)
}

#[inline]
fn extra_len(&self, config: &EZLogConfig) -> usize {
match &config.extra {
Expand Down

0 comments on commit c570f2a

Please sign in to comment.