Writer

Struct Writer 

Source
pub struct Writer { /* private fields */ }
Expand description

Use WriterBuilder to build this struct.

Implementations§

Source§

impl Writer

Source

pub fn write<T: Csv>(&mut self, datapacket: &T) -> Result<()>

Writes a DataPacket to a file

Examples found in repository?
examples/to_csv.rs (line 48)
13fn read_write(path_buf: Option<PathBuf>) -> Result<()> {
14    let filename: &str = path_buf
15        .as_ref()
16        .and_then(|name| name.file_stem())
17        .and_then(|name| name.to_str())
18        .unwrap_or("default");
19
20    let (datapackets, errors): (Vec<_>, Vec<_>) = parser::get_packets(
21        &path_buf
22            .as_ref()
23            .unwrap()
24            .clone()
25            .into_os_string()
26            .into_string()
27            .unwrap(),
28    )?
29    .into_iter()
30    .partition(Result::is_ok);
31
32    let mut output_file = path_buf.clone().unwrap();
33    output_file.set_file_name(format!("{}_ERROR.csv", filename));
34
35    // Write Errors
36    let mut output = File::create(output_file.clone())?;
37    for err in errors.into_iter().filter_map(|result| result.err()) {
38        writeln!(output, "{}", err)?;
39    }
40
41    // Write TimeSyncs
42    output_file.set_file_name(format!("{}_timesyncs.csv", filename));
43    let mut writer = writer::WriterBuilder::new().from_path(output_file.to_str().unwrap())?;
44    match parser::find_syncs(&datapackets) {
45        Ok(syncs) => {
46            let header =
47                StringRecord::from(vec!["RD", "TS_received", "TS_sent", "AK", "RoundTrip"]);
48            writer.write(&header)?;
49            for packet in syncs {
50                writer.write(&packet)?;
51            }
52        }
53        Err(e) => {
54            writer.write(&StringRecord::from(vec![format!("{:?}", e)]))?;
55        }
56    }
57
58    // Extract TypeTags
59    let set: HashSet<&str> = HashSet::from_iter(
60        datapackets
61            .iter()
62            .map(|result| result.as_ref().unwrap().data_type.as_str()),
63    );
64
65    // Write TimeSyncsMap
66    output_file.set_file_name(format!("{}_timeSyncMap.csv", filename));
67    let mut writer = writer::WriterBuilder::new().from_path(output_file.to_str().unwrap())?;
68    let syncmap = parser::generate_sync_map(&datapackets);
69    match &syncmap {
70        Ok(map) => {
71            let header = StringRecord::from(vec![
72                "TE0",
73                "TE1",
74                "TL0",
75                "TL1",
76                "TimeSyncsReceived",
77                "EmotiBitStartTime",
78                "EmotiBitEndTime",
79                "DataParserVersion",
80            ]);
81            writer.write(&header)?;
82            writer.write(map)?;
83        }
84        Err(e) => {
85            writer.write(&StringRecord::from(vec![format!("{:?}", e)]))?;
86        }
87    }
88
89    // Write Packets
90    let packets: Vec<DataPacket> = match syncmap {
91        Ok(map) => datapackets
92            .into_iter()
93            .filter_map(|result| result.ok())
94            .map(|p| p.inject_host_timestamp(&map))
95            .collect(),
96        Err(_) => datapackets
97            .into_iter()
98            .filter_map(|result| result.ok())
99            .collect(),
100    };
101
102    for t in set.iter() {
103        output_file.set_file_name(format!("{}_{}.csv", filename, t));
104        let mut writer = writer::WriterBuilder::new().from_path(output_file.to_str().unwrap())?;
105        let header = StringRecord::from(vec![
106            "LocalTimestamp",
107            "EmotiBitTimestamp",
108            "PacketNumber",
109            "DataLength",
110            "TypeTag",
111            "ProtocolVersion",
112            "DataReliability",
113            t,
114        ]);
115        writer.write(&header)?;
116        for packet in packets.iter().filter(|x| x.data_type.as_str() == *t) {
117            writer.write(packet)?;
118        }
119    }
120
121    Ok(())
122}

Auto Trait Implementations§

§

impl Freeze for Writer

§

impl RefUnwindSafe for Writer

§

impl Send for Writer

§

impl Sync for Writer

§

impl Unpin for Writer

§

impl UnwindSafe for Writer

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.