Skip to content

Commit 868082d

Browse files
committed
allow input of "-" to read from STDIN
1 parent fd5c027 commit 868082d

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/bin/mdr-toml.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
use anyhow::Result;
1+
use anyhow::{anyhow, Result};
22
use clap::Parser;
33
use mdr::metadata::Meta;
4-
use std::path::PathBuf;
4+
use std::{fs, io};
55

66
// --------------------------------------------------
77
#[derive(Parser, Debug)]
88
pub 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
// --------------------------------------------------
2223
fn 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+
}

src/metadata.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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() {

0 commit comments

Comments
 (0)