Skip to content

Commit

Permalink
Check result of read_until, and return Error if 0, which indicates EO…
Browse files Browse the repository at this point in the history
…F was found before delimeter.
  • Loading branch information
reidwagner committed Feb 5, 2019
1 parent 82f048a commit 9d4fb22
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ impl<'b> Controller<'b> {
match input_file.get_reader(&stdin) {
Err(error) => {
handle_error(&error);
no_errors = false;
match error {
Error(ErrorKind::ImmediateEOF, _) => (),
_ => no_errors = false
}
}
Ok(mut reader) => {
let result = if self.config.loop_through {
Expand Down
20 changes: 11 additions & 9 deletions src/inputfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@ pub struct InputFileReader<'a> {
}

impl<'a> InputFileReader<'a> {
fn new<R: BufRead + 'a>(mut reader: R) -> InputFileReader<'a> {
fn new<R: BufRead + 'a>(mut reader: R) -> Result<InputFileReader<'a>> {
let mut first_line = vec![];
reader.read_until(b'\n', &mut first_line).ok();
if reader.read_until(b'\n', &mut first_line)? == 0 {
return Err(ErrorKind::ImmediateEOF.into())
}

let content_type = content_inspector::inspect(&first_line[..]);

if content_type == ContentType::UTF_16LE {
reader.read_until(0x00, &mut first_line).ok();
}

InputFileReader {
Ok(InputFileReader {
inner: Box::new(reader),
first_line,
content_type,
}
})
}

pub fn read_line(&mut self, buf: &mut Vec<u8>) -> io::Result<bool> {
Expand Down Expand Up @@ -57,25 +59,25 @@ pub enum InputFile<'a> {
impl<'a> InputFile<'a> {
pub fn get_reader(&self, stdin: &'a io::Stdin) -> Result<InputFileReader> {
match self {
InputFile::StdIn => Ok(InputFileReader::new(stdin.lock())),
InputFile::StdIn => InputFileReader::new(stdin.lock()),
InputFile::Ordinary(filename) => {
let file = File::open(filename).map_err(|e| format!("'{}': {}", filename, e))?;

if file.metadata()?.is_dir() {
return Err(format!("'{}' is a directory.", filename).into());
}

Ok(InputFileReader::new(BufReader::new(file)))
InputFileReader::new(BufReader::new(file))
}
InputFile::ThemePreviewFile => Ok(InputFileReader::new(THEME_PREVIEW_FILE)),
InputFile::ThemePreviewFile => InputFileReader::new(THEME_PREVIEW_FILE),
}
}
}

#[test]
fn basic() {
let content = b"#!/bin/bash\necho hello";
let mut reader = InputFileReader::new(&content[..]);
let mut reader = InputFileReader::new(&content[..]).unwrap();

assert_eq!(b"#!/bin/bash\n", &reader.first_line[..]);

Expand Down Expand Up @@ -104,7 +106,7 @@ fn basic() {
#[test]
fn utf16le() {
let content = b"\xFF\xFE\x73\x00\x0A\x00\x64\x00";
let mut reader = InputFileReader::new(&content[..]);
let mut reader = InputFileReader::new(&content[..]).unwrap();

assert_eq!(b"\xFF\xFE\x73\x00\x0A\x00", &reader.first_line[..]);

Expand Down
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ mod errors {
SyntectError(::syntect::LoadingError);
ParseIntError(::std::num::ParseIntError);
}
errors {
ImmediateEOF
}
}

pub fn handle_error(error: &Error) {
Expand All @@ -72,6 +75,7 @@ mod errors {
{
super::process::exit(0);
}
&Error(ErrorKind::ImmediateEOF, _) => { () }
_ => {
use ansi_term::Colour::Red;
eprintln!("{}: {}", Red.paint("[bat error]"), error);
Expand Down

0 comments on commit 9d4fb22

Please sign in to comment.