diff options
author | sharkdp <davidpeter@web.de> | 2020-04-18 11:37:12 +0200 |
---|---|---|
committer | David Peter <sharkdp@users.noreply.github.com> | 2020-04-21 08:57:15 +0200 |
commit | 1b8ce60054306b6ab1e7ec50938557cef9be54f6 (patch) | |
tree | b8706990ad30772b56d3c89c084c1368117b5d2a | |
parent | d5a31dc2ecfcb6304eacd1fbe7672ac822831c57 (diff) |
Pass stdin as a generic BufRead, fix stdin tests
-rw-r--r-- | src/assets.rs | 17 | ||||
-rw-r--r-- | src/controller.rs | 4 | ||||
-rw-r--r-- | src/inputfile.rs | 4 |
3 files changed, 17 insertions, 8 deletions
diff --git a/src/assets.rs b/src/assets.rs index 2dfcebfb..ccac5029 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -271,7 +271,7 @@ mod tests { let syntax = self.assets.get_syntax( None, input_file, - &mut input_file.get_reader(&io::stdin()).unwrap(), + &mut input_file.get_reader(io::stdin().lock()).unwrap(), &self.syntax_mapping, ); @@ -281,6 +281,17 @@ mod tests { fn syntax_for_file(&self, file_name: &str) -> String { self.syntax_for_file_with_content(file_name, "") } + + fn syntax_for_stdin_with_content(&self, file_name: &str, content: &[u8]) -> String { + let input_file = InputFile::StdIn(Some(OsStr::new(file_name))); + let syntax = self.assets.get_syntax( + None, + input_file, + &mut input_file.get_reader(content).unwrap(), + &self.syntax_mapping, + ); + syntax.name.clone() + } } #[test] @@ -353,10 +364,10 @@ mod tests { let test = SyntaxDetectionTest::new(); // from file extension - assert_eq!(test.syntax_for_file_with_content("test.cpp", ""), "C++"); + assert_eq!(test.syntax_for_stdin_with_content("test.cpp", b"a"), "C++"); // from first line (fallback) assert_eq!( - test.syntax_for_file_with_content("my_script", "#!/bin/bash"), + test.syntax_for_stdin_with_content("my_script", b"#!/bin/bash"), "Bourne Again Shell (bash)" ); } diff --git a/src/controller.rs b/src/controller.rs index 8c0b96e6..11d4d3a3 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -56,10 +56,8 @@ impl<'b> Controller<'b> { let writer = output_type.handle()?; let mut no_errors: bool = true; - let stdin = io::stdin(); - for input_file in self.config.files.iter() { - match input_file.get_reader(&stdin) { + match input_file.get_reader(io::stdin().lock()) { Err(error) => { handle_error(&error); no_errors = false; diff --git a/src/inputfile.rs b/src/inputfile.rs index d7dd731b..737d3ea3 100644 --- a/src/inputfile.rs +++ b/src/inputfile.rs @@ -86,9 +86,9 @@ pub enum InputFile<'a> { } impl<'a> InputFile<'a> { - pub(crate) fn get_reader(&self, stdin: &'a io::Stdin) -> Result<InputFileReader> { + pub(crate) fn get_reader<R: BufRead + 'a>(&self, stdin: R) -> Result<InputFileReader> { match self { - InputFile::StdIn(_) => Ok(InputFileReader::new(stdin.lock())), + InputFile::StdIn(_) => Ok(InputFileReader::new(stdin)), InputFile::Ordinary(ofile) => { let file = File::open(ofile.path) .map_err(|e| format!("'{}': {}", ofile.path.to_string_lossy(), e))?; |