Skip to content

Commit dd13fbb

Browse files
committed
Handle - as filename indicating to read from stdin
1 parent 4e4f773 commit dd13fbb

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

src/formatting/formatter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ impl<'i> Formatter<'i> {
623623
}
624624

625625
// This is a helper for rendering a single descriptives in error messages.
626-
// The real method is append_decriptives() below; this method simply
626+
// The real method is append_descriptives() below; this method simply
627627
// creates a single element slice that can be passed to it.
628628
fn append_descriptive(&mut self, descriptive: &'i Descriptive) {
629629
use std::slice;

src/parsing/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! parser for the Technique language
22
3+
use std::io::Read;
34
use std::path::Path;
45
use tracing::debug;
56

@@ -15,6 +16,21 @@ pub use parser::{parse_with_recovery, Parser, ParsingError};
1516
/// main function so that the Technique object created by parse() below can
1617
/// have the same lifetime.
1718
pub fn load(filename: &Path) -> Result<String, LoadingError<'_>> {
19+
if filename.to_str() == Some("-") {
20+
let mut buffer = String::new();
21+
match std::io::stdin().read_to_string(&mut buffer) {
22+
Ok(_) => return Ok(buffer),
23+
Err(error) => {
24+
debug!(?error);
25+
return Err(LoadingError {
26+
problem: "Failed reading from stdin".to_string(),
27+
details: error.to_string(),
28+
filename,
29+
});
30+
}
31+
}
32+
}
33+
1834
match std::fs::read_to_string(filename) {
1935
Ok(content) => Ok(content),
2036
Err(error) => {

src/problem/format.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub fn full_parsing_error<'i>(
1111
renderer: &impl Render,
1212
) -> String {
1313
let (problem, details) = generate_error_message(error, renderer);
14+
let input = generate_filename(filename);
1415
let offset = error.offset();
1516
let width = error.width();
1617

@@ -44,7 +45,7 @@ pub fn full_parsing_error<'i>(
4445
{}
4546
"#,
4647
"error".bright_red(),
47-
filename.to_string_lossy(),
48+
input,
4849
line,
4950
column,
5051
problem.bold(),
@@ -71,6 +72,7 @@ pub fn concise_parsing_error<'i>(
7172
renderer: &impl Render,
7273
) -> String {
7374
let (problem, _) = generate_error_message(error, renderer);
75+
let input = generate_filename(filename);
7476
let offset = error.offset();
7577
let i = calculate_line_number(source, offset);
7678
let j = calculate_column_number(source, offset);
@@ -80,7 +82,7 @@ pub fn concise_parsing_error<'i>(
8082
format!(
8183
"{}: {}:{}:{} {}",
8284
"error".bright_red(),
83-
filename.to_string_lossy(),
85+
input,
8486
line,
8587
column,
8688
problem.bold(),
@@ -101,6 +103,16 @@ pub fn concise_loading_error<'i>(error: &LoadingError<'i>) -> String {
101103
)
102104
}
103105

106+
fn generate_filename(filename: &Path) -> String {
107+
if filename.to_str() == Some("-") {
108+
"<stdin>".to_string()
109+
} else {
110+
filename
111+
.display()
112+
.to_string()
113+
}
114+
}
115+
104116
// Helper functions for line/column calculation
105117
pub fn calculate_line_number(content: &str, offset: usize) -> usize {
106118
content[..offset]

0 commit comments

Comments
 (0)