Use io::stdin() to read from standard input in a portable manner

This commit is contained in:
Olivier Tilloy
2024-04-04 22:14:03 +02:00
parent 6dc34fed44
commit 84ad116845
+7 -3
View File
@@ -7,7 +7,7 @@ use crate::params::{parse_params, Format};
use std::env;
use std::ffi::OsString;
use std::fs;
use std::io::{self, Write};
use std::io::{self, Read, Write};
use std::process::{exit, ExitCode};
mod context_diff;
@@ -46,8 +46,12 @@ fn main() -> ExitCode {
}
// read files
fn read_file_contents(filepath: &OsString) -> io::Result<Vec<u8>> {
let stdin = OsString::from("/dev/stdin");
fs::read(if filepath == "-" { &stdin } else { filepath })
if filepath == "-" {
let mut content = Vec::new();
io::stdin().read_to_end(&mut content).and(Ok(content))
} else {
fs::read(filepath)
}
}
let from_content = match read_file_contents(&params.from) {
Ok(from_content) => from_content,