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