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
use std::fmt; use chrono_tz::Tz; use crate::error::CliResult; use crate::util::{csv_util, timestamp_util}; pub static DELIMITER_DEFAULT: &str = ","; pub static TIMESTAMP_COL_DATE_DEFAULT: usize = 0; pub type DateCol = usize; pub type TimeCol = usize; #[derive(Clone)] pub enum TimestampCol { Timestamp(usize), DateAndTime(DateCol, TimeCol), } #[derive(Clone)] pub struct TimestampConfig { timestamp_col: TimestampCol, timestamp_fmt: Option<String>, timezone: Tz, } impl TimestampConfig { pub fn new(timestamp_col: TimestampCol, timestamp_fmt: Option<String>, timezone: Tz) -> Self { TimestampConfig { timestamp_col, timestamp_fmt, timezone } } pub fn default() -> Self { let timestamp_col = TimestampCol::Timestamp(0); let timezone = timestamp_util::DEFAULT_ZONE; TimestampConfig { timestamp_col, timestamp_fmt: None, timezone } } pub fn timestamp_col(&mut self) -> &mut TimestampCol { &mut self.timestamp_col } pub fn timestamp_fmt(&mut self) -> &mut Option<String> { &mut self.timestamp_fmt } pub fn set_timestamp_fmt(&mut self, fmt: String) { self.timestamp_fmt = Some(fmt) } pub fn timezone(&self) -> Tz { self.timezone } } #[derive(Clone)] pub struct CSVInputConfig { delimiter: u8, has_header: bool, timestamp_config: TimestampConfig, } #[derive(Clone)] pub struct CSVOutputConfig { delimiter: String, print_timestamp: bool } impl CSVInputConfig { pub fn new(delimiter: &str, has_header: bool, timestamp_config: TimestampConfig) -> CliResult<Self> { let delimiter = csv_util::parse_into_delimiter(delimiter)?; Ok(CSVInputConfig { delimiter, has_header, timestamp_config }) } pub fn new_default() -> CliResult<Self> { let delimiter = csv_util::parse_into_delimiter(DELIMITER_DEFAULT)?; let timestamp_config = TimestampConfig::default(); Ok(CSVInputConfig { delimiter, has_header: false, timestamp_config }) } pub fn has_header(&self) -> bool { self.has_header } pub fn delimiter(&self) -> u8 { self.delimiter } pub fn timestamp_config(&mut self) -> &mut TimestampConfig { &mut 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: 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) } }