fuzzy_datetime/
date_order.rs

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

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DateOrder {
  YMD,
  DMY,
  MDY,
}

impl DateOrder {
  /// render date format as indices for year, month and day
  pub fn to_ymd_indices(&self) -> (usize, usize, usize) {
    match self {
      DateOrder::YMD => (0, 1, 2),
      DateOrder::DMY => (2, 1, 0),
      DateOrder::MDY => (2, 0, 1),
    }
  }
}


/// Options for parsing the date component of strings
pub struct DateOptions(pub DateOrder, pub char);

impl DateOptions {
  pub fn order(&self) -> DateOrder {
    self.0
  }

  pub fn splitter(&self) -> char {
    self.1
  }
}

impl Default for DateOptions {
  fn default() -> Self {
    DateOptions(DateOrder::YMD, '-')
  }
}

/// instantiate options with three common orders + split character
/// e.g. DateOptions::dmy('.')
impl DateOptions {
  pub fn ymd(splitter: char) -> Self {
    DateOptions(DateOrder::YMD, splitter)
  }

  pub fn dmy(splitter: char) -> Self {
    DateOptions(DateOrder::DMY, splitter)
  }

  pub fn mdy(splitter: char) -> Self {
    DateOptions(DateOrder::MDY, splitter)
  }
}