tablefi 0.3.1

Simple table to store, manipulate and format tabular data.
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
use regex::Regex;
use rust_decimal::Decimal;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::{Value};
use std::ops::{Add, Sub, Mul, Div};
use std::cmp::Ordering;
use std::str::FromStr;
use std::sync::LazyLock;

static RE_NUMERIC_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r#"^[+-]?(?:(?:(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d+)?)|(?:\.\d+))$"#).unwrap()
});

static RE_STRIP_CHARS: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"[^0-9+.-]").unwrap()
});

pub const DIV0: &str = "#DIV/0";

/// Represents a single cell in a table, which can either contain text or a number.
///
/// # Examples
///
/// ```
/// use rust_decimal::Decimal;
/// use std::cmp::Ordering;
/// use tablefi::Cell;
///
/// let text_cell = Cell::Text("hello".to_string());
/// // or
/// let text_cell = Cell::from("hello");
/// assert_eq!(text_cell.to_string(), "hello");
///
/// let num_cell = Cell::Number(Decimal::new(1234567, 2));
/// // or
/// let num_cell = Cell::from(Decimal::new(1234567, 2));
/// // or
/// let text_cell = Cell::from("12,345.67");
/// assert_eq!(num_cell.to_string(), "12345.67");
/// 
/// // Adding two cells
/// let number1 = Cell::from("123.456");
/// let number2 = Cell::from("8");
/// let mut number3 = &number1 + &number2;
/// number3.add_value(Decimal::from(8));
/// 
/// // Comparing cell values
/// assert_eq!(number3.compare_value(&Decimal::new(123456, 3)), Some(Ordering::Greater));
/// assert_eq!(number3.equal_value(&Decimal::new(139456, 3)), true);
/// ```
#[derive(Clone, Debug, PartialEq)]
pub enum Cell {
    /// A cell containing textual data, stored as a `String`.
    Text(String),
    /// A cell containing a numerical value, stored as a `Decimal` for precision.
    Number(Decimal),
}

impl Default for Cell {

    fn default() -> Self {
        Cell::Text("".to_string())
    }

}

impl Serialize for Cell {

    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> 
    where
        S: Serializer,
    {
        // serialize numbers as strings to maintain precision
        self.to_string().serialize(serializer)
    }

}

impl<'de> Deserialize<'de> for Cell {

    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let val = Value::deserialize(deserializer)?;
        let str = match val {
            Value::String(s) => s,
            Value::Number(n) => n.to_string(),
            Value::Bool(b) => b.to_string(),
            Value::Null => String::new(),
            Value::Array(a) => serde_json::to_string(&a).unwrap_or_default(),
            Value::Object(o) => serde_json::to_string(&o).unwrap_or_default(),
        };
        Ok(Cell::from(str))
    }

}

impl ToString for Cell {

    fn to_string(&self) -> String {
        match self {
            Cell::Text(s) => s.to_string(),
            Cell::Number(n) => n.to_string(),
        }
    }

}

fn cell_from_string(s_ref: &str) -> Cell {
    // filter for number pattern like (+/-)123,456.789
    if RE_NUMERIC_PATTERN.is_match(s_ref) {
        // strip unecessary characters appropriate for decimal
        let stripped = RE_STRIP_CHARS.replace_all(s_ref, "");
        if let Ok(d) = Decimal::from_str(&stripped) {
            return Cell::Number(d);
        }
    }
    // otherwise return text
    Cell::Text(s_ref.to_string())
}


impl From<String> for Cell {

    fn from(s: String) -> Self {
        cell_from_string(s.as_str())
    }

}

impl<'a> From<&'a String> for Cell {

    fn from(s_ref: &'a String) -> Self {
        cell_from_string(s_ref.as_str())
    }

}

impl From<&str> for Cell {

    fn from(s: &str) -> Self {
        Self::from(s.to_string())
    }

}

impl<'a> From<&'a Decimal> for Cell {

    fn from(d_ref: &'a Decimal) -> Self {
        Cell::Number(*d_ref)
    }

}

impl From<Decimal> for Cell {

    fn from(n: Decimal) -> Self {
        Cell::Number(n)
    }

}

impl<'a> From<&'a Cell> for Cell {

    fn from(c_ref: &'a Cell) -> Self {
        c_ref.clone()
    }

}

impl TryFrom<Cell> for Decimal {
    type Error = String;

    fn try_from(cell: Cell) -> Result<Self, Self::Error> {
        match cell {
            Cell::Text(_) => Err("Cell is not a number".to_string()),
            Cell::Number(d) => Ok(d),
        }
    }

}

impl Add<&Cell> for &Cell {

    type Output = Cell;

    fn add(self, other: &Cell) -> Cell {
        if self.is_number() && other.is_number() {
            Cell::Number(self.to_decimal().unwrap() + other.to_decimal().unwrap())
        } else {
            self.clone()
        }
    }

}

impl Sub<&Cell> for &Cell {

    type Output = Cell;

    fn sub(self, other: &Cell) -> Cell {
        if self.is_number() && other.is_number() {
            Cell::Number(self.to_decimal().unwrap() - other.to_decimal().unwrap())
        } else {
            self.clone()
        }
    }

}

impl Mul<&Cell> for &Cell {

    type Output = Cell;

    fn mul(self, other: &Cell) -> Cell {
        if self.is_number() && other.is_number() {
            Cell::Number(self.to_decimal().unwrap() * other.to_decimal().unwrap())
        } else {
            self.clone()
        }
    }

}

impl Div<&Cell> for &Cell {
    
    type Output = Cell;

    fn div(self, other: &Cell) -> Cell {
        if self.is_number() && other.is_number() {
            let other_val = other.to_decimal().unwrap();
            if other_val.is_zero() {
                return Cell::from(DIV0);
            }
            Cell::Number(self.to_decimal().unwrap() / other_val)
        } else {
            self.clone()
        }
    }

}

impl Cell {

    /// Whether this cell contains textual data.
    pub fn is_text(&self) -> bool {
        match self {
            Cell::Text(_) => true,
            Cell::Number(_) => false,
        }
    }

    /// Whether this cell contains a numerical value.
    pub fn is_number(&self) -> bool {
        !self.is_text()
    }

    /// Converts the cell to a Decimal.
    pub fn to_decimal(&self) -> Option<Decimal> {
        TryInto::<Decimal>::try_into(self.clone()).ok()
    }

    /// Replaces the cell with a new value.
    pub fn replace_value(&mut self, new_value: &Cell) {
        *self = new_value.clone();
    }

    /// Adds value.
    pub fn add_value(&mut self, value: Decimal) {
        if let Cell::Number(d) = self {
            *d += value;
        }
    }

    /// Subtracts value.
    pub fn sub_value(&mut self, value: Decimal) {
        if let Cell::Number(d) = self {
            *d -= value;
        }
    }

    /// Multiplies value.
    pub fn mul_value(&mut self, value: Decimal) {
        if let Cell::Number(d) = self {
            *d *= value;
        }
    }

    /// Divides value.
    pub fn div_value(&mut self, value: Decimal) {
        if let Cell::Number(d) = self {
            match value.is_zero() {
                true => *self = Cell::from(DIV0),
                false => *d /= value,
            }
        }
    }

    /// Whether the value of the cell has been divided by zero.
    pub fn is_divide_by_zero(&self) -> bool {
        self.to_string() == DIV0
    }

    /// Compares the value of this cell with another value.
    ///
    /// The `other_value` can be a `String`, `&str`, `Decimal`, or another `Cell`.
    /// It returns `Some(Ordering)` if the types are comparable (Number with Number, Text with Text),
    /// and `None` otherwise (e.g., Text with Number).
    ///
    /// # Examples
    /// ```
    /// use tablefi::Cell;
    /// use rust_decimal::Decimal;
    /// use std::cmp::Ordering;
    ///
    /// assert_eq!(Cell::from("10").compare_value("5"), Some(Ordering::Greater));
    /// assert_eq!(Cell::from("apple").compare_value("banana"), Some(Ordering::Less));
    /// assert_eq!(Cell::from("10").compare_value("banana"), None);
    /// assert_eq!(Cell::Number(Decimal::new(5,0)).compare_value(&Decimal::new(5,0)), Some(Ordering::Equal));
    /// ```
    pub fn compare_value<T: ?Sized>(&self, other_value: &T) -> Option<Ordering> where for<'r> &'r T: Into<Cell> {
        let other_cell: Cell = other_value.into();
        match (self, other_cell) {
            (Cell::Number(n1), Cell::Number(n2)) => n1.partial_cmp(&n2),
            (Cell::Text(s1), Cell::Text(s2)) => s1.partial_cmp(&s2),
            _ => None, // Mismatched types (Number vs Text or Text vs Number)
        }
    }

