File tree Expand file tree Collapse file tree 2 files changed +25
-4
lines changed
Expand file tree Collapse file tree 2 files changed +25
-4
lines changed Original file line number Diff line number Diff line change 1- use anyhow:: Result ;
1+ use anyhow:: { anyhow , Result } ;
22use clap:: Parser ;
33use mdr:: metadata:: Meta ;
4- use std:: path :: PathBuf ;
4+ use std:: { fs , io } ;
55
66// --------------------------------------------------
77#[ derive( Parser , Debug ) ]
88pub struct Args {
9+ /// Input filename or "-" for STDIN
910 #[ arg( ) ]
10- pub filename : PathBuf ,
11+ pub filename : String ,
1112}
1213
1314// --------------------------------------------------
@@ -20,7 +21,23 @@ fn main() {
2021
2122// --------------------------------------------------
2223fn run ( args : Args ) -> Result < ( ) > {
23- let meta = Meta :: from_file ( & args. filename ) ?;
24+ let contents = open ( & args. filename ) ?;
25+ let meta = Meta :: from_string ( & contents) ?;
2426 println ! ( "{}" , meta. check( ) . join( "\n " ) ) ;
2527 Ok ( ( ) )
2628}
29+
30+ // --------------------------------------------------
31+ fn open ( filename : & str ) -> Result < String > {
32+ match filename {
33+ "-" => {
34+ let mut contents = vec ! [ ] ;
35+ for line in io:: stdin ( ) . lines ( ) {
36+ contents. push ( line. unwrap ( ) ) ;
37+ }
38+ Ok ( contents. join ( "\n " ) )
39+ }
40+ _ => fs:: read_to_string ( filename)
41+ . map_err ( |e| anyhow ! ( format!( "{filename}: {e}" ) ) ) ,
42+ }
43+ }
Original file line number Diff line number Diff line change @@ -119,6 +119,10 @@ impl Meta {
119119 . map_err ( |e| anyhow ! ( r#"Failed to parse "{}": {e}"# , path. display( ) ) )
120120 }
121121
122+ pub fn from_string ( contents : & str ) -> Result < Self > {
123+ toml:: from_str ( & contents) . map_err ( |e| anyhow ! ( r#"Failed to parse input: {e}"# ) )
124+ }
125+
122126 pub fn check ( & self ) -> Vec < String > {
123127 let mut messages = vec ! [ ] ;
124128 if let Err ( e) = self . validate ( ) {
You can’t perform that action at this time.
0 commit comments