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
//! Struct to hold the various options. Those are fed by the user through [clap].

use crate::score::Score;
use clap::{value_t, ArgMatches};

/// Options related to the presentation and filtering.
/// See also [CliOptions].
#[derive(Debug)]
pub struct PresentationOptions {
    pub debug: bool,
    pub count: bool,
    pub line_numbers: bool,
    pub inverted: bool,
    pub threshold: Score, // TODO: probably should be renamed into distance
    pub headers: bool,
}

/// Options related to the indexing.
/// See also [CliOptions].
#[derive(Debug)]
pub struct IndexingOptions {
    pub case_sensitive: bool,
    pub ignore_spaces: bool,
    pub scope: usize,
    pub full: bool,
}

/// Options for the CLI. The options are split into categories.
/// See [IndexingOptions] and [PresentationOptions].
#[derive(Debug)]
pub struct CliOptions {
    pub indexing: IndexingOptions,
    pub presentation: PresentationOptions,
}

impl CliOptions {
    pub fn new(matches: &ArgMatches, buffer_length: usize) -> CliOptions {
        let scope = if matches.is_present("full") {
            buffer_length
        } else {
            value_t!(matches.value_of("scope"), usize).unwrap_or(3)
        };
        
        CliOptions {
            indexing: IndexingOptions {
                case_sensitive: matches.is_present("case-sensitive"),
                ignore_spaces: matches.is_present("ignore-spaces"),
                scope,
                full: matches.is_present("full"),
            },
            presentation: PresentationOptions {
                inverted: matches.is_present("invert"),
                threshold: if matches.is_present("strict") { 0 } else { value_t!(matches.value_of("distance"), u8).unwrap_or(3)},
                line_numbers: matches.is_present("line-numbers"),
                count: matches.is_present("count") && !matches.is_present("invert"),
                debug: matches.is_present("debug"),
                headers: matches.is_present("headers"),
            },
            ..Default::default()
        }
    }
}

impl Default for CliOptions {
    fn default() -> Self {
        CliOptions {
            indexing: IndexingOptions::default(),
            presentation: PresentationOptions::default(),
        }
    }
}

impl Default for PresentationOptions {
    fn default() -> Self {
        PresentationOptions {
            inverted: false,
            threshold: 3,
            line_numbers: false,
            count: false,
            debug: false,
            headers: true,
        }
    }
}

impl Default for IndexingOptions {
    fn default() -> Self {
        IndexingOptions {
            case_sensitive: false,
            ignore_spaces: false,
            scope: 10,
            full: false,
        }
    }
}

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

    #[test]
    fn test_default() {
        let default_options: CliOptions = Default::default();
        assert_eq!(default_options.indexing.scope, 10);
        assert_eq!(default_options.presentation.inverted, false);
        assert_eq!(default_options.presentation.threshold, 3);
    }
}