Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

Commit

Permalink
[cli] add codespan_reporting to glsl error
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobhellermann committed Jun 18, 2021
1 parent 794e9fd commit 1338361
Showing 1 changed file with 47 additions and 5 deletions.
52 changes: 47 additions & 5 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ trait PrettyResult {
}

fn print_err(error: impl Error) {
eprintln!("{}:", error);
eprint!("{}", error);

let mut e = error.source();
if e.is_some() {
eprintln!(": ");
} else {
eprintln!();
}

while let Some(source) = e {
eprintln!("\t{}", source);
e = source.source();
Expand Down Expand Up @@ -92,7 +99,7 @@ fn main() {
}

let input_path = match input_path {
Some(ref string) => string,
Some(ref string) => Path::new(string),
None => {
println!("Call with <input> <output> [<options>]");
return;
Expand Down Expand Up @@ -135,7 +142,11 @@ fn main() {
defines: Default::default(),
},
)
.unwrap_pretty()
.unwrap_or_else(|err| {
let filename = input_path.file_name().and_then(std::ffi::OsStr::to_str);
emit_glsl_parser_error(err, filename.unwrap_or("glsl"), &input);
std::process::exit(1);
})
}
"frag" => {
let input = fs::read_to_string(input_path).unwrap();
Expand All @@ -148,7 +159,11 @@ fn main() {
defines: Default::default(),
},
)
.unwrap_pretty()
.unwrap_or_else(|err| {
let filename = input_path.file_name().and_then(std::ffi::OsStr::to_str);
emit_glsl_parser_error(err, filename.unwrap_or("glsl"), &input);
std::process::exit(1);
})
}
"comp" => {
let input = fs::read_to_string(input_path).unwrap();
Expand All @@ -161,7 +176,11 @@ fn main() {
defines: Default::default(),
},
)
.unwrap_pretty()
.unwrap_or_else(|err| {
let filename = input_path.file_name().and_then(std::ffi::OsStr::to_str);
emit_glsl_parser_error(err, filename.unwrap_or("glsl"), &input);
std::process::exit(1);
})
}
other => panic!("Unknown input extension: {}", other),
};
Expand Down Expand Up @@ -274,3 +293,26 @@ fn main() {
}
}
}

use codespan_reporting::{
diagnostic::{Diagnostic, Label},
files::SimpleFile,
term::{
self,
termcolor::{ColorChoice, StandardStream},
},
};

pub fn emit_glsl_parser_error(err: naga::front::glsl::ParseError, filename: &str, source: &str) {
let diagnostic = match err.kind.metadata() {
Some(metadata) => Diagnostic::error()
.with_message(err.kind.to_string())
.with_labels(vec![Label::primary((), metadata.start..metadata.end)]),
None => Diagnostic::error().with_message(err.kind.to_string()),
};

let files = SimpleFile::new(filename, source);
let config = codespan_reporting::term::Config::default();
let writer = StandardStream::stderr(ColorChoice::Auto);
term::emit(&mut writer.lock(), &config, &files, &diagnostic).expect("cannot write error");
}

0 comments on commit 1338361

Please sign in to comment.