-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Use `if let Some()` to deconstruct vs `is_some()` and `unwrap()` - Use RawFd over i32 for clarity - We don't need the Arc<Mutex<>> - Use a `BufWriter` (forgetting this is a big performance footgun in Rust) - Add a unit test of the basic code Signed-off-by: Colin Walters <[email protected]>
- Loading branch information
Showing
4 changed files
with
106 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,4 @@ pub mod spec; | |
mod docgen; | ||
mod glyph; | ||
mod imgstorage; | ||
mod progress_jsonl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
//! Output progress data using the json-lines format. For more information | ||
//! see <https://jsonlines.org/>. | ||
use std::fs; | ||
use std::io::{BufWriter, Write}; | ||
use std::os::fd::{FromRawFd, RawFd}; | ||
|
||
use anyhow::Result; | ||
use serde::Serialize; | ||
|
||
pub(crate) struct JsonlWriter { | ||
fd: BufWriter<fs::File>, | ||
} | ||
|
||
impl From<fs::File> for JsonlWriter { | ||
fn from(value: fs::File) -> Self { | ||
Self { | ||
fd: BufWriter::new(value), | ||
} | ||
} | ||
} | ||
|
||
impl JsonlWriter { | ||
/// Given a raw file descriptor, create an instance of a json-lines writer. | ||
#[allow(unsafe_code)] | ||
pub(crate) fn from_raw_fd(fd: RawFd) -> Self { | ||
unsafe { fs::File::from_raw_fd(fd) }.into() | ||
} | ||
|
||
/// Serialize the target object to JSON as a single line | ||
pub(crate) fn send<T: Serialize>(&mut self, v: T) -> Result<()> { | ||
// serde is guaranteed not to output newlines here | ||
serde_json::to_writer(&mut self.fd, &v)?; | ||
// We always end in a newline | ||
self.fd.write_all(b"\n")?; | ||
// And flush to ensure the remote side sees updates immediately | ||
self.fd.flush()?; | ||
Ok(()) | ||
} | ||
|
||
/// Flush remaining data and return the underlying file. | ||
#[allow(dead_code)] | ||
pub(crate) fn into_inner(self) -> Result<fs::File> { | ||
self.fd.into_inner().map_err(Into::into) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use std::io::{BufRead, BufReader, Seek}; | ||
|
||
use serde::Deserialize; | ||
|
||
use super::*; | ||
|
||
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)] | ||
struct S { | ||
s: String, | ||
v: u32, | ||
} | ||
|
||
#[test] | ||
fn test_jsonl() -> Result<()> { | ||
let tf = tempfile::tempfile()?; | ||
let mut w = JsonlWriter::from(tf); | ||
let testvalues = [ | ||
S { | ||
s: "foo".into(), | ||
v: 42, | ||
}, | ||
S { | ||
// Test with an embedded newline to sanity check that serde doesn't write it literally | ||
s: "foo\nbar".into(), | ||
v: 0, | ||
}, | ||
]; | ||
for value in &testvalues { | ||
w.send(value).unwrap(); | ||
} | ||
let mut tf = w.into_inner().unwrap(); | ||
tf.seek(std::io::SeekFrom::Start(0))?; | ||
let tf = BufReader::new(tf); | ||
for (line, expected) in tf.lines().zip(testvalues.iter()) { | ||
let line = line?; | ||
let found: S = serde_json::from_str(&line)?; | ||
assert_eq!(&found, expected); | ||
} | ||
Ok(()) | ||
} | ||
} |