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
use colored::*;
use nom::IResult;
use std::process::exit;
use std::path::PathBuf;
use regex::Regex;
use error::*;
use clap::Values;
use utils::get_processors;

/// Parse a string into a regular expression for the 'artifacts' subcommand. Adds ignores for
/// typical version control directories if none are present.
pub fn get_excludes(cli_excludes: Option<&str>) -> Regex {
    match cli_excludes {
        Some(s) => {
            let mut x = "(".to_string();
            x.push_str(s);
            x.push_str(r")|(\.git|\.pijul|_darcs|\.hg)$");
            check_regex(&x)
        }
        _ => Regex::new(r"(\.git|\.pijul|_darcs|\.hg)$").unwrap(), // ok because static
    }
}

pub fn get_depth(depth_from_cli: Option<&str>) -> u8 {
    if let Some(n) = depth_from_cli {
        if let Ok(n) = n.parse::<u8>() {
            n
        } else {
            eprintln!("{}", Internal::ParseNum);
            exit(0x0f01);
        }
    } else {
        2
    }
}

pub fn get_num(num_from_cli: Option<&str>) -> usize {
    if let Some(num) = num_from_cli {
        if let Ok(n) = num.parse::<usize>() {
            n
        } else {
            eprintln!("{}", Internal::ParseNum);
            exit(0x0f01);
        }
    } else {
        8
    }
}


/// If the user has supplied a string, parse it, otherwise, read the number of processors.
pub fn get_threads(num_from_cli: Option<&str>) -> usize {
    match num_from_cli {
        Some(num) => {
            if let Ok(n) = num.parse::<usize>() {
                n
            } else {
                eprintln!("{}", Internal::ParseNum);
                exit(0x0f01);
            }
        }
        _ => get_processors(),
    }
}

pub fn get_dirs(paths_from_cli: Option<Values>) -> Vec<PathBuf> {
    if let Some(read) = paths_from_cli {
        read.map(PathBuf::from).collect()
    } else {
        let mut v = Vec::new();
        v.push(PathBuf::from("."));
        v
    }
}

pub fn get_dir(path_from_cli: Option<&str>) -> PathBuf {
    match path_from_cli {
        Some(read) => PathBuf::from(read),
        _ => PathBuf::from("."),
    }
}

/// Parse a threshold from a command-line flag.
///
/// # Examples
///
/// ```
/// use liboskar::prelude::*;
///
/// let threshold_string = Some("31M");
/// assert_eq!(threshold(threshold_string), Some(32505856))
/// ```
pub fn threshold(s: Option<&str>) -> Option<u64> {
    s.map(pre_threshold)
}

fn pre_threshold(t_from_cli: &str) -> u64 {
    match get_threshold(t_from_cli.as_bytes()) {
        IResult::Done(_, n) => n,
        _ => {
            eprintln!(
                "{}: failed to parse threshold. defaulting to 1M",
                "Warning".yellow()
            );
            1048576 // 1 MB
        }
    }
}

fn to_u64(nums: Vec<char>, size_tag: &[u8]) -> u64 {

    let pre: String = nums.into_iter().collect();
    let n = if let Ok(n) = pre.parse::<u64>() {
        n
    } else {
        eprintln!("{}", Internal::ParseNum);
        exit(0x0f01);
    };

    match size_tag {
        b"G" | b"g" => n * 1073741824,
        b"M" | b"m" => n * 1048576,
        b"k" | b"K" => n * 1024,
        b"b" | b"B" => n,
        _ => exit(0x0f01),
    }

}

named!(digit_char<&[u8], char>,
    alt!(
        char!('1') |
        char!('2') |
        char!('3') |
        char!('4') |
        char!('5') |
        char!('6') |
        char!('7') |
        char!('8') |
        char!('9') |
        char!('0')
    )
);

named!(get_threshold<&[u8],u64>,
    do_parse!(
        nums:     many1!(digit_char) >>
        size_tag: alt!(tag!("M") |
                       tag!("G") |
                       tag!("k") | 
                       tag!("b") | 
                       tag!("B") | 
                       tag!("K") | 
                       tag!("g") | 
                       tag!("m")) >>
        (to_u64(nums, size_tag))
    )
);