Skip to content

Commit ecdec70

Browse files
authored
Merge pull request #279 from dtolnay/stdin
Accept `-` to mean stdin in command line code generator
2 parents 16ab146 + c0e07dc commit ecdec70

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

gen/src/fs.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use std::error::Error as StdError;
44
use std::fmt::{self, Display};
5-
use std::io;
5+
use std::io::{self, Read};
66
use std::path::{Path, PathBuf};
77

88
pub(crate) type Result<T> = std::result::Result<T, Error>;
@@ -66,6 +66,14 @@ pub(crate) fn read(path: impl AsRef<Path>) -> Result<Vec<u8>> {
6666
}
6767
}
6868

69+
pub(crate) fn read_stdin() -> Result<Vec<u8>> {
70+
let mut bytes = Vec::new();
71+
match io::stdin().read_to_end(&mut bytes) {
72+
Ok(_len) => Ok(bytes),
73+
Err(e) => err!(e, "Failed to read input from stdin"),
74+
}
75+
}
76+
6977
pub(crate) fn remove_file(path: impl AsRef<Path>) -> Result<()> {
7078
let path = path.as_ref();
7179
match std::fs::remove_file(path) {

gen/src/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ pub(super) fn generate_from_path(path: &Path, opt: &Opt) -> GeneratedCode {
8080
}
8181

8282
fn read_to_string(path: &Path) -> Result<String> {
83-
let bytes = fs::read(path)?;
83+
let bytes = if path == Path::new("-") {
84+
fs::read_stdin()
85+
} else {
86+
fs::read(path)
87+
}?;
8488
match String::from_utf8(bytes) {
8589
Ok(string) => Ok(string),
8690
Err(err) => Err(Error::Utf8(path.to_owned(), err.utf8_error())),

0 commit comments

Comments
 (0)