Skip to content

Commit

Permalink
Adds implementation of finish for IonWriter (#720)
Browse files Browse the repository at this point in the history
  • Loading branch information
desaikd authored Feb 23, 2024
1 parent d326d5a commit e593200
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/binary/binary_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ impl<W: Write> IonWriter for BinaryWriter<W> {
fn parent_type(&self) -> Option<IonType>;
fn depth(&self) -> usize;
fn step_out(&mut self) -> IonResult<()>;
fn finish(self) -> IonResult<Self::Output>;
fn output(&self) -> &Self::Output;
fn output_mut(&mut self) -> &mut Self::Output;
}
Expand Down Expand Up @@ -262,4 +263,21 @@ mod tests {

Ok(())
}

#[test]
fn finish_test() -> IonResult<()> {
let buffer = Vec::new();
let mut binary_writer = BinaryWriterBuilder::new().build(buffer)?;
binary_writer.write_string("foo")?;
binary_writer.write_i64(5)?;
let output = binary_writer.finish()?;

let mut reader = ReaderBuilder::new().build(output)?;
assert_eq!(Value(IonType::String), reader.next()?);
assert_eq!("foo", reader.read_string()?);

assert_eq!(Value(IonType::Int), reader.next()?);
assert_eq!(5, reader.read_i64()?);
Ok(())
}
}
5 changes: 5 additions & 0 deletions src/binary/raw_binary_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,11 @@ impl<W: Write> IonWriter for RawBinaryWriter<W> {
Ok(())
}

fn finish(mut self) -> IonResult<Self::Output> {
self.flush()?;
Ok(self.out)
}

fn output(&self) -> &Self::Output {
&self.out
}
Expand Down
4 changes: 4 additions & 0 deletions src/element/element_stream_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ where
Ok(())
}

fn finish(self) -> IonResult<Self::Output> {
Ok(self.output)
}

fn output(&self) -> &Self::Output {
&self.output
}
Expand Down
4 changes: 4 additions & 0 deletions src/ion_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ pub trait IonWriter {
/// This method can only be called when the writer is at the top level.
fn flush(&mut self) -> IonResult<()>;

/// Flushes any buffered data to be written to the underlying io::Write implementation,
/// and returns the writer's output.
fn finish(self) -> IonResult<Self::Output>;

// This portion of the doc comment is always visible
#[doc = r##"
Returns a reference to the writer's output.
Expand Down
5 changes: 5 additions & 0 deletions src/text/raw_text_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,11 @@ impl<W: Write> IonWriter for RawTextWriter<W> {
Ok(())
}

fn finish(mut self) -> IonResult<W> {
self.output.flush()?;
Ok(self.output.into_inner().map_err(|e| e.into_error())?)
}

fn output(&self) -> &W {
self.output.get_ref()
}
Expand Down
18 changes: 18 additions & 0 deletions src/text/text_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ impl<W: Write> IonWriter for TextWriter<W> {
fn depth(&self) -> usize;
fn step_out(&mut self) -> IonResult<()>;
fn flush(&mut self) -> IonResult<()>;
fn finish(self) -> IonResult<Self::Output>;
fn output(&self) -> &Self::Output;
fn output_mut(&mut self) -> &mut Self::Output;
}
Expand Down Expand Up @@ -221,4 +222,21 @@ mod tests {

Ok(())
}

#[test]
fn finish_test() -> IonResult<()> {
let buffer = Vec::new();
let mut text_writer = TextWriterBuilder::default().build(buffer)?;
text_writer.write_string("foo")?;
text_writer.write_i64(5)?;
let output = text_writer.finish()?;

let mut reader = ReaderBuilder::new().build(output)?;
assert_eq!(Value(IonType::String), reader.next()?);
assert_eq!("foo", reader.read_string()?);

assert_eq!(Value(IonType::Int), reader.next()?);
assert_eq!(5, reader.read_i64()?);
Ok(())
}
}

0 comments on commit e593200

Please sign in to comment.