xls-rs 0.1.2

A powerful CLI tool and library for spreadsheet manipulation with pandas-style operations. Supports CSV, Excel (XLSX, XLS, ODS), Parquet, and Avro formats with formula evaluation, data transformation, and comprehensive analytics capabilities.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Type-safe data structures for xls-rs
//!
//! This module provides strongly-typed representations of cell data
//! to improve type safety and performance over string-only representations.

use std::fmt;

/// A strongly-typed cell value that can represent different data types
///
/// This enum provides type safety for cell values, allowing the codebase
/// to distinguish between strings, numbers, booleans, dates, and empty values.
/// This eliminates the need for repeated string parsing and improves performance.
#[derive(Debug, Clone, PartialEq)]
pub enum CellValue {
    /// String data
    String(String),
    /// Numeric data (float)
    Number(f64),
    /// Integer data (for exact precision when needed)
    Integer(i64),
    /// Boolean data
    Boolean(bool),
    /// Date/time data (stored as timestamp)
    DateTime(i64),
    /// Empty/null value
    Empty,
}

impl CellValue {
    /// Create a String cell value
    pub fn string(s: impl Into<String>) -> Self {
        CellValue::String(s.into())
    }

    /// Create a Number cell value
    pub fn number(n: f64) -> Self {
        CellValue::Number(n)
    }

    /// Create an Integer cell value
    pub fn integer(i: i64) -> Self {
        CellValue::Integer(i)
    }

    /// Create a Boolean cell value
    pub fn boolean(b: bool) -> Self {
        CellValue::Boolean(b)
    }

    /// Create a DateTime cell value from timestamp
    pub fn datetime(timestamp: i64) -> Self {
        CellValue::DateTime(timestamp)
    }

    /// Create an Empty cell value
    pub fn empty() -> Self {
        CellValue::Empty
    }

    /// Check if the value is empty
    pub fn is_empty(&self) -> bool {
        matches!(self, CellValue::Empty)
    }

    /// Check if the value is numeric (Number or Integer)
    pub fn is_numeric(&self) -> bool {
        matches!(self, CellValue::Number(_) | CellValue::Integer(_))
    }

    /// Get the value as a string reference
    pub fn as_str(&self) -> Option<&str> {
        match self {
            CellValue::String(s) => Some(s),
            _ => None,
        }
    }

    /// Get the value as a number (f64)
    ///
    /// Returns Some(f64) for Number and Integer values, None otherwise
    pub fn as_number(&self) -> Option<f64> {
        match self {
            CellValue::Number(n) => Some(*n),
            CellValue::Integer(i) => Some(*i as f64),
            _ => None,
        }
    }

    /// Get the value as a boolean
    pub fn as_bool(&self) -> Option<bool> {
        match self {
            CellValue::Boolean(b) => Some(*b),
            _ => None,
        }
    }

    /// Convert to display string
    pub fn to_display_string(&self) -> String {
        match self {
            CellValue::String(s) => s.clone(),
            CellValue::Number(n) => {
                // Format without unnecessary decimal places
                if n.fract() == 0.0 && n.abs() < (i64::MAX as f64) {
                    format!("{}", *n as i64)
                } else {
                    format!("{}", n)
                }
            }
            CellValue::Integer(i) => format!("{}", i),
            CellValue::Boolean(b) => format!("{}", b),
            CellValue::DateTime(ts) => format!("{}", ts),
            CellValue::Empty => String::new(),
        }
    }

    /// Parse a string into the most appropriate CellValue type
    ///
    /// Attempts to parse the string as:
    /// 1. Empty -> Empty
    /// 2. Boolean ("true"/"false") -> Boolean
    /// 3. Integer -> Integer
    /// 4. Float -> Number
    /// 5. Otherwise -> String
    pub fn parse(s: &str) -> Self {
        let trimmed = s.trim();

        if trimmed.is_empty() {
            return CellValue::Empty;
        }

        // Try boolean
        match trimmed.to_lowercase().as_str() {
            "true" | "yes" | "1" => return CellValue::Boolean(true),
            "false" | "no" | "0" => return CellValue::Boolean(false),
            _ => {}
        }

        // Try integer first (more precise)
        if let Ok(i) = trimmed.parse::<i64>() {
            return CellValue::Integer(i);
        }

        // Try float
        if let Ok(n) = trimmed.parse::<f64>() {
            return CellValue::Number(n);
        }

        // Default to string
        CellValue::String(trimmed.to_string())
    }

    /// Convert from string representation with type hint
    pub fn from_string_with_type(s: &str, type_hint: Option<&DataType>) -> Self {
        match type_hint {
            Some(DataType::Integer) => s.parse::<i64>()
                .map(CellValue::Integer)
                .unwrap_or_else(|_| CellValue::String(s.to_string())),
            Some(DataType::Number) => s.parse::<f64>()
                .map(CellValue::Number)
                .unwrap_or_else(|_| CellValue::String(s.to_string())),
            Some(DataType::Boolean) => match s.to_lowercase().as_str() {
                "true" | "yes" | "1" => CellValue::Boolean(true),
                "false" | "no" | "0" => CellValue::Boolean(false),
                _ => CellValue::String(s.to_string()),
            },
            Some(DataType::String) | None => CellValue::parse(s),
            Some(DataType::DateTime) => s.parse::<i64>()
                .map(CellValue::DateTime)
                .unwrap_or_else(|_| CellValue::String(s.to_string())),
        }
    }
}

impl Default for CellValue {
    fn default() -> Self {
        CellValue::Empty
    }
}

impl fmt::Display for CellValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CellValue::String(s) => write!(f, "{}", s),
            CellValue::Number(n) => write!(f, "{}", n),
            CellValue::Integer(i) => write!(f, "{}", i),
            CellValue::Boolean(b) => write!(f, "{}", b),
            CellValue::DateTime(ts) => write!(f, "{}", ts),
            CellValue::Empty => Ok(()),
        }
    }
}

impl From<String> for CellValue {
    fn from(s: String) -> Self {
        CellValue::parse(&s)
    }
}

impl From<&str> for CellValue {
    fn from(s: &str) -> Self {
        CellValue::parse(s)
    }
}

impl From<f64> for CellValue {
    fn from(n: f64) -> Self {
        CellValue::Number(n)
    }
}

impl From<i64> for CellValue {
    fn from(i: i64) -> Self {
        CellValue::Integer(i)
    }
}

impl From<bool> for CellValue {
    fn from(b: bool) -> Self {
        CellValue::Boolean(b)
    }
}

/// Data type metadata for columns
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DataType {
    String,
    Number,
    Integer,
    Boolean,
    DateTime,
}

impl DataType {
    /// Detect the data type from a cell value
    pub fn from_value(value: &CellValue) -> Self {
        match value {
            CellValue::String(_) => DataType::String,
            CellValue::Number(_) => DataType::Number,
            CellValue::Integer(_) => DataType::Integer,
            CellValue::Boolean(_) => DataType::Boolean,
            CellValue::DateTime(_) => DataType::DateTime,
            CellValue::Empty => DataType::String,
        }
    }

    /// Get the string name of the data type
    pub fn name(&self) -> &'static str {
        match self {
            DataType::String => "string",
            DataType::Number => "float",
            DataType::Integer => "integer",
            DataType::Boolean => "boolean",
            DataType::DateTime => "datetime",
        }
    }
}

impl fmt::Display for DataType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.name())
    }
}

/// A row of type-safe cell values
pub type DataRow = Vec<CellValue>;

/// A dataset of type-safe data
#[derive(Debug, Clone, PartialEq)]
pub struct DataSet {
    /// Column names (header row)
    pub columns: Vec<String>,
    /// Data rows
    pub rows: Vec<DataRow>,
    /// Column type metadata
    pub column_types: Vec<DataType>,
}

impl DataSet {
    /// Create a new empty dataset
    pub fn new() -> Self {
        Self {
            columns: Vec::new(),
            rows: Vec::new(),
            column_types: Vec::new(),
        }
    }

    /// Create a dataset with columns but no data
    pub fn with_columns(columns: Vec<String>) -> Self {
        let column_types = vec![DataType::String; columns.len()];
        Self {
            columns,
            rows: Vec::new(),
            column_types,
        }
    }

