bookdata/io/object/
mod.rs
1use std::io::prelude::*;
2use std::marker::PhantomData;
3use std::mem::drop;
4
5use anyhow::Result;
6use csv;
7use serde::Serialize;
8
9mod chunks;
10mod thread;
11mod transform;
12
13pub use chunks::{ChunkWriter, UnchunkWriter};
14pub use thread::ThreadObjectWriter;
15pub use transform::MapWriter;
16
17pub trait ObjectWriter<T>: Sized {
19 fn write_object(&mut self, object: T) -> Result<()>;
21
22 fn write_all_objects<I>(&mut self, objects: I) -> Result<usize>
24 where
25 I: Iterator<Item = T>,
26 {
27 let mut count = 0;
28 for obj in objects {
29 self.write_object(obj)?;
30 count += 1;
31 }
32 Ok(count)
33 }
34
35 fn write_and_finish<I>(mut self, objects: I) -> Result<usize>
37 where
38 I: Iterator<Item = T>,
39 {
40 let n = self.write_all_objects(objects)?;
41 self.finish()?;
42 Ok(n)
43 }
44
45 fn finish(self) -> Result<usize>;
47
48 fn with_transform<F, T2>(self, transform: F) -> MapWriter<F, T2, T, Self>
50 where
51 F: Fn(T2) -> Result<T>,
52 {
53 MapWriter {
54 _phantom: PhantomData,
55 transform,
56 writer: self,
57 }
58 }
59}
60
61impl<T, W> ObjectWriter<T> for &mut W
65where
66 W: ObjectWriter<T>,
67{
68 fn write_object(&mut self, object: T) -> Result<()> {
69 (**self).write_object(object)
70 }
71
72 fn finish(self) -> Result<usize> {
73 Ok(0)
75 }
76}
77
78impl<T: Serialize, W: Write> ObjectWriter<T> for csv::Writer<W> {
79 fn write_object(&mut self, object: T) -> Result<()> {
80 self.serialize(object)?;
81 Ok(())
82 }
83
84 fn finish(mut self) -> Result<usize> {
85 self.flush()?;
86 drop(self);
87 Ok(0)
88 }
89}