diff options
author | Ben Bridle <bridle.benjamin@gmail.com> | 2024-10-30 16:29:36 +1300 |
---|---|---|
committer | Ben Bridle <bridle.benjamin@gmail.com> | 2024-10-30 16:29:36 +1300 |
commit | 02deef3511e8613a5d737d5586f1efd9fd01637c (patch) | |
tree | b0e1b68a721825472f6e713bb83651d304af08b3 | |
parent | ec8ed73838d6850c98722bc68b484174b735abfd (diff) | |
download | bedrock-pc-02deef3511e8613a5d737d5586f1efd9fd01637c.zip |
Change error message format
Removed trailing periods, removed "exiting" from error messages, and
made error details print not in bold.
-rw-r--r-- | src/bin/br.rs | 65 |
1 files changed, 35 insertions, 30 deletions
diff --git a/src/bin/br.rs b/src/bin/br.rs index d9d792f..f472840 100644 --- a/src/bin/br.rs +++ b/src/bin/br.rs @@ -4,6 +4,7 @@ use phosphor::*; use std::io::{Read, Write}; use std::path::{Path, PathBuf}; +use std::process::exit; const NORMAL: &str = "\x1b[0m"; @@ -23,7 +24,7 @@ macro_rules! verbose { macro_rules! error { ($($tokens:tt)*) => {{ eprint!("{BOLD}{RED}[ERROR]: {WHITE}"); eprint!($($tokens)*); - eprintln!("{NORMAL}"); std::process::exit(1); + eprintln!("{NORMAL}"); }}; } @@ -134,8 +135,8 @@ fn load_bytecode(path: Option<&Path>) -> Bytecode { verbose!("Loaded program from {path:?} ({length} bytes)"); return bytecode; } else { - eprintln!("Could not read program from {path:?}, exiting"); - std::process::exit(1); + error!("Could not read program from {path:?}, exiting"); + exit(1); } } else { verbose!("Reading program from standard input..."); @@ -144,8 +145,8 @@ fn load_bytecode(path: Option<&Path>) -> Bytecode { verbose!("Loaded program from standard input ({length} bytes)"); return bytecode; } else { - eprintln!("Could not read program from standard input, exiting"); - std::process::exit(1); + error!("Could not read program from standard input, exiting"); + exit(1); } } } @@ -215,28 +216,31 @@ fn main_asm(args: Asm) { let mut resolver = if let Some(path) = &source_path { match SourceUnit::from_path(&path, &ext) { Ok(source_unit) => SymbolResolver::from_source_unit(source_unit), - Err(err) => match err { - ParseError::InvalidExtension => error!( - "File {path:?} has invalid extension, must be '.{ext}'"), - ParseError::NotFound => error!( - "File {path:?} was not found"), - ParseError::InvalidUtf8 => error!( - "File {path:?} does not contain valid UTF-8 text"), - ParseError::NotReadable => error!( - "File {path:?} is not readable"), - ParseError::IsADirectory => error!( - "File {path:?} is a directory"), - ParseError::Unknown => error!( - "Unknown error while attempting to read from {path:?}") + Err(err) => { + match err { + ParseError::InvalidExtension => error!( + "File {path:?} has invalid extension, must be '.{ext}'"), + ParseError::NotFound => error!( + "File {path:?} was not found"), + ParseError::InvalidUtf8 => error!( + "File {path:?} does not contain valid UTF-8 text"), + ParseError::NotReadable => error!( + "File {path:?} is not readable"), + ParseError::IsADirectory => error!( + "File {path:?} is a directory"), + ParseError::Unknown => error!( + "Unknown error while attempting to read from {path:?}") + }; + exit(1); } } } else { let mut source_code = String::new(); verbose!("Reading program source from standard input"); if let Err(err) = std::io::stdin().read_to_string(&mut source_code) { - eprintln!("Could not read from standard input, exiting."); - eprintln!("({err:?})"); - std::process::exit(1); + error!("Could not read from standard input"); + eprintln!("{err:?}"); + exit(1); } let path = "<standard input>"; let source_unit = SourceUnit::from_source_code(source_code, path); @@ -294,7 +298,8 @@ fn main_asm(args: Asm) { symbols_path.set_extension("br.sym"); let symbols = generate_symbols_file(&semantic_tokens); if let Err(err) = std::fs::write(&symbols_path, symbols) { - verbose!("Could not write to symbols path {symbols_path:?}: ({err:?})."); + verbose!("Could not write to symbols path {symbols_path:?}"); + eprintln!("{err:?}"); } else { verbose!("Saved debug symbols to {symbols_path:?}"); } @@ -303,7 +308,7 @@ fn main_asm(args: Asm) { let length = bytecode.len(); let percentage = (length as f32 / 65536.0 * 100.0).round() as u16; - verbose!("Assembled program in {length} bytes ({percentage}% of maximum)."); + verbose!("Assembled program in {length} bytes ({percentage}% of maximum)"); if !args.check { write_bytes_and_exit(&bytecode, args.output.as_ref()); @@ -313,18 +318,18 @@ fn main_asm(args: Asm) { fn write_bytes_and_exit<P: AsRef<Path>>(bytes: &[u8], path: Option<&P>) -> ! { if let Some(path) = path { if let Err(err) = std::fs::write(path, bytes) { - eprintln!("Could not write to path {:?}, exiting.", path.as_ref()); - eprintln!("({err:?})"); - std::process::exit(1); + error!("Could not write to path {:?}", path.as_ref()); + eprintln!("{err:?}"); + exit(1); } } else { if let Err(err) = std::io::stdout().write_all(bytes) { - eprintln!("Could not write to standard output, exiting."); - eprintln!("({err:?})"); - std::process::exit(1); + error!("Could not write to standard output"); + eprintln!("{err:?}"); + exit(1); } } - std::process::exit(0); + exit(0); } |