bat/src/inputfile.rs

163 lines
4.3 KiB
Rust
Raw Normal View History

2020-02-28 10:27:06 +01:00
use std::ffi::OsStr;
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};
2019-03-08 11:48:22 +01:00
use crate::errors::*;
2018-10-07 11:54:01 +02:00
const THEME_PREVIEW_FILE: &[u8] = include_bytes!("../assets/theme_preview.rs");
pub struct InputFileReader<'a> {
inner: Box<dyn BufRead + 'a>,
2020-03-21 20:45:46 +01:00
pub(crate) first_line: Vec<u8>,
pub(crate) content_type: Option<ContentType>,
}
impl<'a> InputFileReader<'a> {
fn new<R: BufRead + 'a>(mut reader: R) -> InputFileReader<'a> {
2018-10-07 13:47:54 +02:00
let mut first_line = vec![];
reader.read_until(b'\n', &mut first_line).ok();
2018-10-07 13:47:54 +02:00
let content_type = if first_line.is_empty() {
None
} else {
Some(content_inspector::inspect(&first_line[..]))
};
2018-10-07 16:44:59 +02:00
if content_type == Some(ContentType::UTF_16LE) {
2018-10-07 16:44:59 +02:00
reader.read_until(0x00, &mut first_line).ok();
}
InputFileReader {
inner: Box::new(reader),
2018-10-07 13:47:54 +02:00
first_line,
2018-10-07 16:44:59 +02:00
content_type,
}
}
2020-03-21 20:45:46 +01:00
pub(crate) 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 == Some(ContentType::UTF_16LE) {
2018-10-07 16:44:59 +02:00
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
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OrdinaryFile<'a> {
2020-04-11 19:17:36 +02:00
path: &'a OsStr,
user_provided_path: Option<&'a OsStr>,
}
impl<'a> OrdinaryFile<'a> {
2020-04-11 19:17:36 +02:00
pub fn from_path(path: &'a OsStr) -> OrdinaryFile<'a> {
OrdinaryFile {
2020-04-11 19:17:36 +02:00
path,
user_provided_path: None,
2020-04-11 19:17:36 +02:00
}
}
pub fn set_provided_path(&mut self, user_provided_path: &'a OsStr) {
self.user_provided_path = Some(user_provided_path);
}
pub(crate) fn provided_path(&self) -> &'a OsStr {
self.user_provided_path.unwrap_or_else(|| self.path)
}
}
2018-10-07 11:21:41 +02:00
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum InputFile<'a> {
StdIn(Option<&'a OsStr>),
Ordinary(OrdinaryFile<'a>),
2018-10-07 11:21:41 +02:00
ThemePreviewFile,
}
2018-10-07 11:54:01 +02:00
impl<'a> InputFile<'a> {
pub(crate) fn get_reader<R: BufRead + 'a>(&self, stdin: R) -> Result<InputFileReader> {
2018-10-07 11:54:01 +02:00
match self {
InputFile::StdIn(_) => Ok(InputFileReader::new(stdin)),
InputFile::Ordinary(ofile) => {
2020-04-11 19:17:36 +02:00
let file = File::open(ofile.path)
.map_err(|e| format!("'{}': {}", ofile.path.to_string_lossy(), e))?;
2018-10-07 11:54:01 +02:00
if file.metadata()?.is_dir() {
return Err(
2020-04-11 19:17:36 +02:00
format!("'{}' is a directory.", ofile.path.to_string_lossy()).into(),
);
2018-10-07 11:54:01 +02:00
}
Ok(InputFileReader::new(BufReader::new(file)))
2018-10-07 11:54:01 +02:00
}
InputFile::ThemePreviewFile => Ok(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";
let mut reader = InputFileReader::new(&content[..]);
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";
let mut reader = InputFileReader::new(&content[..]);
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());
}