2018-10-07 09:54:01 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{self, BufRead, BufReader};
|
|
|
|
|
|
|
|
use errors::*;
|
|
|
|
|
|
|
|
const THEME_PREVIEW_FILE: &[u8] = include_bytes!("../assets/theme_preview.rs");
|
|
|
|
|
2018-10-07 10:15:49 +00:00
|
|
|
pub struct InputFileReader<'a> {
|
|
|
|
inner: Box<dyn BufRead + 'a>,
|
2018-10-07 11:25:49 +00:00
|
|
|
buffer: Vec<u8>,
|
2018-10-07 10:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> InputFileReader<'a> {
|
|
|
|
fn new<R: BufRead + 'a>(reader: R) -> InputFileReader<'a> {
|
|
|
|
InputFileReader {
|
|
|
|
inner: Box::new(reader),
|
2018-10-07 11:25:49 +00:00
|
|
|
buffer: vec![],
|
2018-10-07 10:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-07 11:25:49 +00:00
|
|
|
pub fn get_first_line(&mut self) -> io::Result<Vec<u8>> {
|
|
|
|
assert!(self.buffer.is_empty());
|
|
|
|
|
|
|
|
self.inner.read_until(b'\n', &mut self.buffer)?;
|
|
|
|
Ok(self.buffer.clone())
|
|
|
|
}
|
|
|
|
|
2018-10-07 10:15:49 +00:00
|
|
|
pub fn read_line(&mut self, buf: &mut Vec<u8>) -> io::Result<bool> {
|
2018-10-07 11:25:49 +00:00
|
|
|
if self.buffer.is_empty() {
|
|
|
|
self.inner.read_until(b'\n', buf).map(|size| size > 0)
|
|
|
|
} else {
|
|
|
|
buf.append(&mut self.buffer);
|
|
|
|
return Ok(true);
|
|
|
|
}
|
2018-10-07 10:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-07 09:21:41 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
pub enum InputFile<'a> {
|
|
|
|
StdIn,
|
|
|
|
Ordinary(&'a str),
|
|
|
|
ThemePreviewFile,
|
|
|
|
}
|
2018-10-07 09:54:01 +00:00
|
|
|
|
|
|
|
impl<'a> InputFile<'a> {
|
2018-10-07 10:15:49 +00:00
|
|
|
pub fn get_reader(&self, stdin: &'a io::Stdin) -> Result<InputFileReader> {
|
2018-10-07 09:54:01 +00:00
|
|
|
match self {
|
2018-10-07 10:15:49 +00:00
|
|
|
InputFile::StdIn => Ok(InputFileReader::new(stdin.lock())),
|
2018-10-07 09:54:01 +00:00
|
|
|
InputFile::Ordinary(filename) => {
|
|
|
|
let file = File::open(filename)?;
|
|
|
|
|
|
|
|
if file.metadata()?.is_dir() {
|
|
|
|
return Err(format!("'{}' is a directory.", filename).into());
|
|
|
|
}
|
|
|
|
|
2018-10-07 10:15:49 +00:00
|
|
|
Ok(InputFileReader::new(BufReader::new(file)))
|
2018-10-07 09:54:01 +00:00
|
|
|
}
|
2018-10-07 10:15:49 +00:00
|
|
|
InputFile::ThemePreviewFile => Ok(InputFileReader::new(THEME_PREVIEW_FILE)),
|
2018-10-07 09:54:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-07 10:29:38 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic() {
|
2018-10-07 11:25:49 +00:00
|
|
|
let content = b"#!/bin/bash\necho hello";
|
2018-10-07 10:29:38 +00:00
|
|
|
let mut reader = InputFileReader::new(&content[..]);
|
|
|
|
|
2018-10-07 11:25:49 +00:00
|
|
|
let first_line = reader.get_first_line();
|
|
|
|
assert!(first_line.is_ok());
|
|
|
|
assert_eq!(b"#!/bin/bash\n", &first_line.unwrap()[..]);
|
|
|
|
|
2018-10-07 10:29:38 +00:00
|
|
|
let mut buffer = vec![];
|
|
|
|
|
|
|
|
let res = reader.read_line(&mut buffer);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(true, res.unwrap());
|
2018-10-07 11:25:49 +00:00
|
|
|
assert_eq!(b"#!/bin/bash\n", &buffer[..]);
|
2018-10-07 10:29:38 +00:00
|
|
|
|
|
|
|
buffer.clear();
|
|
|
|
|
|
|
|
let res = reader.read_line(&mut buffer);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(true, res.unwrap());
|
2018-10-07 11:25:49 +00:00
|
|
|
assert_eq!(b"echo hello", &buffer[..]);
|
2018-10-07 10:29:38 +00:00
|
|
|
|
|
|
|
buffer.clear();
|
|
|
|
|
|
|
|
let res = reader.read_line(&mut buffer);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(false, res.unwrap());
|
|
|
|
assert!(buffer.is_empty());
|
|
|
|
}
|