Repository: sharkdp/bat
File: src/app.rs (Rust)

Code before:
pub enum PagingMode {
    Always,
    QuitIfOneScreen,
    Never,
}

pub struct Config<'a> {
    pub true_color: bool,
    pub output_wrap: OutputWrap,
    pub output_components: OutputComponents,
    pub language: Option<&'a str>,
    pub colored_output: bool,
    pub paging_mode: PagingMode,
    pub term_width: usize,
    pub files: Vec<Option<&'a str>>,
    pub theme: Option<&'a str>,
    pub line_range: Option<(usize, usize)>,
}

fn is_truecolor_terminal() -> bool {
    env::var("COLORTERM")
        .map(|colorterm| colorterm == "truecolor" || colorterm == "24bit")
        .unwrap_or(false)
}

fn get_ranges(value: Option<&str>) -> Option<(usize, usize)> {
    match value {
        None => None,
        Some(str_range) => {
            let mut new_range = (usize::min_value(), usize::max_value());
            if str_range.bytes().nth(0).expect("Something should be here!") == b':' {
                new_range.1 = str_range[1..].parse().expect("This should be a number!");
                return Some(new_range);
            } else if str_range.bytes().last().expect("There should be a last!") == b':' {
                new_range.0 = str_range[..str_range.len() - 1]
                    .parse()
                    .expect("This should be a number!");
                return Some(new_range);
            }

            let line_numbers: Vec<&str> = str_range.split(':').collect();
            if line_numbers.len() == 2 {
                new_range.0 = line_numbers[0].parse().expect("Should be a number!");
                new_range.1 = line_numbers[1].parse().expect("Should be a number!");
            }

            Some(new_range)
        }
    }
}


Review comment (suggestion):
A few things:
* Maybe rename this to `parse_ranges`?
* This should probably return a `Result<...>` in order to properly handle the errors that may occur.
* The `Option<..>` is probably not necessary if we use `.map(get_ranges)`, as suggested above.

Code after:
}

#[derive(Debug, Clone, Copy)]
pub enum PagingMode {
    Always,
    QuitIfOneScreen,
    Never,
}

pub struct Config<'a> {
    pub true_color: bool,
    pub output_wrap: OutputWrap,
    pub output_components: OutputComponents,
    pub language: Option<&'a str>,
    pub colored_output: bool,
    pub paging_mode: PagingMode,
    pub term_width: usize,
    pub files: Vec<Option<&'a str>>,
    pub theme: Option<&'a str>,
    pub line_range: Option<LineRange>,
}

fn is_truecolor_terminal() -> bool {
    env::var("COLORTERM")
        .map(|colorterm| colorterm == "truecolor" || colorterm == "24bit")
        .unwrap_or(false)
}

pub struct LineRange {
    pub lower: usize,
    pub upper: usize,
}

impl LineRange {
    pub fn from(range_raw: &str) -> Result<LineRange> {
        LineRange::parse_range(range_raw)
    }

    pub fn new() -> LineRange {
        LineRange {
            lower: usize::min_value(),
            upper: usize::max_value(),
        }
    }

    pub fn parse_range(range_raw: &str) -> Result<LineRange> {
        let mut new_range = LineRange::new();

        if range_raw.bytes().nth(0).ok_or("Empty line range")? == b':' {
            new_range.upper = range_raw[1..].parse()?;
            return Ok(new_range);