    /// Add a row to the dataset
    pub fn push_row(&mut self, row: DataRow) {
        // Update column types based on new data
        for (i, cell) in row.iter().enumerate() {
            if i < self.column_types.len() {
                let detected = DataType::from_value(cell);
                // Prefer more specific types
                if std::mem::discriminant(&self.column_types[i])
                    != std::mem::discriminant(&detected)
                {
                    self.column_types[i] = detected;
                }
            }
        }
        self.rows.push(row);
    }

    /// Get the number of rows
    pub fn row_count(&self) -> usize {
        self.rows.len()
    }

    /// Get the number of columns
    pub fn column_count(&self) -> usize {
        self.columns.len()
    }

    /// Check if the dataset is empty
    pub fn is_empty(&self) -> bool {
        self.rows.is_empty()
    }

    /// Infer column types from existing data
    pub fn infer_types(&mut self) {
        for col_idx in 0..self.columns.len() {
            let mut type_count: std::collections::HashMap<DataType, usize> =
                std::collections::HashMap::new();

            for row in &self.rows {
                if let Some(cell) = row.get(col_idx) {
                    let dt = DataType::from_value(cell);
                    *type_count.entry(dt).or_insert(0) += 1;
                }
            }

            // Choose the most common non-empty type
            let most_common = type_count
                .iter()
                .filter(|(dt, _)| *dt != &DataType::String)
                .max_by_key(|(_, count)| *count)
                .map(|(dt, _)| *dt)
                .unwrap_or(DataType::String);

            if col_idx < self.column_types.len() {
                self.column_types[col_idx] = most_common;
            }
        }
    }
}

impl Default for DataSet {
    fn default() -> Self {
        Self::new()
    }
}

/// Conversion from legacy `Vec<Vec<String>>` format
impl From<Vec<Vec<String>>> for DataSet {
    fn from(data: Vec<Vec<String>>) -> Self {
        if data.is_empty() {
            return DataSet::new();
        }

        let columns = data[0].clone();
        let mut dataset = DataSet::with_columns(columns);

        for row in &data[1..] {
            let typed_row: DataRow = row.iter().map(|s| CellValue::parse(s)).collect();
            dataset.push_row(typed_row);
        }

        dataset.infer_types();
        dataset
    }
}

/// Conversion to legacy `Vec<Vec<String>>` format
impl From<DataSet> for Vec<Vec<String>> {
    fn from(dataset: DataSet) -> Vec<Vec<String>> {
        let mut result = vec![dataset.columns];

        for row in dataset.rows {
            let string_row: Vec<String> =
                row.iter().map(|v| v.to_display_string()).collect();
            result.push(string_row);
        }

        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cell_value_parse() {
        assert_eq!(CellValue::parse(""), CellValue::Empty);
        assert_eq!(CellValue::parse("true"), CellValue::Boolean(true));
        assert_eq!(CellValue::parse("false"), CellValue::Boolean(false));
        assert_eq!(CellValue::parse("42"), CellValue::Integer(42));
        assert_eq!(CellValue::parse("3.14"), CellValue::Number(3.14));
        assert_eq!(CellValue::parse("hello"), CellValue::String("hello".to_string()));
    }

    #[test]
    fn test_cell_value_numeric() {
        assert!(CellValue::Integer(42).is_numeric());
        assert!(CellValue::Number(3.14).is_numeric());
        assert!(!CellValue::String("42".to_string()).is_numeric());
        assert!(!CellValue::Boolean(true).is_numeric());
    }

    #[test]
    fn test_cell_value_as_number() {
        assert_eq!(CellValue::Integer(42).as_number(), Some(42.0));
        assert_eq!(CellValue::Number(3.14).as_number(), Some(3.14));
        assert_eq!(CellValue::String("42".to_string()).as_number(), None);
    }

    #[test]
    fn test_dataset_conversion() {
        let legacy = vec![
            vec!["name".to_string(), "age".to_string()],
            vec!["Alice".to_string(), "30".to_string()],
            vec!["Bob".to_string(), "25".to_string()],
        ];

        let dataset: DataSet = legacy.clone().into();
        assert_eq!(dataset.columns, vec!["name", "age"]);
        assert_eq!(dataset.row_count(), 2);

        let back: Vec<Vec<String>> = dataset.into();
        assert_eq!(back, legacy);
    }
}