etop_format/table_formats/
cell_format.rs

1use crate::{
2    BinaryFormat, BoolFormat, FormatError, FormatType, NumberFormat, StringFormat, UnknownFormat,
3};
4use polars::prelude::DataType;
5
6/// cell format shorthand
7#[derive(Debug, Clone)]
8pub enum CellFormatShorthand {
9    /// number format
10    Number(NumberFormat),
11    /// binary format
12    Binary(BinaryFormat),
13    /// string format
14    String(StringFormat),
15    /// bool format
16    Bool(BoolFormat),
17    /// unknown format
18    Unknown(UnknownFormat),
19}
20
21impl From<NumberFormat> for CellFormatShorthand {
22    fn from(format: NumberFormat) -> CellFormatShorthand {
23        CellFormatShorthand::Number(format)
24    }
25}
26
27impl From<StringFormat> for CellFormatShorthand {
28    fn from(format: StringFormat) -> CellFormatShorthand {
29        CellFormatShorthand::String(format)
30    }
31}
32
33impl From<BinaryFormat> for CellFormatShorthand {
34    fn from(format: BinaryFormat) -> CellFormatShorthand {
35        CellFormatShorthand::Binary(format)
36    }
37}
38
39impl From<BoolFormat> for CellFormatShorthand {
40    fn from(format: BoolFormat) -> CellFormatShorthand {
41        CellFormatShorthand::Bool(format)
42    }
43}
44
45impl CellFormatShorthand {
46    /// set min width
47    pub fn min_width(self, min_width: usize) -> CellFormatShorthand {
48        match self {
49            CellFormatShorthand::Number(fmt) => {
50                CellFormatShorthand::Number(fmt.min_width(min_width))
51            }
52            CellFormatShorthand::String(fmt) => {
53                CellFormatShorthand::String(fmt.min_width(min_width))
54            }
55            CellFormatShorthand::Binary(fmt) => {
56                CellFormatShorthand::Binary(fmt.min_width(min_width))
57            }
58            CellFormatShorthand::Bool(fmt) => CellFormatShorthand::Bool(fmt.min_width(min_width)),
59            CellFormatShorthand::Unknown(fmt) => {
60                CellFormatShorthand::Unknown(fmt.min_width(min_width))
61            }
62        }
63    }
64
65    /// set max width
66    pub fn max_width(self, max_width: usize) -> CellFormatShorthand {
67        match self {
68            CellFormatShorthand::Number(fmt) => {
69                CellFormatShorthand::Number(fmt.max_width(max_width))
70            }
71            CellFormatShorthand::String(fmt) => {
72                CellFormatShorthand::String(fmt.max_width(max_width))
73            }
74            CellFormatShorthand::Binary(fmt) => {
75                CellFormatShorthand::Binary(fmt.max_width(max_width))
76            }
77            CellFormatShorthand::Bool(fmt) => CellFormatShorthand::Bool(fmt.max_width(max_width)),
78            CellFormatShorthand::Unknown(fmt) => {
79                CellFormatShorthand::Unknown(fmt.max_width(max_width))
80            }
81        }
82    }
83
84    /// convert shorthand into formal version
85    pub fn finalize(self, dtype: &DataType) -> Result<CellFormat, FormatError> {
86        let fmt = match self {
87            CellFormatShorthand::Number(fmt) => CellFormat::Number(fmt),
88            CellFormatShorthand::Binary(fmt) => CellFormat::Binary(fmt),
89            CellFormatShorthand::String(fmt) => CellFormat::String(fmt),
90            CellFormatShorthand::Bool(fmt) => CellFormat::Bool(fmt),
91            CellFormatShorthand::Unknown(fmt) => match dtype {
92                DataType::Utf8 => CellFormat::String(fmt.into()),
93                DataType::Boolean => CellFormat::Bool(fmt.into()),
94                DataType::Binary => CellFormat::Binary(fmt.into()),
95                dtype if dtype.is_integer() => {
96                    let fmt: NumberFormat = fmt.into();
97                    let fmt = fmt.format_type(&FormatType::Decimal).precision(0);
98                    CellFormat::Number(fmt)
99                }
100                dtype if dtype.is_float() => {
101                    let fmt: NumberFormat = fmt.into();
102                    let fmt = fmt.format_type(&FormatType::Exponent);
103                    CellFormat::Number(fmt)
104                }
105                _ => {
106                    return Err(FormatError::UnsupportedDatatype(format!(
107                        "Unsupported datatype: {:?}",
108                        dtype
109                    )))
110                }
111            },
112        };
113        Ok(fmt)
114    }
115}
116
117/// cell format
118#[derive(Debug, Clone)]
119pub enum CellFormat {
120    /// number format
121    Number(NumberFormat),
122    /// binary format
123    Binary(BinaryFormat),
124    /// string format
125    String(StringFormat),
126    /// bool format
127    Bool(BoolFormat),
128}
129
130impl CellFormat {
131    /// set min width
132    pub fn min_width(self, min_width: usize) -> CellFormat {
133        match self {
134            CellFormat::Number(fmt) => CellFormat::Number(fmt.min_width(min_width)),
135            CellFormat::String(fmt) => CellFormat::String(fmt.min_width(min_width)),
136            CellFormat::Binary(fmt) => CellFormat::Binary(fmt.min_width(min_width)),
137            CellFormat::Bool(fmt) => CellFormat::Bool(fmt.min_width(min_width)),
138        }
139    }
140
141    /// set max width
142    pub fn max_width(self, max_width: usize) -> CellFormat {
143        match self {
144            CellFormat::Number(fmt) => CellFormat::Number(fmt.max_width(max_width)),
145            CellFormat::String(fmt) => CellFormat::String(fmt.max_width(max_width)),
146            CellFormat::Binary(fmt) => CellFormat::Binary(fmt.max_width(max_width)),
147            CellFormat::Bool(fmt) => CellFormat::Bool(fmt.max_width(max_width)),
148        }
149    }
150
151    /// get min width
152    pub fn get_min_width(&self) -> Option<usize> {
153        match self {
154            CellFormat::Number(fmt) => Some(fmt.min_width),
155            CellFormat::String(fmt) => Some(fmt.min_width),
156            CellFormat::Binary(fmt) => Some(fmt.min_width),
157            CellFormat::Bool(fmt) => Some(fmt.min_width),
158        }
159    }
160
161    /// get max width
162    pub fn get_max_width(&self) -> Option<usize> {
163        match self {
164            CellFormat::Number(fmt) => Some(fmt.max_width),
165            CellFormat::String(fmt) => Some(fmt.max_width),
166            CellFormat::Binary(fmt) => Some(fmt.max_width),
167            CellFormat::Bool(fmt) => Some(fmt.max_width),
168        }
169    }
170}
171
172impl TryInto<NumberFormat> for CellFormat {
173    type Error = FormatError;
174
175    fn try_into(self) -> Result<NumberFormat, FormatError> {
176        match self {
177            CellFormat::Number(format) => Ok(format),
178            _ => Err(FormatError::MismatchedFormatType("not a NumberFormat".to_string())),
179        }
180    }
181}
182
183impl TryInto<StringFormat> for CellFormat {
184    type Error = FormatError;
185
186    fn try_into(self) -> Result<StringFormat, FormatError> {
187        match self {
188            CellFormat::String(format) => Ok(format),
189            _ => Err(FormatError::MismatchedFormatType("not a StringFormat".to_string())),
190        }
191    }
192}
193
194impl TryInto<BinaryFormat> for CellFormat {
195    type Error = FormatError;
196
197    fn try_into(self) -> Result<BinaryFormat, FormatError> {
198        match self {
199            CellFormat::Binary(format) => Ok(format),
200            _ => Err(FormatError::MismatchedFormatType("not a BinaryFormat".to_string())),
201        }
202    }
203}
204
205impl TryInto<BoolFormat> for CellFormat {
206    type Error = FormatError;
207
208    fn try_into(self) -> Result<BoolFormat, FormatError> {
209        match self {
210            CellFormat::Bool(format) => Ok(format),
211            _ => Err(FormatError::MismatchedFormatType("not a BoolFormat".to_string())),
212        }
213    }
214}