ferric_alpha_render/
data.rs1use ferric_alpha::{ReportColumnData, ReportTable, ReportTimeUnit, TearSheetData};
2
3use crate::{RenderError, RenderResult};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub struct DatetimeColumn<'a> {
7 pub values: &'a [Option<i64>],
8 pub time_unit: ReportTimeUnit,
9 pub timezone: Option<&'a str>,
10}
11
12pub fn require_table<'a>(report: &'a TearSheetData, id: &str) -> RenderResult<&'a ReportTable> {
13 report
14 .table(id)
15 .ok_or_else(|| RenderError::MissingTable { id: id.to_string() })
16}
17
18pub fn bools<'a>(table: &'a ReportTable, name: &str) -> RenderResult<&'a [Option<bool>]> {
19 match column_data(table, name)? {
20 ReportColumnData::Boolean(values) => Ok(values),
21 _ => wrong_type(table, name, "boolean"),
22 }
23}
24
25pub fn int32<'a>(table: &'a ReportTable, name: &str) -> RenderResult<&'a [Option<i32>]> {
26 match column_data(table, name)? {
27 ReportColumnData::Int32(values) => Ok(values),
28 _ => wrong_type(table, name, "int32"),
29 }
30}
31
32pub fn int64<'a>(table: &'a ReportTable, name: &str) -> RenderResult<&'a [Option<i64>]> {
33 match column_data(table, name)? {
34 ReportColumnData::Int64(values) => Ok(values),
35 _ => wrong_type(table, name, "int64"),
36 }
37}
38
39pub fn uint32<'a>(table: &'a ReportTable, name: &str) -> RenderResult<&'a [Option<u32>]> {
40 match column_data(table, name)? {
41 ReportColumnData::UInt32(values) => Ok(values),
42 _ => wrong_type(table, name, "uint32"),
43 }
44}
45
46pub fn uint64<'a>(table: &'a ReportTable, name: &str) -> RenderResult<&'a [Option<u64>]> {
47 match column_data(table, name)? {
48 ReportColumnData::UInt64(values) => Ok(values),
49 _ => wrong_type(table, name, "uint64"),
50 }
51}
52
53pub fn float64<'a>(table: &'a ReportTable, name: &str) -> RenderResult<&'a [Option<f64>]> {
54 match column_data(table, name)? {
55 ReportColumnData::Float64(values) => Ok(values),
56 _ => wrong_type(table, name, "float64"),
57 }
58}
59
60pub fn strings<'a>(table: &'a ReportTable, name: &str) -> RenderResult<&'a [Option<String>]> {
61 match column_data(table, name)? {
62 ReportColumnData::String(values) => Ok(values),
63 _ => wrong_type(table, name, "string"),
64 }
65}
66
67pub fn datetimes<'a>(table: &'a ReportTable, name: &str) -> RenderResult<DatetimeColumn<'a>> {
68 match column_data(table, name)? {
69 ReportColumnData::Datetime {
70 values,
71 time_unit,
72 timezone,
73 } => Ok(DatetimeColumn {
74 values,
75 time_unit: *time_unit,
76 timezone: timezone.as_deref(),
77 }),
78 _ => wrong_type(table, name, "datetime"),
79 }
80}
81
82fn column_data<'a>(table: &'a ReportTable, name: &str) -> RenderResult<&'a ReportColumnData> {
83 table
84 .columns()
85 .iter()
86 .find(|column| column.name() == name)
87 .map(|column| column.data())
88 .ok_or_else(|| RenderError::MissingColumn {
89 table: table.id().to_string(),
90 column: name.to_string(),
91 })
92}
93
94fn wrong_type<T>(table: &ReportTable, name: &str, expected: &'static str) -> RenderResult<T> {
95 Err(RenderError::InvalidColumnType {
96 table: table.id().to_string(),
97 column: name.to_string(),
98 expected,
99 })
100}