use std::ffi::{OsStr, OsString}; use std::fs::File; use std::io::{self, BufRead, BufReader, Read}; use content_inspector::{self, ContentType}; use crate::errors::*; const THEME_PREVIEW_FILE: &[u8] = include_bytes!("../assets/theme_preview.rs"); #[derive(Debug, Clone, PartialEq)] pub struct OrdinaryFile { path: OsString, user_provided_path: Option, } impl OrdinaryFile { pub fn from_path(path: &OsStr) -> OrdinaryFile { OrdinaryFile { path: path.to_os_string(), user_provided_path: None, } } pub fn set_provided_path(&mut self, user_provided_path: &OsStr) { self.user_provided_path = Some(user_provided_path.to_os_string()); } pub(crate) fn provided_path<'a>(&'a self) -> &'a OsStr { self.user_provided_path .as_ref() .unwrap_or_else(|| &self.path) } } #[derive(Debug, Clone)] pub struct InputDescription { pub full: String, pub prefix: String, pub name: String, } pub enum Input { StdIn(Option), Ordinary(OrdinaryFile), FromReader(Box, Option), ThemePreviewFile, } impl Input { pub(crate) fn get_reader<'a, R: BufRead + 'a>(&self, stdin: R) -> Result> { match self { Input::StdIn(_) => Ok(InputReader::new(stdin)), Input::Ordinary(ofile) => { let file = File::open(&ofile.path) .map_err(|e| format!("'{}': {}", ofile.path.to_string_lossy(), e))?; if file.metadata()?.is_dir() { return Err( format!("'{}' is a directory.", ofile.path.to_string_lossy()).into(), ); } Ok(InputReader::new(BufReader::new(file))) } Input::ThemePreviewFile => Ok(InputReader::new(THEME_PREVIEW_FILE)), Input::FromReader(_, _) => unimplemented!(), //Ok(InputReader::new(BufReader::new(reader))), } } pub(crate) fn description(&self) -> InputDescription { match self { Input::Ordinary(ofile) => InputDescription { full: format!("file '{}'", &ofile.provided_path().to_string_lossy()), prefix: "File: ".to_owned(), name: ofile.provided_path().to_string_lossy().into_owned(), }, Input::StdIn(Some(name)) => InputDescription { full: format!( "STDIN (with name '{}')", name.to_string_lossy().into_owned() ), prefix: "File: ".to_owned(), name: name.to_string_lossy().into_owned(), }, Input::StdIn(None) => InputDescription { full: "STDIN".to_owned(), prefix: "".to_owned(), name: "STDIN".to_owned(), }, Input::ThemePreviewFile => InputDescription { full: "".to_owned(), prefix: "".to_owned(), name: "".to_owned(), }, Input::FromReader(_, Some(name)) => InputDescription { full: format!("file '{}'", name.to_string_lossy()), prefix: "File: ".to_owned(), name: name.to_string_lossy().into_owned(), }, Input::FromReader(_, None) => InputDescription { full: "reader".to_owned(), prefix: "".to_owned(), name: "READER".into(), }, } } } pub struct InputReader<'a> { inner: Box, pub(crate) first_line: Vec, pub(crate) content_type: Option, } impl<'a> InputReader<'a> { fn new(mut reader: R) -> InputReader<'a> { let mut first_line = vec![]; reader.read_until(b'\n', &mut first_line).ok(); let content_type = if first_line.is_empty() { None } else { Some(content_inspector::inspect(&first_line[..])) }; if content_type == Some(ContentType::UTF_16LE) { reader.read_until(0x00, &mut first_line).ok(); } InputReader { inner: Box::new(reader), first_line, content_type, } } pub(crate) fn read_line(&mut self, buf: &mut Vec) -> io::Result { if self.first_line.is_empty() { let res = self.inner.read_until(b'\n', buf).map(|size| size > 0)?; if self.content_type == Some(ContentType::UTF_16LE) { self.inner.read_until(0x00, buf).ok(); } Ok(res) } else { buf.append(&mut self.first_line); Ok(true) } } } #[test] fn basic() { let content = b"#!/bin/bash\necho hello"; let mut reader = InputReader::new(&content[..]); assert_eq!(b"#!/bin/bash\n", &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"#!/bin/bash\n", &buffer[..]); buffer.clear(); let res = reader.read_line(&mut buffer); assert!(res.is_ok()); assert_eq!(true, res.unwrap()); assert_eq!(b"echo hello", &buffer[..]); buffer.clear(); let res = reader.read_line(&mut buffer); assert!(res.is_ok()); assert_eq!(false, res.unwrap()); assert!(buffer.is_empty()); } #[test] fn utf16le() { let content = b"\xFF\xFE\x73\x00\x0A\x00\x64\x00"; let mut reader = InputReader::new(&content[..]); 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()); }