Skip to content

Commit c79539e

Browse files
committed
Add get_ref() and get_mut() methods
1 parent 06cd413 commit c79539e

File tree

6 files changed

+102
-6
lines changed

6 files changed

+102
-6
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
v0.2.0 (in development)
2+
-----------------------
3+
- Gave `JsonLinesReader` and `JsonLinesWriter` new `get_ref()` and `get_mut()`
4+
methods
5+
6+
v0.1.0 (2022-10-28)
7+
-------------------
8+
Initial release

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "serde-jsonlines"
3-
version = "0.1.0"
3+
version = "0.2.0-dev"
44
edition = "2021"
55
rust-version = "1.56"
66
description = "Read & write JSON Lines documents"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![codecov.io](https://codecov.io/gh/jwodder/serde-jsonlines/branch/master/graph/badge.svg)](https://codecov.io/gh/jwodder/serde-jsonlines)
44
[![MIT License](https://img.shields.io/github/license/jwodder/serde-jsonlines.svg)](https://opensource.org/licenses/MIT)
55

6-
[GitHub](https://github.com/jwodder/serde-jsonlines) | [crates.io](https://crates.io/crates/serde-jsonlines) | [Documentation](https://docs.rs/serde-jsonlines) | [Issues](https://github.com/jwodder/serde-jsonlines/issues)
6+
[GitHub](https://github.com/jwodder/serde-jsonlines) | [crates.io](https://crates.io/crates/serde-jsonlines) | [Documentation](https://docs.rs/serde-jsonlines) | [Issues](https://github.com/jwodder/serde-jsonlines/issues) | [Changelog](https://github.com/jwodder/serde-jsonlines/blob/master/CHANGELOG.md)
77

88
JSON Lines (a.k.a. newline-delimited JSON) is a simple format for storing
99
sequences of JSON values in which each value is serialized on a single line and

src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ impl<W> JsonLinesWriter<W> {
139139
pub fn into_inner(self) -> W {
140140
self.inner
141141
}
142+
143+
/// Get a reference to the underlying writer
144+
pub fn get_ref(&self) -> &W {
145+
&self.inner
146+
}
147+
148+
/// Get a mutable reference to the underlying writer
149+
pub fn get_mut(&mut self) -> &mut W {
150+
&mut self.inner
151+
}
142152
}
143153

144154
impl<W: Write> JsonLinesWriter<W> {
@@ -267,6 +277,16 @@ impl<R> JsonLinesReader<R> {
267277
pub fn into_inner(self) -> R {
268278
self.inner
269279
}
280+
281+
/// Get a reference to the underlying reader
282+
pub fn get_ref(&self) -> &R {
283+
&self.inner
284+
}
285+
286+
/// Get a mutable reference to the underlying reader
287+
pub fn get_mut(&mut self) -> &mut R {
288+
&mut self.inner
289+
}
270290
}
271291

272292
impl<R: BufRead> JsonLinesReader<R> {

tests/json_lines_reader.rs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use assert_fs::fixture::FileTouch;
1+
use assert_fs::fixture::{FileTouch, FileWriteStr};
22
use assert_fs::NamedTempFile;
33
use serde_jsonlines::JsonLinesReader;
4-
use std::fs::File;
5-
use std::io::{BufRead, BufReader, ErrorKind, Result};
4+
use std::fs::{File, OpenOptions};
5+
use std::io::{BufRead, BufReader, ErrorKind, Result, Seek, SeekFrom, Write};
66
use std::path::Path;
77
mod common;
88
use common::*;
@@ -187,3 +187,43 @@ fn test_iter_invalid_json() {
187187
);
188188
assert!(items.next().is_none());
189189
}
190+
191+
#[test]
192+
fn test_read_then_write_then_read() {
193+
let tmpfile = NamedTempFile::new("test.jsonl").unwrap();
194+
tmpfile
195+
.write_str("{\"name\": \"Foo Bar\", \"on\":true,\"size\": 42 }\n")
196+
.unwrap();
197+
let fp = BufReader::new(
198+
OpenOptions::new()
199+
.read(true)
200+
.write(true)
201+
.open(&tmpfile)
202+
.unwrap(),
203+
);
204+
let mut reader = JsonLinesReader::new(fp);
205+
assert_eq!(
206+
reader.read::<Structure>().unwrap(),
207+
Some(Structure {
208+
name: "Foo Bar".into(),
209+
size: 42,
210+
on: true,
211+
})
212+
);
213+
assert_eq!(reader.read::<Structure>().unwrap(), None);
214+
let fp: &mut File = reader.get_mut().get_mut();
215+
let pos = fp.stream_position().unwrap();
216+
fp.write_all(b"{ \"name\":\"Quux\", \"on\" : false ,\"size\": 23}\n")
217+
.unwrap();
218+
fp.flush().unwrap();
219+
fp.seek(SeekFrom::Start(pos)).unwrap();
220+
assert_eq!(
221+
reader.read::<Structure>().unwrap(),
222+
Some(Structure {
223+
name: "Quux".into(),
224+
size: 23,
225+
on: false,
226+
})
227+
);
228+
assert_eq!(reader.read::<Structure>().unwrap(), None);
229+
}

tests/json_lines_writer.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use assert_fs::assert::PathAssert;
22
use assert_fs::NamedTempFile;
33
use serde_jsonlines::JsonLinesWriter;
44
use std::fs::File;
5-
use std::io::Write;
5+
use std::io::{Seek, SeekFrom, Write};
66
use std::iter::empty;
77
mod common;
88
use common::*;
@@ -109,3 +109,31 @@ fn test_write_all_none() {
109109
}
110110
tmpfile.assert("");
111111
}
112+
113+
#[test]
114+
fn test_write_then_back_up_then_write() {
115+
let tmpfile = NamedTempFile::new("test.jsonl").unwrap();
116+
{
117+
let fp = File::create(&tmpfile).unwrap();
118+
let mut writer = JsonLinesWriter::new(fp);
119+
writer
120+
.write(&Structure {
121+
name: "Foo Bar".into(),
122+
size: 42,
123+
on: true,
124+
})
125+
.unwrap();
126+
writer.flush().unwrap();
127+
let fp: &mut File = writer.get_mut();
128+
fp.seek(SeekFrom::Start(0)).unwrap();
129+
writer
130+
.write(&Structure {
131+
name: "Gnusto Cleesh".into(),
132+
size: 17,
133+
on: true,
134+
})
135+
.unwrap();
136+
writer.flush().unwrap();
137+
}
138+
tmpfile.assert("{\"name\":\"Gnusto Cleesh\",\"size\":17,\"on\":true}\n");
139+
}

0 commit comments

Comments
 (0)