summaryrefslogtreecommitdiffstats
path: root/src/config.rs
blob: d87a602b7c62ba802e9f8a42b9fae88869d68ee5 (plain)
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
use regex::Regex;
use std::num::ParseIntError;
use std::process;

use crate::choice::Choice;
use crate::opt::Opt;

lazy_static! {
    static ref PARSE_CHOICE_RE: Regex = Regex::new(r"^(-?\d*):(-?\d*)$").unwrap();
}

pub struct Config {
    pub opt: Opt,
    pub separator: Regex,
    pub output_separator: Box<[u8]>,
}

impl Config {
    pub fn new(mut opt: Opt) -> Self {
        if opt.exclusive {
            for mut choice in &mut opt.choice {
                if choice.is_reverse_range() {
                    choice.start = choice.start - 1;
                } else {
                    choice.end = choice.end - 1;
                }
            }
        }

        let separator = match Regex::new(match &opt.field_separator {
            Some(s) => s,
            None => "[[:space:]]",
        }) {
            Ok(r) => r,
            Err(e) => {
                // Exit code of 2 means failed to compile field_separator regex
                match e {
                    regex::Error::Syntax(e) => {
                        eprintln!("Syntax error compiling regular expression: {}", e);
                        process::exit(2);
                    }
                    regex::Error::CompiledTooBig(e) => {
                        eprintln!("Compiled regular expression too big: compiled size cannot exceed {} bytes", e);
                        process::exit(2);
                    }
                    _ => {
                        eprintln!("Error compiling regular expression: {}", e);
                        process::exit(2);
                    }
                }
            }
        };

        let output_separator = match opt.output_field_separator.clone() {
            Some(s) => s.into_boxed_str().into_boxed_bytes(),
            None => Box::new([0x20; 1]),
        };

        Config {
            opt,
            separator,
            output_separator,
        }
    }

    pub fn parse_choice(src: &str) -> Result<Choice, ParseIntError> {
        let cap = match PARSE_CHOICE_RE.captures_iter(src).next() {
            Some(v) => v,
            None => match src.parse() {
                Ok(x) => return Ok(Choice::new(x, x)),
                Err(e) => {
                    eprintln!("failed to parse choice argument: {}", src);
                    return Err(e);
                }
            },
        };

        let start = if cap[1].is_empty() {
            0
        } else {
            match cap[1].parse() {
                Ok(x) => x,
                Err(e) => {
                    eprintln!("failed to parse range start: {}", &cap[1]);
                    return Err(e);
                }
            }
        };

        let end = if cap[2].is_empty() {
            isize::max_value()
        } else {
            match cap[2].parse() {
                Ok(x) => x,
                Err(e) => {
                    eprintln!("failed to parse range end: {}", &cap[2]);
                    return Err(e);
                }
            }
        };

        return Ok(Choice::new(start, end));
    }

    pub fn parse_output_field_separator(src: &str) -> String {
        String::from(src)
    }
}

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

    mod parse_choice_tests {
        use super::*;

        #[test]
        fn parse_single_choice_start() {
            let result = Config::parse_choice("6").unwrap();
            assert_eq!(6, result.start)
        }

        #[test]
        fn parse_single_choice_end() {
            let result = Config::parse_choice("6").unwrap();
            assert_eq!(6, result.end)
        }

        #[test]
        fn parse_none_started_range() {
            let result = Config::parse_choice(":5").unwrap();
            assert_eq!((0, 5), (result.start, result.end))
        }

        #[test]
        fn parse_none_terminated_range() {
            let result = Config::parse_choice("5:").unwrap();
            assert_eq!((5, isize::max_value()), (result.start, result.end))
        }

        #[test]
        fn parse_full_range_pos_pos() {
            let result = Config::parse_choice("5:7").unwrap();
            assert_eq!((5, 7), (result.start, result.end))
        }

        #[test]
        fn parse_full_range_neg_neg() {
            let result = Config::parse_choice("-3:-1").unwrap();
            assert_eq!((-3, -1), (result.start, result.end))
        }

        #[test]
        fn parse_neg_started_none_ended() {
            let result = Config::parse_choice("-3:").unwrap();
            assert_eq!((-3, isize::max_value()), (result.start, result.end))
        }

        #[test]
        fn parse_none_started_neg_ended() {
            let result = Config::parse_choice(":-1").unwrap();
            assert_eq!((0, -1), (result.start, result.end))
        }

        #[test]
        fn parse_full_range_pos_neg() {
            let result = Config::parse_choice("5:-3").unwrap();
            assert_eq!((5, -3), (result.start, result.end))
        }

        #[test]
        fn parse_full_range_neg_pos() {
            let result = Config::parse_choice("-3:5").unwrap();
            assert_eq!((-3, 5), (result.start, result.end))
        }

        #[test]
        fn parse_beginning_to_end_range() {
            let result = Config::parse_choice(":").unwrap();
            assert_eq!((0, isize::max_value()), (result.start, result.end))
        }

        #[test]
        fn parse_bad_choice() {
            assert!(Config::parse_choice("d").is_err());
        }

        #[test]
        fn parse_bad_range() {
            assert!(Config::parse_choice("d:i").is_err());
        }
    }
}