diff --git a/RELEASES.md b/RELEASES.md index 517c82d0..c014d5e6 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -16,6 +16,7 @@ Enhancements * Source information is provided when there is an error within a call to a function defined in another module. * Source information is provided to the `NativeCallContext` for native Rust functions. +* `EvalAltResult::clear_position` to clear the position information of an error - useful when only the message is needed and the position doesn't need to be printed out. Version 0.19.9 diff --git a/src/bin/rhai-repl.rs b/src/bin/rhai-repl.rs index d5d5c406..67f39957 100644 --- a/src/bin/rhai-repl.rs +++ b/src/bin/rhai-repl.rs @@ -11,9 +11,10 @@ use std::{ }; /// Pretty-print error. -fn print_error(input: &str, err: EvalAltResult) { +fn print_error(input: &str, mut err: EvalAltResult) { let lines: Vec<_> = input.trim().split('\n').collect(); let pos = err.position(); + err.clear_position(); let line_no = if lines.len() > 1 { if pos.is_none() { @@ -26,8 +27,6 @@ fn print_error(input: &str, err: EvalAltResult) { }; // Print error position - let pos_text = format!(" ({})", pos); - if pos.is_none() { // No position println!("{}", err); @@ -40,7 +39,7 @@ fn print_error(input: &str, err: EvalAltResult) { "{0:>1$} {2}", "^", line_no.len() + pos.position().unwrap(), - err.to_string().replace(&pos_text, "") + err ); } } diff --git a/src/bin/rhai-run.rs b/src/bin/rhai-run.rs index 4453f84b..24e60452 100644 --- a/src/bin/rhai-run.rs +++ b/src/bin/rhai-run.rs @@ -5,19 +5,17 @@ use rhai::OptimizationLevel; use std::{env, fs::File, io::Read, process::exit}; -fn eprint_error(input: &str, err: EvalAltResult) { - fn eprint_line(lines: &[&str], pos: Position, err: &str) { +fn eprint_error(input: &str, mut err: EvalAltResult) { + fn eprint_line(lines: &[&str], pos: Position, err_msg: &str) { let line = pos.line().unwrap(); - let line_no = format!("{}: ", line); - let pos_text = format!(" ({})", pos); eprintln!("{}{}", line_no, lines[line - 1]); eprintln!( "{:>1$} {2}", "^", line_no.len() + pos.position().unwrap(), - err.replace(&pos_text, "") + err_msg ); eprintln!(""); } @@ -26,6 +24,7 @@ fn eprint_error(input: &str, err: EvalAltResult) { // Print error let pos = err.position(); + err.clear_position(); if pos.is_none() { // No position diff --git a/src/result.rs b/src/result.rs index 0face85e..d0ca7e52 100644 --- a/src/result.rs +++ b/src/result.rs @@ -366,6 +366,11 @@ impl EvalAltResult { | Self::Return(_, pos) => *pos, } } + /// Clear the [position][Position] information of this error. + pub fn clear_position(&mut self) -> &mut Self { + self.set_position(Position::NONE); + self + } /// Override the [position][Position] of this error. pub fn set_position(&mut self, new_position: Position) { match self {