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
use std::fmt;

use crate::cli::util::YesNoAuto;
use crate::error::CliResult;
use crate::util::csv_util;
use crate::util::tz::ChopperTz;

pub static OUTPUT_DELIMITER_DEFAULT: &str = ",";
pub static TIMESTAMP_COL_DATE_DEFAULT: usize = 0;

pub type DateColIdx = usize;
pub type TimeColIdx = usize;

#[derive(Clone)]
pub enum TimestampFmtConfig {
    Auto,
    Explicit(String),
    DateTimeExplicit(String, String),
}

#[derive(Clone)]
pub enum TimestampColConfig {
    Auto,
    Index(usize),
    DateTimeIndex(DateColIdx, TimeColIdx),
    Name(String),
    DateTimeName(String, String),
}

#[derive(Clone)]
pub struct TimestampConfig {
    timestamp_col: TimestampColConfig,
    timestamp_fmt: TimestampFmtConfig,
    timezone: ChopperTz,
}

impl TimestampConfig {
    pub fn new(
        timestamp_col: TimestampColConfig,
        timestamp_fmt: TimestampFmtConfig,
        timezone: ChopperTz,
    ) -> Self {
        TimestampConfig {
            timestamp_col,
            timestamp_fmt,
            timezone,
        }
    }

    pub fn timestamp_col(&self) -> &TimestampColConfig {
        &self.timestamp_col
    }

    pub fn timestamp_fmt(&self) -> &TimestampFmtConfig {
        &self.timestamp_fmt
    }

    pub fn timezone(&self) -> &ChopperTz {
        &self.timezone
    }
}

#[derive(Clone)]
pub struct CSVInputConfig {
    delimiter: Option<u8>,
    has_header: YesNoAuto,
    timestamp_config: TimestampConfig,
}

#[derive(Clone)]
pub struct CSVOutputConfig {
    delimiter: String,
    print_timestamp: bool,
}

impl CSVInputConfig {
    pub fn new(
        delimiter: Option<&str>,
        has_header: YesNoAuto,
        timestamp_config: TimestampConfig,
    ) -> CliResult<Self> {
        let delimiter = match delimiter {
            None => None,
            Some(x) => Some(csv_util::parse_into_delimiter(x)?),
        };
        Ok(CSVInputConfig {
            delimiter,
            has_header,
            timestamp_config,
        })
    }

    pub fn has_header(&self) -> YesNoAuto {
        self.has_header
    }

    pub fn delimiter(&self) -> Option<u8> {
        self.delimiter
    }

    pub fn timestamp_config(&self) -> &TimestampConfig {
        &self.timestamp_config
    }
}

impl CSVOutputConfig {
    pub fn new(delimiter: &str, print_timestamp: bool) -> Self {
        CSVOutputConfig {
            delimiter: delimiter.to_string(),
            print_timestamp,
        }
    }

    pub fn new_default() -> Self {
        CSVOutputConfig {
            delimiter: OUTPUT_DELIMITER_DEFAULT.to_string(),
            print_timestamp: true,
        }
    }

    pub fn delimiter(&self) -> &String {
        &self.delimiter
    }

    pub fn print_timestamp(&self) -> bool {
        self.print_timestamp
    }
}

impl fmt::Debug for CSVInputConfig {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "delimiter: {:?}, has headers: {:?}",
            self.delimiter, self.has_header
        )
    }
}