Makes sure all writers flushes before the end

The API user is likely to write a lot of data with them. This way the API is more "foolproof"
pull/190/head
Tpt 3 years ago
parent 1f6c1db400
commit 4327f8e729
  1. 14
      lib/sparesults/src/csv.rs
  2. 4
      lib/sparesults/src/json.rs
  3. 8
      lib/sparesults/src/lib.rs
  4. 4
      lib/sparesults/src/xml.rs
  5. 11
      lib/src/io/write.rs

@ -55,8 +55,9 @@ impl<W: Write> CsvSolutionsWriter<W> {
Ok(()) Ok(())
} }
pub fn finish(self) -> W { pub fn finish(mut self) -> io::Result<W> {
self.sink self.sink.flush()?;
Ok(self.sink)
} }
} }
@ -145,8 +146,9 @@ impl<W: Write> TsvSolutionsWriter<W> {
Ok(()) Ok(())
} }
pub fn finish(self) -> W { pub fn finish(mut self) -> io::Result<W> {
self.sink self.sink.flush()?;
Ok(self.sink)
} }
} }
@ -319,7 +321,7 @@ mod tests {
.filter_map(|(v, s)| s.as_ref().map(|s| (v.as_ref(), s.as_ref()))), .filter_map(|(v, s)| s.as_ref().map(|s| (v.as_ref(), s.as_ref()))),
)?; )?;
} }
let result = writer.finish(); let result = writer.finish()?;
assert_eq!(str::from_utf8(&result).unwrap(), "x,literal\r\nhttp://example/x,String\r\nhttp://example/x,\"String-with-dquote\"\"\"\r\n_:b0,Blank node\r\n,Missing 'x'\r\n,\r\nhttp://example/x,\r\n_:b1,String-with-lang\r\n_:b1,123"); assert_eq!(str::from_utf8(&result).unwrap(), "x,literal\r\nhttp://example/x,String\r\nhttp://example/x,\"String-with-dquote\"\"\"\r\n_:b0,Blank node\r\n,Missing 'x'\r\n,\r\nhttp://example/x,\r\n_:b1,String-with-lang\r\n_:b1,123");
Ok(()) Ok(())
} }
@ -337,7 +339,7 @@ mod tests {
.filter_map(|(v, s)| s.as_ref().map(|s| (v.as_ref(), s.as_ref()))), .filter_map(|(v, s)| s.as_ref().map(|s| (v.as_ref(), s.as_ref()))),
)?; )?;
} }
let result = writer.finish(); let result = writer.finish()?;
assert_eq!(str::from_utf8(&result).unwrap(), "?x\t?literal\n<http://example/x>\t\"String\"\n<http://example/x>\t\"String-with-dquote\\\"\"\n_:b0\t\"Blank node\"\n\t\"Missing 'x'\"\n\t\n<http://example/x>\t\n_:b1\t\"String-with-lang\"@en\n_:b1\t123"); assert_eq!(str::from_utf8(&result).unwrap(), "?x\t?literal\n<http://example/x>\t\"String\"\n<http://example/x>\t\"String-with-dquote\\\"\"\n_:b0\t\"Blank node\"\n\t\"Missing 'x'\"\n\t\n<http://example/x>\t\n_:b1\t\"String-with-lang\"@en\n_:b1\t123");
Ok(()) Ok(())
} }

@ -66,7 +66,9 @@ impl<W: Write> JsonSolutionsWriter<W> {
self.writer.write_event(JsonEvent::EndArray)?; self.writer.write_event(JsonEvent::EndArray)?;
self.writer.write_event(JsonEvent::EndObject)?; self.writer.write_event(JsonEvent::EndObject)?;
self.writer.write_event(JsonEvent::EndObject)?; self.writer.write_event(JsonEvent::EndObject)?;
Ok(self.writer.into_inner()) let mut inner = self.writer.into_inner();
inner.flush()?;
Ok(inner)
} }
} }

@ -498,11 +498,11 @@ impl<W: Write> SolutionsWriter<W> {
/// Writes the last bytes of the file. /// Writes the last bytes of the file.
pub fn finish(self) -> io::Result<W> { pub fn finish(self) -> io::Result<W> {
Ok(match self.formatter { match self.formatter {
SolutionsWriterKind::Xml(write) => write.finish()?, SolutionsWriterKind::Xml(write) => write.finish(),
SolutionsWriterKind::Json(write) => write.finish()?, SolutionsWriterKind::Json(write) => write.finish(),
SolutionsWriterKind::Csv(write) => write.finish(), SolutionsWriterKind::Csv(write) => write.finish(),
SolutionsWriterKind::Tsv(write) => write.finish(), SolutionsWriterKind::Tsv(write) => write.finish(),
}) }
} }
} }

@ -85,7 +85,9 @@ impl<W: Write> XmlSolutionsWriter<W> {
} }
pub fn finish(self) -> io::Result<W> { pub fn finish(self) -> io::Result<W> {
self.do_finish().map_err(map_xml_error) let mut inner = self.do_finish().map_err(map_xml_error)?;
inner.flush()?;
Ok(inner)
} }
fn do_finish(mut self) -> Result<W, quick_xml::Error> { fn do_finish(mut self) -> Result<W, quick_xml::Error> {

@ -144,12 +144,9 @@ impl<W: Write> TripleWriter<W> {
/// Writes the last bytes of the file /// Writes the last bytes of the file
pub fn finish(self) -> io::Result<()> { pub fn finish(self) -> io::Result<()> {
match self.formatter { match self.formatter {
TripleWriterKind::NTriples(_) => (), TripleWriterKind::NTriples(mut writer) => writer.flush(),
TripleWriterKind::RdfXml(formatter) => { TripleWriterKind::RdfXml(formatter) => formatter.finish()?.flush(), //TODO: remove flush when the next version of Rio is going to be released
formatter.finish()?;
} }
};
Ok(())
} }
} }
@ -259,6 +256,8 @@ impl<W: Write> QuadWriter<W> {
/// Writes the last bytes of the file /// Writes the last bytes of the file
#[allow(clippy::unused_self, clippy::unnecessary_wraps)] #[allow(clippy::unused_self, clippy::unnecessary_wraps)]
pub fn finish(self) -> io::Result<()> { pub fn finish(self) -> io::Result<()> {
Ok(()) match self.formatter {
QuadWriterKind::NQuads(mut writer) | QuadWriterKind::TriG(mut writer) => writer.flush(),
}
} }
} }

Loading…
Cancel
Save