Accept `-` to mean stdin in command line code generator
diff --git a/gen/src/fs.rs b/gen/src/fs.rs
index 89965ee..8f94f00 100644
--- a/gen/src/fs.rs
+++ b/gen/src/fs.rs
@@ -2,7 +2,7 @@
use std::error::Error as StdError;
use std::fmt::{self, Display};
-use std::io;
+use std::io::{self, Read};
use std::path::{Path, PathBuf};
pub(crate) type Result<T> = std::result::Result<T, Error>;
@@ -66,6 +66,14 @@
}
}
+pub(crate) fn read_stdin() -> Result<Vec<u8>> {
+ let mut bytes = Vec::new();
+ match io::stdin().read_to_end(&mut bytes) {
+ Ok(_len) => Ok(bytes),
+ Err(e) => err!(e, "Failed to read input from stdin"),
+ }
+}
+
pub(crate) fn remove_file(path: impl AsRef<Path>) -> Result<()> {
let path = path.as_ref();
match std::fs::remove_file(path) {
diff --git a/gen/src/mod.rs b/gen/src/mod.rs
index f4d643d..9578fe8 100644
--- a/gen/src/mod.rs
+++ b/gen/src/mod.rs
@@ -80,7 +80,11 @@
}
fn read_to_string(path: &Path) -> Result<String> {
- let bytes = fs::read(path)?;
+ let bytes = if path == Path::new("-") {
+ fs::read_stdin()
+ } else {
+ fs::read(path)
+ }?;
match String::from_utf8(bytes) {
Ok(string) => Ok(string),
Err(err) => Err(Error::Utf8(path.to_owned(), err.utf8_error())),