mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
Merge pull request #5100 from cakebaker/nl_shorten_enum_variants
nl: shorten variants of NumberingStyle enum
This commit is contained in:
commit
4004281f34
2 changed files with 16 additions and 19 deletions
|
@ -5,17 +5,15 @@ use crate::options;
|
||||||
// parse_style parses a style string into a NumberingStyle.
|
// parse_style parses a style string into a NumberingStyle.
|
||||||
fn parse_style(chars: &[char]) -> Result<crate::NumberingStyle, String> {
|
fn parse_style(chars: &[char]) -> Result<crate::NumberingStyle, String> {
|
||||||
if chars.len() == 1 && chars[0] == 'a' {
|
if chars.len() == 1 && chars[0] == 'a' {
|
||||||
Ok(crate::NumberingStyle::NumberForAll)
|
Ok(crate::NumberingStyle::All)
|
||||||
} else if chars.len() == 1 && chars[0] == 't' {
|
} else if chars.len() == 1 && chars[0] == 't' {
|
||||||
Ok(crate::NumberingStyle::NumberForNonEmpty)
|
Ok(crate::NumberingStyle::NonEmpty)
|
||||||
} else if chars.len() == 1 && chars[0] == 'n' {
|
} else if chars.len() == 1 && chars[0] == 'n' {
|
||||||
Ok(crate::NumberingStyle::NumberForNone)
|
Ok(crate::NumberingStyle::None)
|
||||||
} else if chars.len() > 1 && chars[0] == 'p' {
|
} else if chars.len() > 1 && chars[0] == 'p' {
|
||||||
let s: String = chars[1..].iter().cloned().collect();
|
let s: String = chars[1..].iter().cloned().collect();
|
||||||
match regex::Regex::new(&s) {
|
match regex::Regex::new(&s) {
|
||||||
Ok(re) => Ok(crate::NumberingStyle::NumberForRegularExpression(Box::new(
|
Ok(re) => Ok(crate::NumberingStyle::Regex(Box::new(re))),
|
||||||
re,
|
|
||||||
))),
|
|
||||||
Err(_) => Err(String::from("Illegal regular expression")),
|
Err(_) => Err(String::from("Illegal regular expression")),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -43,9 +43,9 @@ pub struct Settings {
|
||||||
impl Default for Settings {
|
impl Default for Settings {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
header_numbering: NumberingStyle::NumberForNone,
|
header_numbering: NumberingStyle::None,
|
||||||
body_numbering: NumberingStyle::NumberForAll,
|
body_numbering: NumberingStyle::All,
|
||||||
footer_numbering: NumberingStyle::NumberForNone,
|
footer_numbering: NumberingStyle::None,
|
||||||
section_delimiter: ['\\', ':'],
|
section_delimiter: ['\\', ':'],
|
||||||
starting_line_number: 1,
|
starting_line_number: 1,
|
||||||
line_increment: 1,
|
line_increment: 1,
|
||||||
|
@ -64,12 +64,11 @@ impl Default for Settings {
|
||||||
// 2. Number only nonempty lines
|
// 2. Number only nonempty lines
|
||||||
// 3. Don't number any lines at all
|
// 3. Don't number any lines at all
|
||||||
// 4. Number all lines that match a basic regular expression.
|
// 4. Number all lines that match a basic regular expression.
|
||||||
#[allow(clippy::enum_variant_names)]
|
|
||||||
enum NumberingStyle {
|
enum NumberingStyle {
|
||||||
NumberForAll,
|
All,
|
||||||
NumberForNonEmpty,
|
NonEmpty,
|
||||||
NumberForNone,
|
None,
|
||||||
NumberForRegularExpression(Box<regex::Regex>),
|
Regex(Box<regex::Regex>),
|
||||||
}
|
}
|
||||||
|
|
||||||
// NumberFormat specifies how line numbers are output within their allocated
|
// NumberFormat specifies how line numbers are output within their allocated
|
||||||
|
@ -279,7 +278,7 @@ fn nl<T: Read>(reader: &mut BufReader<T>, settings: &Settings) -> UResult<()> {
|
||||||
let mut empty_line_count: u64 = 0;
|
let mut empty_line_count: u64 = 0;
|
||||||
// Initially, we use the body's line counting settings
|
// Initially, we use the body's line counting settings
|
||||||
let mut regex_filter = match settings.body_numbering {
|
let mut regex_filter = match settings.body_numbering {
|
||||||
NumberingStyle::NumberForRegularExpression(ref re) => re,
|
NumberingStyle::Regex(ref re) => re,
|
||||||
_ => ®exp,
|
_ => ®exp,
|
||||||
};
|
};
|
||||||
let mut line_filter: fn(&str, ®ex::Regex) -> bool = pass_regex;
|
let mut line_filter: fn(&str, ®ex::Regex) -> bool = pass_regex;
|
||||||
|
@ -340,16 +339,16 @@ fn nl<T: Read>(reader: &mut BufReader<T>, settings: &Settings) -> UResult<()> {
|
||||||
// a catch-all here.
|
// a catch-all here.
|
||||||
_ => &settings.body_numbering,
|
_ => &settings.body_numbering,
|
||||||
} {
|
} {
|
||||||
NumberingStyle::NumberForAll => {
|
NumberingStyle::All => {
|
||||||
line_filter = pass_all;
|
line_filter = pass_all;
|
||||||
}
|
}
|
||||||
NumberingStyle::NumberForNonEmpty => {
|
NumberingStyle::NonEmpty => {
|
||||||
line_filter = pass_nonempty;
|
line_filter = pass_nonempty;
|
||||||
}
|
}
|
||||||
NumberingStyle::NumberForNone => {
|
NumberingStyle::None => {
|
||||||
line_filter = pass_none;
|
line_filter = pass_none;
|
||||||
}
|
}
|
||||||
NumberingStyle::NumberForRegularExpression(ref re) => {
|
NumberingStyle::Regex(ref re) => {
|
||||||
line_filter = pass_regex;
|
line_filter = pass_regex;
|
||||||
regex_filter = re;
|
regex_filter = re;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue