liboskar/
error.rs

1use colored::*;
2use regex::Regex;
3use std::fmt;
4use std::process;
5
6#[derive(Debug)]
7pub enum Internal {
8    ParseNum,
9    ParseIgnore,
10    GetPath,
11    DirPermissions,
12    NotDirectory,
13    PathDoesNotExist,
14    IoError,
15}
16
17impl fmt::Display for Internal {
18    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
19        match *self {
20            Internal::IoError => write!(
21                f,
22                "{}: IO error when trying to read directory contents",
23                "Error".red()
24            ),
25            Internal::ParseNum => write!(
26                f,
27                "{}: Please enter a positive whole number.",
28                "Error".red()
29            ),
30            _ => write!(f, "other error"),
31        }
32    }
33}
34
35/// Check that the user-supplied regex is valid. If not, fail gracefully.
36pub fn check_regex(re: &str) -> Regex {
37    if let Ok(r) = Regex::new(re) {
38        r
39    } else if let Err(x) = Regex::new(re) {
40        eprintln!("{}: Invalid regex:\n    {}", "Error".red(), x);
41        process::exit(0x0f01)
42    } else {
43        process::exit(0x0f01)
44    }
45}