on master help sign in
Move error module to separate file
commit7e0115641d2e0582d2abe93c18cec354090ea7d1 authorsharkdp <davidpeter@web.de> dateSat 21 Mar 2020, 19:35 (7 years ago) parentfedd321
3 files changed+35−32
src/assets.rs+1−1 src/errors.rs+24 src/lib.rs+10−31
src/assets.rs+1−1
@@ -192,4 +192,4 @@ impl HighlightingAssets {
192192
193193 syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text())
194194 }
195−}
195+}
src/errors.rsnew file+24
@@ -0,0 +1,24 @@
1+use error_chain::error_chain;
2+
3+error_chain! {
4+ foreign_links {
5+ Clap(::clap::Error);
6+ Io(::std::io::Error);
7+ SyntectError(::syntect::LoadingError);
8+ ParseIntError(::std::num::ParseIntError);
9+ }
10+}
11+
12+pub fn handle_error(error: &Error) {
13+ match error {
14+ Error(ErrorKind::Io(ref io_error), _)
15+ if io_error.kind() == ::std::io::ErrorKind::BrokenPipe =>
16+ {
17+ ::std::process::exit(0);
18+ }
19+ _ => {
20+ use ansi_term::Colour::Red;
21+ eprintln!("{}: {}", Red.paint("[bat error]"), error);
22+ }
23+ };
24+}
src/lib.rs+10−31
@@ -1,9 +1,6 @@
11// `error_chain!` can recurse deeply
22#![recursion_limit = "1024"]
33
4−#[macro_use]
5−extern crate error_chain;
6−
74extern crate ansi_term;
85extern crate atty;
96extern crate console;
@@ -19,6 +16,7 @@ pub mod assets;
1916pub mod controller;
2017mod decorations;
2118mod diff;
19+pub mod errors;
2220pub mod inputfile;
2321mod less;
2422pub mod line_range;
@@ -29,31 +27,6 @@ pub mod style;
2927pub mod syntax_mapping;
3028mod terminal;
3129
32−pub mod errors {
33− error_chain! {
34− foreign_links {
35− Clap(::clap::Error);
36− Io(::std::io::Error);
37− SyntectError(::syntect::LoadingError);
38− ParseIntError(::std::num::ParseIntError);
39− }
40− }
41−
42− pub fn handle_error(error: &Error) {
43− match error {
44− Error(ErrorKind::Io(ref io_error), _)
45− if io_error.kind() == ::std::io::ErrorKind::BrokenPipe =>
46− {
47− ::std::process::exit(0);
48− }
49− _ => {
50− use ansi_term::Colour::Red;
51− eprintln!("{}: {}", Red.paint("[bat error]"), error);
52− }
53− };
54− }
55−}
56−
5730#[derive(Debug, Clone, Copy, PartialEq)]
5831pub enum PagingMode {
5932 Always,
@@ -68,7 +41,7 @@ impl Default for PagingMode {
6841}
6942
7043use inputfile::InputFile;
71−use line_range::{LineRanges, HighlightedLineRanges};
44+use line_range::{HighlightedLineRanges, LineRanges};
7245use style::{OutputComponents, OutputWrap};
7346use syntax_mapping::SyntaxMapping;
7447
@@ -131,12 +104,18 @@ pub struct Config<'a> {
131104fn default_config_should_include_all_lines() {
132105 use line_range::RangeCheckResult;
133106
134− assert_eq!(Config::default().line_ranges.check(17), RangeCheckResult::InRange);
107+ assert_eq!(
108+ Config::default().line_ranges.check(17),
109+ RangeCheckResult::InRange
110+ );
135111}
136112
137113#[test]
138114fn default_config_should_highlight_no_lines() {
139115 use line_range::RangeCheckResult;
140116
141− assert_ne!(Config::default().highlighted_lines.0.check(17), RangeCheckResult::InRange);
117+ assert_ne!(
118+ Config::default().highlighted_lines.0.check(17),
119+ RangeCheckResult::InRange
120+ );
142121}