    /// Whether the value of this cell is equal to another value.
    /// 
    /// The `other_value` can be a `String`, `&str`, `Decimal`, or another `Cell`.
    pub fn equal_value<T: ?Sized>(&self, other_value: &T) -> bool where for<'r> &'r T: Into<Cell> {
        self.compare_value(other_value) == Some(Ordering::Equal)
    }

}

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

    #[test]
    fn test_cell() {
        let cell = Cell::Text("Hello, world!".to_string());
        assert_eq!(cell.to_string(), "Hello, world!");
        let cell = Cell::Number(Decimal::from(12345));
        assert_eq!(cell.to_string(), "12345");
    }
    
    #[test]
    fn test_cell_string() {
        let s = "Hello, world!".to_string();
        let cell: Cell = s.clone().into();
        assert_eq!(cell, Cell::Text("Hello, world!".to_string()));
        let cell = Cell::from(s.clone());
        assert_eq!(cell, Cell::Text("Hello, world!".to_string()));
        assert_eq!(cell.to_string(), "Hello, world!");
        assert!(TryInto::<Decimal>::try_into(cell).is_err());
    }

    #[test]
    fn test_cell_from_str() {
        let s = "Hello, world!";
        let cell: Cell = s.into();
        assert_eq!(cell, Cell::Text("Hello, world!".to_string()));
        let cell = Cell::from(s);
        assert_eq!(cell, Cell::Text("Hello, world!".to_string()));
        assert_eq!(cell.to_string(), "Hello, world!");
    }

    #[test]
    fn test_cell_decimal() {
        let d = Decimal::from(12345);
        let cell: Cell = d.clone().into();
        assert_eq!(cell, Cell::Number(Decimal::from(12345)));
        let cell = Cell::from(d.clone());
        assert_eq!(cell, Cell::Number(Decimal::from(12345)));
        assert!(TryInto::<Decimal>::try_into(cell.clone()).is_ok());
        assert_eq!(cell.to_decimal(), Some(Decimal::from(12345)));
        assert_eq!(cell.clone().to_string(), "12345");
    }

    #[test]
    fn test_string_or_decimal() {
        assert_eq!(TryInto::<Decimal>::try_into(Cell::from("12345678")).unwrap(), Decimal::from(12345678));
        assert_eq!(TryInto::<Decimal>::try_into(Cell::from("+12345678")).unwrap(), Decimal::from(12345678));
        assert_eq!(TryInto::<Decimal>::try_into(Cell::from("-12345678")).unwrap(), Decimal::from(-12345678));
        assert_eq!(TryInto::<Decimal>::try_into(Cell::from("123456.78")).unwrap(), Decimal::new(12345678, 2));
        assert_eq!(TryInto::<Decimal>::try_into(Cell::from("12,345,678")).unwrap(), Decimal::from(12345678));
        assert_eq!(TryInto::<Decimal>::try_into(Cell::from("-12,345,678.901")).unwrap(), Decimal::new(-12345678901, 3));
        assert!(TryInto::<Decimal>::try_into(Cell::from("++12345678")).is_err());
        assert!(TryInto::<Decimal>::try_into(Cell::from("1234.56.78")).is_err());
        assert!(TryInto::<Decimal>::try_into(Cell::from("-12,34,567,8.901")).is_err());
        assert!(TryInto::<Decimal>::try_into(Cell::from("-123,456,78.901")).is_err());
        assert_eq!(TryInto::<Decimal>::try_into(Cell::from("-123,456,781.901")).unwrap(), Decimal::new(-123456781901, 3));
        assert!(TryInto::<Decimal>::try_into(Cell::from("-123,456,78")).is_err());
        assert_eq!(TryInto::<Decimal>::try_into(Cell::from("-123,456,781")).unwrap(), Decimal::from(-123456781));
        assert!(TryInto::<Decimal>::try_into(Cell::from("\"-123,456,781\"")).is_err());
        assert!(TryInto::<Decimal>::try_into(Cell::from("'-123,456,781'")).is_err());
        assert!(TryInto::<Decimal>::try_into(Cell::from("-12a,456,781")).is_err());
    }

    #[test]
    fn test_json() {
        let cell: Cell = serde_json::from_str(r#""hello""#).unwrap();
        assert_eq!(cell.to_string(), "hello");
        let cell: Cell = serde_json::from_str(r#""12345.67""#).unwrap();
        assert_eq!(cell.to_decimal(), Some(Decimal::new(1234567, 2)));
        let cell: Cell = serde_json::from_str(r#"12345.67"#).unwrap();
        assert_eq!(cell.to_decimal(), Some(Decimal::new(1234567, 2)));
        let cell: Cell = serde_json::from_str(r#"true"#).unwrap();
        assert_eq!(cell.to_string(), "true");
        let cell: Cell = serde_json::from_str(r#"null"#).unwrap();
        assert_eq!(cell.to_string(), "");
        let cell: Cell = serde_json::from_str(r#"["a"]"#).unwrap();
        assert_eq!(cell.to_string(), r#"["a"]"#);
        let cell: Cell = serde_json::from_str(r#"{"a":1}"#).unwrap();
        assert_eq!(cell.to_string(), r#"{"a":1}"#);
    }

    #[test]
    fn test_is_text() {
        let cell = Cell::Text("Hello, world!".to_string());
        assert!(cell.is_text());
        assert!(!cell.is_number());
    }

    #[test]
    fn test_is_number() {
        let cell = Cell::Number(Decimal::from(12345));
        assert!(!cell.is_text());
        assert!(cell.is_number());
    }

    #[test]
    fn test_cell_replace() {
        let mut cell = Cell::from("123.456");
        assert_eq!(cell.to_decimal(), Some(Decimal::new(123456, 3)));
        cell.replace_value(&Cell::from("abcd"));
        assert_eq!(cell.to_string(), "abcd".to_string());
    }

    #[test]
    fn test_cell_add() {
        let number1 = Cell::from("123.456");
        let number2 = Cell::from("8");
        assert_eq!((&number1 + &number2).to_decimal(), Some(Decimal::new(131456, 3)));
        let text1 = Cell::from("abcd");
        assert_eq!((&text1 + &number2).to_string(), "abcd".to_string());
        let mut number3 = number1.clone();
        number3.add_value(Decimal::from(8));
        assert_eq!(number3.to_decimal(), Some(Decimal::new(131456, 3)));
    }

    #[test]
    fn test_cell_sub() {
        let number1 = Cell::from("123.456");
        let number2 = Cell::from("8");
        assert_eq!((&number1 - &number2).to_decimal(), Some(Decimal::new(115456, 3)));
        let mut number3 = number1.clone();
        number3.sub_value(Decimal::from(8));
        assert_eq!(number3.to_decimal(), Some(Decimal::new(115456, 3)));
    }

    #[test]
    fn test_cell_mul() {
        let number1 = Cell::from("123.456");
        let number2 = Cell::from("8");
        assert_eq!((&number1 * &number2).to_decimal(), Some(Decimal::new(987648, 3)));
        let mut number3 = number1.clone();
        number3.mul_value(Decimal::from(8));
        assert_eq!(number3.to_decimal(), Some(Decimal::new(987648, 3)));
    }

    #[test]
    fn test_cell_div() {
        let number1 = Cell::from("123.456");
        let number2 = Cell::from("8");
        assert_eq!((&number1 / &number2).to_decimal(), Some(Decimal::new(15432, 3)));
        assert!((&number1 / &Cell::from("0")).to_decimal().is_none());
        assert!((&number1 / &Cell::from("0")).is_divide_by_zero());
        let mut number3 = number1.clone();
        number3.div_value(Decimal::from(8));
        assert_eq!(number3.to_decimal(), Some(Decimal::new(15432, 3)));
        number3.div_value(Decimal::from(0));
        assert!(number3.to_decimal().is_none());
        assert!(number3.is_divide_by_zero());
    }

    #[test]
    fn test_compare_value_number() {
        // numbers
        let dec_10 = Decimal::from(10);
        let dec_5 = Decimal::from(5);
        let num_10 = Cell::from(dec_10);
        let num_5 = Cell::from(dec_5);

        // Number comparisons
        assert_eq!(num_10.compare_value(&num_5), Some(Ordering::Greater));
        assert_eq!(num_5.compare_value(&num_10), Some(Ordering::Less));
        assert_eq!(num_10.compare_value(&Cell::from("10.0")), Some(Ordering::Equal));

        // Number vs Decimal
        assert_eq!(num_10.compare_value(&Decimal::from(5)), Some(Ordering::Greater));
        assert_eq!(num_10.compare_value(&Decimal::from(10)), Some(Ordering::Equal));
        assert_eq!(num_10.compare_value(&Decimal::from(20)), Some(Ordering::Less));

        // Number vs String (numeric)
        assert_eq!(num_10.compare_value("5"), Some(Ordering::Greater));
        assert_eq!(num_10.compare_value("10.0"), Some(Ordering::Equal));
        assert_eq!(num_10.compare_value("20"), Some(Ordering::Less));
        
        // Number vs String (non-numeric)
        assert_eq!(num_10.compare_value("abc"), None);
        assert_eq!(num_10.compare_value(&"abc".to_string()), None);
        assert_eq!(num_10.compare_value(&Cell::from("abc")), None);
    }

    #[test]
    fn test_compare_value_text() {
        // text
        let str_apple = "apple"; // &str
        let str_banana = "banana"; // &str
        let text_apple = Cell::from(str_apple);
        let text_banana = Cell::from(str_banana);

        // Text comparisons
        assert_eq!(text_apple.compare_value(&text_banana), Some(Ordering::Less));
        assert_eq!(text_banana.compare_value(&text_apple), Some(Ordering::Greater));
        assert_eq!(text_apple.compare_value(&Cell::Text("apple".to_string())), Some(Ordering::Equal));
        
        // Text vs str
        assert_eq!(text_apple.compare_value("banana"), Some(Ordering::Less));
        assert_eq!(text_apple.compare_value("apple"), Some(Ordering::Equal));
        assert_eq!(text_banana.compare_value("apple"), Some(Ordering::Greater));

        // Text vs String
        assert_eq!(text_apple.compare_value(&"banana".to_string()), Some(Ordering::Less));
        assert_eq!(text_apple.compare_value(&"apple".to_string()), Some(Ordering::Equal));
        assert_eq!(text_banana.compare_value(&"apple".to_string()), Some(Ordering::Greater));

        // Text vs Number
        assert_eq!(text_apple.compare_value(&Cell::from("10")), None);
        assert_eq!(text_apple.compare_value(&Decimal::from(10)), None);
    }

    #[test]
    fn test_equal_value() {
        // numbers
        let dec_10 = Decimal::from(10);
        let dec_5 = Decimal::from(5);
        let num_10 = Cell::from(dec_10);
        let num_5 = Cell::from(dec_5);

        // text
        let str_apple = "apple";
        let str_banana = "banana";
        let text_apple = Cell::from(str_apple);
        let text_banana = Cell::from(str_banana);

        // Number comparisons
        assert_eq!(num_10.equal_value(&num_5), false);
        assert_eq!(num_5.equal_value(&num_10), false);
        assert_eq!(num_10.equal_value(&Cell::from("10.0")), true);

        // Number vs Decimal
        assert_eq!(num_10.equal_value(&Decimal::from(5)), false);
        assert_eq!(num_10.equal_value(&Decimal::from(10)), true);
        assert_eq!(num_10.equal_value(&Decimal::from(20)), false);

        // Number vs String (numeric)
        assert_eq!(num_10.equal_value("5"), false);
        assert_eq!(num_10.equal_value("10.0"), true);
        assert_eq!(num_10.equal_value("20"), false);
        
        // Number vs String (non-numeric)
        assert_eq!(num_10.equal_value("abc"), false);
        assert_eq!(num_10.equal_value(&"abc".to_string()), false);
        assert_eq!(num_10.equal_value(&Cell::from("abc")), false);

        // Text comparisons
        assert_eq!(text_apple.equal_value(&text_banana), false);
        assert_eq!(text_banana.equal_value(&text_apple), false);
        assert_eq!(text_apple.equal_value(&Cell::Text("apple".to_string())), true);
        
        // Text vs str
        assert_eq!(text_apple.equal_value("banana"), false);
        assert_eq!(text_apple.equal_value("apple"), true);
        assert_eq!(text_banana.equal_value("apple"), false);
        
        // Text vs String
        assert_eq!(text_apple.equal_value(&"banana".to_string()), false);
        assert_eq!(text_apple.equal_value(&"apple".to_string()), true);
        assert_eq!(text_banana.equal_value(&"apple".to_string()), false);

        // Text vs Number
        assert_eq!(text_apple.equal_value(&Cell::from("10")), false);
        assert_eq!(text_apple.equal_value(&Decimal::from(10)), false);
    }

}