File tree Expand file tree Collapse file tree 2 files changed +14
-2
lines changed
Expand file tree Collapse file tree 2 files changed +14
-2
lines changed Original file line number Diff line number Diff line change 22
33use std:: error:: Error as StdError ;
44use std:: fmt:: { self , Display } ;
5- use std:: io;
5+ use std:: io:: { self , Read } ;
66use std:: path:: { Path , PathBuf } ;
77
88pub ( 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+
6977pub ( crate ) fn remove_file ( path : impl AsRef < Path > ) -> Result < ( ) > {
7078 let path = path. as_ref ( ) ;
7179 match std:: fs:: remove_file ( path) {
Original file line number Diff line number Diff line change @@ -80,7 +80,11 @@ pub(super) fn generate_from_path(path: &Path, opt: &Opt) -> GeneratedCode {
8080}
8181
8282fn 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 ( ) ) ) ,
You can’t perform that action at this time.
0 commit comments