-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.rs
100 lines (82 loc) · 2.71 KB
/
main.rs
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
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate serde_derive;
use crate::args::Args;
use docopt::Docopt;
use lines::{IntoMaybeUt8Lines, MaybeUtf8LinesError};
use miette::IntoDiagnostic;
use std::io::{self};
const VERSION: &str = env!("CARGO_PKG_VERSION");
const USAGE: &str = include_str!("USAGE.txt");
mod args;
mod file;
mod file_ext;
mod lines;
mod devicon_lookup;
use devicon_lookup::*;
struct Cli {
args: Args,
}
impl Cli {
fn process_stdin(&self) {
let stdin = io::stdin();
let maybe_regex_closure = self
.args
.flag_regex
.clone()
.map(|string_regex| {
regex::Regex::new(&string_regex)
.unwrap_or_else(|e| panic!("The provided regex could not be parsed: {}", e))
})
.map(devicon_lookup::parsers::regex::parser_from_regex);
let maybe_prefix_closure =
self.args.flag_prefix.clone().map(|prefix| {
devicon_lookup::parsers::prefix::parser_from_prefix_delimiter(prefix)
});
for line_result in stdin.lock().into_maybe_utf8_lines() {
match line_result {
// IO Succeeded AND UTF8 Decoded
Ok(line) => {
let line: Line = {
let mut line_builder = LineBuilder::new(line);
if self.args.flag_color {
line_builder.with_parser(&devicon_lookup::parsers::color::strip_color);
};
if let Some(prefix_closure) = &maybe_prefix_closure {
line_builder.with_parser(prefix_closure);
};
if let Some(regex_closure) = &maybe_regex_closure {
line_builder.with_parser(regex_closure);
};
line_builder.build()
};
match line.parse() {
Ok(p) => p.print_with_symbol(&self.args),
Err(e) => panic!("{}", e),
};
}
Err(MaybeUtf8LinesError::Utf8Error(e)) => write_to_stdout(e.as_bytes()),
Err(e) => panic!("{}", e),
}
}
}
fn run(&self) {
if self.args.flag_version {
println!("Dev Icon Lookup v{}", VERSION);
} else {
self.process_stdin();
}
}
}
fn main() -> miette::Result<()> {
let args = Docopt::new(USAGE).into_diagnostic()?.deserialize();
match args {
Ok(args) => {
let cli: Cli = Cli { args };
cli.run();
Ok(())
}
Err(e) => e.exit(),
}
}