Skip to content

Commit

Permalink
fix: fixed sorting when some lines are empty
Browse files Browse the repository at this point in the history
  • Loading branch information
pamburus committed May 5, 2024
1 parent 57719e6 commit b0c3209
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ members = [".", "crate/encstr"]
[workspace.package]
repository = "https://github.com/pamburus/hl"
authors = ["Pavel Ivanov <[email protected]>"]
version = "0.29.2-alpha.3"
version = "0.29.2-alpha.4"
edition = "2021"
license = "MIT"

Expand Down
28 changes: 27 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ impl Options {
Self { raw, ..self }
}

#[cfg(test)]
fn with_sort(self, sort: bool) -> Self {
Self { sort, ..self }
}

#[cfg(test)]
fn with_input_info(self, input_info: Option<InputInfo>) -> Self {
Self { input_info, ..self }
Expand Down Expand Up @@ -1178,8 +1183,29 @@ mod tests {
assert_eq!(std::str::from_utf8(&output).unwrap(), format!("{}\n\n{}\n", L1, L2),);
}

#[test]
fn test_sort_with_blank_lines() {
let input = input(concat!(
r#"{"level":"debug","ts":"2024-01-25T19:10:20.435369+01:00","msg":"m2"}"#,
"\n\r\n",
r#"{"level":"debug","ts":"2024-01-25T19:09:16.860711+01:00","msg":"m1"}"#,
"\n",
));

let mut output = Vec::new();
let app = App::new(options().with_sort(true));
app.run(vec![input], &mut output).unwrap();
assert_eq!(
std::str::from_utf8(&output).unwrap(),
concat!(
"2024-01-25 18:09:16.860 |DBG| m1\n",
"2024-01-25 18:10:20.435 |DBG| m2\n",
),
);
}

fn input<S: Into<String>>(s: S) -> InputHolder {
InputHolder::new(InputReference::File("-".into()), Some(Box::new(Cursor::new(s.into()))))
InputHolder::new(InputReference::Stdin, Some(Box::new(Cursor::new(s.into()))))
}

fn options() -> Options {
Expand Down
2 changes: 2 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,8 @@ impl Indexer {
}
}
}
} else {
stat.add_invalid();
}
lines.push((ts.or(prev_ts), i as u32, offset));
offset += data_len as u32 + 1;
Expand Down
21 changes: 19 additions & 2 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl InputHolder {

pub fn open(self) -> io::Result<Input> {
match self.reference {
InputReference::Stdin => Ok(Input::new(self.reference, Box::new(stdin()))),
InputReference::Stdin => Ok(Input::new(self.reference.clone(), self.stdin())),
InputReference::File(path) => match self.stream {
Some(stream) => Input::open_stream(&path, stream),
None => Input::open(&path),
Expand All @@ -151,13 +151,19 @@ impl InputHolder {

pub fn index(self, indexer: &Indexer) -> Result<IndexedInput> {
match self.reference {
InputReference::Stdin => IndexedInput::open_sequential(self.reference.clone(), Box::new(stdin()), indexer),
InputReference::Stdin => IndexedInput::open_sequential(self.reference.clone(), self.stdin(), indexer),
InputReference::File(path) => match self.stream {
Some(stream) => IndexedInput::open_stream(&path, stream, indexer),
None => IndexedInput::open(&path, indexer),
},
}
}

fn stdin(self) -> InputStream {
self.stream
.map(|s| Box::new(ReadSeekToRead(s)) as InputStream)
.unwrap_or_else(|| Box::new(stdin()))
}
}

pub struct Input {
Expand Down Expand Up @@ -515,6 +521,17 @@ pub trait ReadSeek: Read + Seek {}

impl<T: Read + Seek> ReadSeek for T {}

pub struct ReadSeekToRead<T>(T);

impl<T> Read for ReadSeekToRead<T>
where
T: ReadSeek,
{
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.read(buf)
}
}

trait AsInputStream {
fn as_input_stream(self) -> InputStream;
}
Expand Down

0 comments on commit b0c3209

Please sign in to comment.