diff --git a/src/assets.rs b/src/assets.rs index 36436f29..4bf8f6a3 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -180,10 +180,8 @@ impl HighlightingAssets { .find_syntax_by_extension(file_name) .or_else(|| self.syntax_set.find_syntax_by_extension(extension)); let line_syntax = if ext_syntax.is_none() { - reader - .get_first_line() + String::from_utf8(reader.first_line.clone()) .ok() - .and_then(|v| String::from_utf8(v).ok()) .and_then(|l| self.syntax_set.find_syntax_by_first_line(&l)) } else { None diff --git a/src/inputfile.rs b/src/inputfile.rs index feec9857..47b47f5e 100644 --- a/src/inputfile.rs +++ b/src/inputfile.rs @@ -7,29 +7,25 @@ const THEME_PREVIEW_FILE: &[u8] = include_bytes!("../assets/theme_preview.rs"); pub struct InputFileReader<'a> { inner: Box, - buffer: Vec, + pub first_line: Vec, } impl<'a> InputFileReader<'a> { - fn new(reader: R) -> InputFileReader<'a> { + fn new(mut reader: R) -> InputFileReader<'a> { + let mut first_line = vec![]; + reader.read_until(b'\n', &mut first_line).ok(); + InputFileReader { inner: Box::new(reader), - buffer: vec![], + first_line, } } - pub fn get_first_line(&mut self) -> io::Result> { - assert!(self.buffer.is_empty()); - - self.inner.read_until(b'\n', &mut self.buffer)?; - Ok(self.buffer.clone()) - } - pub fn read_line(&mut self, buf: &mut Vec) -> io::Result { - if self.buffer.is_empty() { + if self.first_line.is_empty() { self.inner.read_until(b'\n', buf).map(|size| size > 0) } else { - buf.append(&mut self.buffer); + buf.append(&mut self.first_line); return Ok(true); } } @@ -65,9 +61,7 @@ fn basic() { let content = b"#!/bin/bash\necho hello"; let mut reader = InputFileReader::new(&content[..]); - let first_line = reader.get_first_line(); - assert!(first_line.is_ok()); - assert_eq!(b"#!/bin/bash\n", &first_line.unwrap()[..]); + assert_eq!(b"#!/bin/bash\n", &reader.first_line[..]); let mut buffer = vec![];