summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorsharkdp <davidpeter@web.de>2018-05-19 11:46:41 +0200
committerDavid Peter <sharkdp@users.noreply.github.com>2018-05-19 12:50:41 +0200
commit2a9f5a24edb1a24a1b17065bfaa460a297c7452f (patch)
treef4d3aa53a5be69566f17123fe948e2db1e357d99 /src
parenta0ae089c4a61bb77f1acb8daf87e5c1043b557ac (diff)
Permissive error handling, closes #17
Diffstat (limited to 'src')
-rw-r--r--src/assets.rs16
-rw-r--r--src/main.rs54
2 files changed, 46 insertions, 24 deletions
diff --git a/src/assets.rs b/src/assets.rs
index 22f870de..91139d38 100644
--- a/src/assets.rs
+++ b/src/assets.rs
@@ -128,11 +128,7 @@ impl HighlightingAssets {
})?)
}
- pub fn get_syntax(
- &self,
- language: Option<&str>,
- filename: Option<&str>,
- ) -> Result<&SyntaxDefinition> {
+ pub fn get_syntax(&self, language: Option<&str>, filename: Option<&str>) -> &SyntaxDefinition {
let syntax = match (language, filename) {
(Some(language), _) => self.syntax_set.find_syntax_by_token(language),
(None, Some(filename)) => {
@@ -142,10 +138,14 @@ impl HighlightingAssets {
// Do not peek at the file (to determine the syntax) if it is a FIFO because they can
// only be read once.
#[cfg(unix)]
- let may_read_from_file = !fs::metadata(filename)?.file_type().is_fifo();
+ let may_read_from_file = !fs::metadata(filename)
+ .map(|m| m.file_type().is_fifo())
+ .unwrap_or(false);
if may_read_from_file {
- self.syntax_set.find_syntax_for_file(filename)?
+ self.syntax_set
+ .find_syntax_for_file(filename)
+ .unwrap_or(None)
} else {
None
}
@@ -153,7 +153,7 @@ impl HighlightingAssets {
(None, None) => None,
};
- Ok(syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text()))
+ syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text())
}
}
diff --git a/src/main.rs b/src/main.rs
index 2ee8d6f0..3cb65f2d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -168,7 +168,9 @@ fn print_file(
Ok(())
}
-fn run() -> Result<()> {
+/// Returns `Err(..)` upon fatal errors. Otherwise, returns `Some(true)` on full success and
+/// `Some(false)` if any intermediate errors occured (were printed).
+fn run() -> Result<bool> {
let app = App::new();
match app.matches.subcommand() {
@@ -190,6 +192,8 @@ fn run() -> Result<()> {
} else if cache_matches.is_present("config-dir") {
println!("{}", config_dir());
}
+
+ return Ok(true);
}
_ => {
let config = app.config()?;
@@ -239,7 +243,7 @@ fn run() -> Result<()> {
println!();
}
- return Ok(());
+ return Ok(true);
}
if app.matches.is_present("list-themes") {
@@ -247,37 +251,55 @@ fn run() -> Result<()> {
for (theme, _) in themes.iter() {
println!("{}", theme);
}
- return Ok(());
+ return Ok(true);
}
let mut output_type = OutputType::from_mode(config.paging_mode);
let handle = output_type.handle()?;
let mut printer = Printer::new(handle, &config);
+ let mut no_errors: bool = true;
for file in &config.files {
printer.line_changes = file.and_then(|filename| get_git_diff(filename));
- let syntax = assets.get_syntax(config.language, *file)?;
+ let syntax = assets.get_syntax(config.language, *file);
+
+ let result = print_file(theme, &syntax, &mut printer, *file);
- print_file(theme, &syntax, &mut printer, *file)?;
+ if let Err(error) = result {
+ handle_error(&error);
+ no_errors = false;
+ }
}
+
+ Ok(no_errors)
}
}
+}
- Ok(())
+fn handle_error(error: &Error) {
+ match error {
+ &Error(ErrorKind::Io(ref io_error), _) if io_error.kind() == io::ErrorKind::BrokenPipe => {
+ process::exit(0);
+ }
+ _ => {
+ eprintln!("{}: {}", Red.paint("[bat error]"), error);
+ }
+ };
}
fn main() {
let result = run();
- if let Err(error) = result {
- match error {
- Error(ErrorKind::Io(ref io_error), _)
- if io_error.kind() == io::ErrorKind::BrokenPipe => {}
- _ => {
- eprintln!("{}: {}", Red.paint("[bat error]"), error);
-
- process::exit(1);
- }
- };
+ match result {
+ Err(error) => {
+ handle_error(&error);
+ process::exit(1);
+ }
+ Ok(false) => {
+ process::exit(1);
+ }
+ Ok(true) => {
+ process::exit(0);
+ }
}
}