Move error module to separate file

This commit is contained in:
sharkdp 2020-03-21 19:35:04 +01:00 committed by David Peter
parent fedd32173e
commit 7e0115641d
3 changed files with 35 additions and 32 deletions

View File

@ -192,4 +192,4 @@ impl HighlightingAssets {
syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text())
}
}
}

24
src/errors.rs Normal file
View File

@ -0,0 +1,24 @@
use error_chain::error_chain;
error_chain! {
foreign_links {
Clap(::clap::Error);
Io(::std::io::Error);
SyntectError(::syntect::LoadingError);
ParseIntError(::std::num::ParseIntError);
}
}
pub fn handle_error(error: &Error) {
match error {
Error(ErrorKind::Io(ref io_error), _)
if io_error.kind() == ::std::io::ErrorKind::BrokenPipe =>
{
::std::process::exit(0);
}
_ => {
use ansi_term::Colour::Red;
eprintln!("{}: {}", Red.paint("[bat error]"), error);
}
};
}

View File

@ -1,9 +1,6 @@
// `error_chain!` can recurse deeply
#![recursion_limit = "1024"]
#[macro_use]
extern crate error_chain;
extern crate ansi_term;
extern crate atty;
extern crate console;
@ -19,6 +16,7 @@ pub mod assets;
pub mod controller;
mod decorations;
mod diff;
pub mod errors;
pub mod inputfile;
mod less;
pub mod line_range;
@ -29,31 +27,6 @@ pub mod style;
pub mod syntax_mapping;
mod terminal;
pub mod errors {
error_chain! {
foreign_links {
Clap(::clap::Error);
Io(::std::io::Error);
SyntectError(::syntect::LoadingError);
ParseIntError(::std::num::ParseIntError);
}
}
pub fn handle_error(error: &Error) {
match error {
Error(ErrorKind::Io(ref io_error), _)
if io_error.kind() == ::std::io::ErrorKind::BrokenPipe =>
{
::std::process::exit(0);
}
_ => {
use ansi_term::Colour::Red;
eprintln!("{}: {}", Red.paint("[bat error]"), error);
}
};
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PagingMode {
Always,
@ -68,7 +41,7 @@ impl Default for PagingMode {
}
use inputfile::InputFile;
use line_range::{LineRanges, HighlightedLineRanges};
use line_range::{HighlightedLineRanges, LineRanges};
use style::{OutputComponents, OutputWrap};
use syntax_mapping::SyntaxMapping;
@ -131,12 +104,18 @@ pub struct Config<'a> {
fn default_config_should_include_all_lines() {
use line_range::RangeCheckResult;
assert_eq!(Config::default().line_ranges.check(17), RangeCheckResult::InRange);
assert_eq!(
Config::default().line_ranges.check(17),
RangeCheckResult::InRange
);
}
#[test]
fn default_config_should_highlight_no_lines() {
use line_range::RangeCheckResult;
assert_ne!(Config::default().highlighted_lines.0.check(17), RangeCheckResult::InRange);
assert_ne!(
Config::default().highlighted_lines.0.check(17),
RangeCheckResult::InRange
);
}