@@ -14,9 +14,11 @@ pub struct InputFileReader<'a> {
1414}
1515
1616impl<'a> InputFileReader<'a> {
17− fn new<R: BufRead + 'a>(mut reader: R) -> InputFileReader<'a> {
17+ fn new<R: BufRead + 'a>(mut reader: R) -> Result<InputFileReader<'a>> {
1818 let mut first_line = vec![];
19− reader.read_until(b'\n', &mut first_line).ok();
19+ if reader.read_until(b'\n', &mut first_line)? == 0 {
20+ return Err(ErrorKind::ImmediateEOF.into());
21+ }
2022
2123 let content_type = content_inspector::inspect(&first_line[..]);
2224
@@ -24,11 +26,11 @@ impl<'a> InputFileReader<'a> {
2426 reader.read_until(0x00, &mut first_line).ok();
2527 }
2628
27− InputFileReader {
29+ Ok(InputFileReader {
2830 inner: Box::new(reader),
2931 first_line,
3032 content_type,
31− }
33+ })
3234 }
3335
3436 pub fn read_line(&mut self, buf: &mut Vec<u8>) -> io::Result<bool> {
@@ -57,7 +59,7 @@ pub enum InputFile<'a> {
5759impl<'a> InputFile<'a> {
5860 pub fn get_reader(&self, stdin: &'a io::Stdin) -> Result<InputFileReader> {
5961 match self {
60− InputFile::StdIn => Ok(InputFileReader::new(stdin.lock())),
62+ InputFile::StdIn => InputFileReader::new(stdin.lock()),
6163 InputFile::Ordinary(filename) => {
6264 let file = File::open(filename).map_err(|e| format!("'{}': {}", filename, e))?;
6365
@@ -65,9 +67,9 @@ impl<'a> InputFile<'a> {
6567 return Err(format!("'{}' is a directory.", filename).into());
6668 }
6769
68− Ok(InputFileReader::new(BufReader::new(file)))
70+ InputFileReader::new(BufReader::new(file))
6971 }
70− InputFile::ThemePreviewFile => Ok(InputFileReader::new(THEME_PREVIEW_FILE)),
72+ InputFile::ThemePreviewFile => InputFileReader::new(THEME_PREVIEW_FILE),
7173 }
7274 }
7375}
@@ -75,7 +77,7 @@ impl<'a> InputFile<'a> {
7577#[test]
7678fn basic() {
7779 let content = b"#!/bin/bash\necho hello";
78− let mut reader = InputFileReader::new(&content[..]);
80+ let mut reader = InputFileReader::new(&content[..]).unwrap();
7981
8082 assert_eq!(b"#!/bin/bash\n", &reader.first_line[..]);
8183
@@ -104,7 +106,7 @@ fn basic() {
104106#[test]
105107fn utf16le() {
106108 let content = b"\xFF\xFE\x73\x00\x0A\x00\x64\x00";
107− let mut reader = InputFileReader::new(&content[..]);
109+ let mut reader = InputFileReader::new(&content[..]).unwrap();
108110
109111 assert_eq!(b"\xFF\xFE\x73\x00\x0A\x00", &reader.first_line[..]);
110112