coreutils/src/nl/helper.rs

144 lines
4.3 KiB
Rust
Raw Normal View History

2014-06-14 14:43:39 +00:00
extern crate getopts;
extern crate regex;
// parse_style parses a style string into a NumberingStyle.
fn parse_style(chars: &[char]) -> Result<::NumberingStyle, String> {
if chars.len() == 1 && chars[0] == 'a' {
Ok(::NumberingStyle::NumberForAll)
} else if chars.len() == 1 && chars[0] == 't' {
Ok(::NumberingStyle::NumberForNonEmpty)
} else if chars.len() == 1 && chars[0] == 'n' {
Ok(::NumberingStyle::NumberForNone)
} else if chars.len() > 1 && chars[0] == 'p' {
2016-01-05 19:42:52 +00:00
let s: String = chars[1..].iter().cloned().collect();
match regex::Regex::new(&s) {
Ok(re) => Ok(::NumberingStyle::NumberForRegularExpression(re)),
Err(_) => Err(String::from("Illegal regular expression")),
2014-06-14 14:43:39 +00:00
}
} else {
Err(String::from("Illegal style encountered"))
2014-06-14 14:43:39 +00:00
}
}
// parse_options loads the options into the settings, returning an array of
// error messages.
pub fn parse_options(settings: &mut ::Settings, opts: &getopts::Matches) -> Vec<String> {
// This vector holds error messages encountered.
let mut errs: Vec<String> = vec![];
settings.renumber = !opts.opt_present("p");
match opts.opt_str("s") {
2018-03-12 08:20:58 +00:00
None => {}
Some(val) => {
settings.number_separator = val;
}
2014-06-14 14:43:39 +00:00
}
match opts.opt_str("n") {
2018-03-12 08:20:58 +00:00
None => {}
Some(val) => match val.as_ref() {
2018-03-12 08:20:58 +00:00
"ln" => {
settings.number_format = ::NumberFormat::Left;
}
"rn" => {
settings.number_format = ::NumberFormat::Right;
}
"rz" => {
settings.number_format = ::NumberFormat::RightZero;
}
_ => {
errs.push(String::from("Illegal value for -n"));
}
},
2014-06-14 14:43:39 +00:00
}
match opts.opt_str("b") {
2018-03-12 08:20:58 +00:00
None => {}
2014-06-14 14:43:39 +00:00
Some(val) => {
let chars: Vec<char> = val.chars().collect();
match parse_style(&chars) {
2018-03-12 08:20:58 +00:00
Ok(s) => {
settings.body_numbering = s;
}
Err(message) => {
errs.push(message);
}
2014-06-14 14:43:39 +00:00
}
}
}
match opts.opt_str("f") {
2018-03-12 08:20:58 +00:00
None => {}
2014-06-14 14:43:39 +00:00
Some(val) => {
let chars: Vec<char> = val.chars().collect();
match parse_style(&chars) {
2018-03-12 08:20:58 +00:00
Ok(s) => {
settings.footer_numbering = s;
}
Err(message) => {
errs.push(message);
}
2014-06-14 14:43:39 +00:00
}
}
}
match opts.opt_str("h") {
2018-03-12 08:20:58 +00:00
None => {}
2014-06-14 14:43:39 +00:00
Some(val) => {
let chars: Vec<char> = val.chars().collect();
match parse_style(&chars) {
2018-03-12 08:20:58 +00:00
Ok(s) => {
settings.header_numbering = s;
}
Err(message) => {
errs.push(message);
}
2014-06-14 14:43:39 +00:00
}
}
}
match opts.opt_str("i") {
None => {}
Some(val) => {
let conv: Option<u64> = val.parse().ok();
2014-06-14 14:43:39 +00:00
match conv {
2018-03-12 08:20:58 +00:00
None => {
errs.push(String::from("Illegal value for -i"));
}
Some(num) => settings.line_increment = num,
2014-06-14 14:43:39 +00:00
}
}
}
match opts.opt_str("w") {
None => {}
Some(val) => {
let conv: Option<usize> = val.parse().ok();
2014-06-14 14:43:39 +00:00
match conv {
2018-03-12 08:20:58 +00:00
None => {
errs.push(String::from("Illegal value for -w"));
}
Some(num) => settings.number_width = num,
2014-06-14 14:43:39 +00:00
}
}
}
match opts.opt_str("v") {
None => {}
Some(val) => {
let conv: Option<u64> = val.parse().ok();
2014-06-14 14:43:39 +00:00
match conv {
2018-03-12 08:20:58 +00:00
None => {
errs.push(String::from("Illegal value for -v"));
}
Some(num) => settings.starting_line_number = num,
2014-06-14 14:43:39 +00:00
}
}
}
match opts.opt_str("l") {
None => {}
Some(val) => {
let conv: Option<u64> = val.parse().ok();
2014-06-14 14:43:39 +00:00
match conv {
2018-03-12 08:20:58 +00:00
None => {
errs.push(String::from("Illegal value for -l"));
}
Some(num) => settings.join_blank_lines = num,
2014-06-14 14:43:39 +00:00
}
}
}
errs
}