refactor serialization, add json
This commit is contained in:
parent
921b62ed97
commit
aa508a43cb
5 changed files with 155 additions and 69 deletions
|
|
@ -1,4 +1,4 @@
|
|||
use std::{collections::HashMap, fmt::Write, sync::Arc};
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use pdf::{backend::Backend, content::Operation, primitive::Primitive};
|
||||
|
|
@ -15,8 +15,6 @@ lazy_static! {
|
|||
|
||||
const DATE_PARSE_FORMAT: &[time::format_description::FormatItem] =
|
||||
time::macros::format_description!("[month padding:none]/[day padding:none]/[year]");
|
||||
const DATE_DISPLAY_FORMAT: &[time::format_description::FormatItem] =
|
||||
time::macros::format_description!("[year]-[month]-[day]");
|
||||
|
||||
pub struct DataSet {
|
||||
pub columns: Vec<Arc<String>>,
|
||||
|
|
@ -88,34 +86,6 @@ impl DataSet {
|
|||
rows,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn csv_header(&self) -> Result<String, std::fmt::Error> {
|
||||
let mut header = String::from("Date");
|
||||
for column in self.columns.iter() {
|
||||
write!(&mut header, ",{}", column)?;
|
||||
}
|
||||
Ok(header)
|
||||
}
|
||||
|
||||
pub fn csv_row(&self, datapoint: &DataPoint) -> Result<String, std::fmt::Error> {
|
||||
let mut csv_row = datapoint
|
||||
.date
|
||||
.format(DATE_DISPLAY_FORMAT)
|
||||
.expect("Failed to format date!");
|
||||
for column in self.columns.iter() {
|
||||
if let Some(val) = datapoint.values.get(column) {
|
||||
write!(&mut csv_row, ",{}", val)?;
|
||||
} else {
|
||||
write!(&mut csv_row, ",")?;
|
||||
}
|
||||
}
|
||||
Ok(csv_row)
|
||||
}
|
||||
|
||||
pub fn csv_rows(&self) -> impl Iterator<Item = Result<String, std::fmt::Error>> + '_ {
|
||||
std::iter::once_with(|| self.csv_header())
|
||||
.chain(self.rows.iter().map(|datapoint| self.csv_row(datapoint)))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DataPoint {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue