sync::Arc,
};
-use chrono::NaiveDateTime;
+use chrono::{NaiveDateTime, Utc};
use enum_iterator::Sequence;
use enum_map::{Enum, EnumMap, enum_map};
use itertools::Itertools;
use smallvec::SmallVec;
use crate::{
- format::{F40, F40_2, F40_3, Format, PCT40_1, Settings as FormatSettings},
+ format::{Decimal, F40, F40_2, F40_3, Format, PCT40_1, Settings as FormatSettings},
output::pivot::{
look::{Look, Sizing},
value::{BareValue, Value, ValueOptions},
self
}
+ /// Returns this pivot table with the specified decimal point.
+ pub fn with_decimal(mut self, decimal: Decimal) -> Self {
+ if self.style.settings.decimal != decimal {
+ Arc::make_mut(&mut self.style.settings).decimal = decimal;
+ }
+ self
+ }
+
+ /// Returns this pivot table with the date set as specified.
+ pub fn with_date(mut self, date: Option<NaiveDateTime>) -> Self {
+ self.metadata.date = date;
+ self
+ }
+
/// Inserts `number` into the cell with the given `data_indexes`, drawing
/// its format from `class`.
pub fn insert_number(&mut self, data_indexes: &[usize], number: Option<f64>, class: Class) {
}
/// Metadata for a [PivotTable].
-#[derive(Clone, Debug, Default, Serialize)]
+#[derive(Clone, Debug, Serialize)]
pub struct PivotTableMetadata {
/// Title.
///
pub date: Option<NaiveDateTime>,
}
+impl Default for PivotTableMetadata {
+ fn default() -> Self {
+ Self {
+ title: None,
+ caption: None,
+ corner_text: None,
+ notes: None,
+ notes_unexpanded: None,
+ command_local: None,
+ command_c: None,
+ subtype: None,
+ language: None,
+ locale: None,
+ dataset: None,
+ datafile: None,
+ date: Some(Utc::now().naive_local()),
+ }
+ }
+}
+
impl PivotTableMetadata {
/// Return this metadata with the given `subtype`.
pub fn with_subtype(self, subtype: impl Into<Value>) -> Self {
sync::Arc,
};
-use chrono::{NaiveDateTime, NaiveTime};
+use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use displaydoc::Display;
use enum_map::{Enum, EnumMap};
use hashbrown::HashSet;
Datum::String(relabel.to.clone())
};
self.0.insert(OrderedFloat(relabel.from), value);
- // XXX warn on duplicate
}
(format.unwrap_or(F8_0), affixes)
}
Datum::String(vme.to.clone())
};
self.0.insert(OrderedFloat(from), to);
- } else {
- // XXX warn
}
}
}
/// Unsupported applyToConverse.
UnsupportedApplyToConverse,
+
+ /// Invalid creation date {0:?}.
+ InvalidCreationDate(String),
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Visualization {
/// In format `YYYY-MM-DD`.
- // XXX parse this
#[serde(rename = "@date")]
- _date: String,
+ date: String,
// Locale used for output, e.g. `en-US`.
#[serde(rename = "@lang")]
}
impl Visualization {
+ fn decode_date(&self) -> Result<NaiveDate, LegacyXmlWarning> {
+ NaiveDate::parse_from_str(&self.date, "%Y-%m-%d")
+ .map_err(|_| LegacyXmlWarning::InvalidCreationDate(self.date.clone()))
+ }
pub fn decode_series(
&self,
data: IndexMap<String, IndexMap<String, Vec<DataValue>>>,
| VisChild::Other => (),
}
}
- let Some(graph) = graph else { todo!() }; //XXX
+ let Some(graph) = graph else {
+ return Err(super::Error::LegacyMissingGraph);
+ };
let footnotes = self.decode_footnotes(graph, &labels[Purpose::Footnote]);
warn,
);
- let dimensions = dims
- .into_iter()
- .map(|dim| (dim.axis, dim.dimension))
- .collect::<Vec<_>>();
- let mut pivot_table = PivotTable::new(dimensions)
- .with_look(Arc::new(look))
- .with_footnotes(footnotes)
- .with_data(data)
- .with_layer(¤t_layer);
- let decimal = Decimal::for_lang(&self.lang);
- if pivot_table.style.settings.decimal != decimal {
- Arc::make_mut(&mut pivot_table.style.settings).decimal = decimal;
- }
+ let date = match self.decode_date() {
+ Ok(date) => Some(date.into()),
+ Err(w) => {
+ warn(w);
+ None
+ }
+ };
+ let mut pivot_table = PivotTable::new(
+ dims.into_iter()
+ .map(|dim| (dim.axis, dim.dimension))
+ .collect::<Vec<_>>(),
+ )
+ .with_look(Arc::new(look))
+ .with_footnotes(footnotes)
+ .with_data(data)
+ .with_layer(¤t_layer)
+ .with_decimal(Decimal::for_lang(&self.lang))
+ .with_date(date);
+
if let Some(title) = title {
pivot_table = pivot_table.with_title(title);
}
axes: &HashMap<usize, &Axis>,
styles: &HashMap<&str, &Style>,
a: Axis3,
-
footnotes: &pivot::Footnotes,
dims: &mut Vec<Dim<'a>>,
) {