Skip to content

Commit 8ebdc16

Browse files
improved autofilter API
1 parent 4f81c1c commit 8ebdc16

File tree

2 files changed

+73
-22
lines changed

2 files changed

+73
-22
lines changed

libxlsxwriter/src/lib.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,58 @@ where
137137
Ok(r)
138138
}
139139

140+
#[derive(Debug, Clone, PartialEq, PartialOrd)]
141+
pub enum StringOrFloat {
142+
String(String),
143+
Float(f64),
144+
}
145+
146+
impl Default for StringOrFloat {
147+
fn default() -> Self {
148+
StringOrFloat::Float(0.)
149+
}
150+
}
151+
152+
impl StringOrFloat {
153+
pub fn to_string(self) -> Option<String> {
154+
match self {
155+
StringOrFloat::String(x) => Some(x),
156+
StringOrFloat::Float(_) => None,
157+
}
158+
}
159+
160+
pub fn to_str(&self) -> Option<&str> {
161+
match self {
162+
StringOrFloat::String(x) => Some(x.as_str()),
163+
StringOrFloat::Float(_) => None,
164+
}
165+
}
166+
167+
pub fn to_f64(&self) -> Option<f64> {
168+
match self {
169+
StringOrFloat::String(_) => None,
170+
StringOrFloat::Float(x) => Some(*x),
171+
}
172+
}
173+
}
174+
175+
impl From<&str> for StringOrFloat {
176+
fn from(val: &str) -> Self {
177+
StringOrFloat::String(val.to_string())
178+
}
179+
}
180+
181+
impl From<String> for StringOrFloat {
182+
fn from(val: String) -> Self {
183+
StringOrFloat::String(val)
184+
}
185+
}
186+
187+
impl From<f64> for StringOrFloat {
188+
fn from(val: f64) -> Self {
189+
StringOrFloat::Float(val)
190+
}
191+
}
192+
140193
#[cfg(test)]
141194
mod test;

libxlsxwriter/src/worksheet/filter.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::{try_to_vec, CStringHelper, Worksheet, WorksheetCol, WorksheetRow, XlsxError};
1+
use crate::{
2+
try_to_vec, CStringHelper, StringOrFloat, Worksheet, WorksheetCol, WorksheetRow, XlsxError,
3+
};
24

35
/// And/or operator conditions when using 2 filter rules with `filter_column2`.
46
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -80,23 +82,31 @@ impl FilterCriteria {
8082
}
8183
}
8284

85+
/// Options for autofilter rules.
8386
#[derive(Debug, Clone, PartialEq, PartialOrd, Default)]
8487
pub struct FilterRule {
88+
/// The FilterCriteria to define the rule.
8589
pub criteria: FilterCriteria,
86-
pub value_string: Option<String>,
87-
pub value: Option<f64>,
90+
/// value to which the criteria applies.
91+
pub value: StringOrFloat,
8892
}
8993

9094
impl FilterRule {
95+
pub fn new<T: Into<StringOrFloat>>(criteria: FilterCriteria, value: T) -> Self {
96+
FilterRule {
97+
criteria,
98+
value: value.into(),
99+
}
100+
}
101+
91102
pub(crate) fn into_internal(
92103
&self,
93104
c_string_helper: &mut CStringHelper,
94105
) -> Result<libxlsxwriter_sys::lxw_filter_rule, XlsxError> {
95106
Ok(libxlsxwriter_sys::lxw_filter_rule {
96107
criteria: self.criteria.into_internal() as u8,
97-
value_string: c_string_helper.add_opt(self.value_string.as_deref())?
98-
as *mut std::ffi::c_char,
99-
value: self.value.unwrap_or_default(),
108+
value_string: c_string_helper.add_opt(self.value.to_str())? as *mut std::ffi::c_char,
109+
value: self.value.to_f64().unwrap_or_default(),
100110
})
101111
}
102112
}
@@ -131,7 +141,7 @@ impl<'a> Worksheet<'a> {
131141

132142
/// This function can be used to filter columns in a autofilter range based on single rule conditions.
133143
///
134-
/// The `col` parameter is a zero indexed column number and must refer to a column in an existing autofilter created with `autofilter`.
144+
/// The `col` parameter is a zero indexed column number and must refer to a column in an existing autofilter created with [`Worksheet::autofilter`].
135145
/// It isn't sufficient to just specify the filter condition. You must also hide any rows that don't match the filter condition.
136146
pub fn filter_column(
137147
&mut self,
@@ -235,11 +245,7 @@ mod test {
235245
// ------------
236246
let mut worksheet1 = workbook.add_worksheet(Some("Sheet 1"))?;
237247
create_sheet(&mut worksheet1)?;
238-
let worksheet1_criteria = FilterRule {
239-
criteria: FilterCriteria::GreaterThan,
240-
value: Some(10.),
241-
value_string: None,
242-
};
248+
let worksheet1_criteria = FilterRule::new(FilterCriteria::GreaterThan, 10.0);
243249
worksheet1.filter_column(0, &worksheet1_criteria)?;
244250
let mut hidden_row = RowColOptions::new(true, 0, false);
245251
for i in 1..=10 {
@@ -248,16 +254,8 @@ mod test {
248254
// ------------
249255
let mut worksheet2 = workbook.add_worksheet(Some("Sheet 2"))?;
250256
create_sheet(&mut worksheet2)?;
251-
let worksheet2_criteria1 = FilterRule {
252-
criteria: FilterCriteria::GreaterThanOrEqualTo,
253-
value: Some(3.),
254-
value_string: None,
255-
};
256-
let worksheet2_criteria2 = FilterRule {
257-
criteria: FilterCriteria::LessThan,
258-
value: Some(5.5),
259-
value_string: None,
260-
};
257+
let worksheet2_criteria1 = FilterRule::new(FilterCriteria::GreaterThanOrEqualTo, 3.0);
258+
let worksheet2_criteria2 = FilterRule::new(FilterCriteria::LessThan, 5.5);
261259
worksheet2.filter_column2(
262260
1,
263261
&worksheet2_criteria1,

0 commit comments

Comments
 (0)