prettier printing for errors regarding gleam.toml
@@ -126,13 +126,42 @@ fn command_build(root: String) {
}
fn read_project_config(root: &str) -> Result<ProjectConfig, ()> {
+ fn die(message: String) -> ! {
+ use termcolor::{StandardStream, Color, ColorSpec, ColorChoice, WriteColor};
+ let mut stderr = StandardStream::stderr(ColorChoice::Always);
+
+ // Write "error: " in red
+ stderr.set_color(
+ ColorSpec::new()
+ .set_fg(Some(Color::Red))
+ .set_intense(true)
+ .set_bold(true)
+ ).unwrap();
+ write!(&mut stderr, "error: ").unwrap();
+
+ // Write error message in white
+ stderr.reset().unwrap();
+ writeln!(&mut stderr, "{}", message).unwrap();
+
+ std::process::exit(1);
+ }
+
use std::io::Read;
- let mut toml = String::new();
let config_path = PathBuf::from(root).join("gleam.toml");
- let mut file = File::open(config_path).expect("Unable to open gleam.toml");
- file.read_to_string(&mut toml)
- .expect("Unable to read gleam.toml");
- Ok(toml::from_str(&toml).expect("Unable to parse gleam.toml"))
+ let mut file = File::open(config_path).unwrap_or_else(
+ |e| die(format!("could not open gleam.toml: {}", e.to_string()))
+ );
+
+ let mut toml = String::new();
+ file.read_to_string(&mut toml).unwrap_or_else(
+ |e| die(format!("could not read gleam.toml: {}", e.to_string()))
+ );
+
+ let project_config = toml::from_str(&toml).unwrap_or_else(
+ |e| die(format!("could not parse gleam.toml: {}", e.to_string()))
+ );
+
+ Ok(project_config)
}
fn collect_source(src_dir: PathBuf, origin: ModuleOrigin, srcs: &mut Vec<crate::project::Input>) {
