From 85bb81b728fdb99987ce7fce0c7e7ba5f5c64928 Mon Sep 17 00:00:00 2001 From: Fabrice Reix Date: Sat, 16 Oct 2021 11:10:20 +0200 Subject: [PATCH] Update API for error_string --- packages/hurl/src/cli/logger.rs | 39 +++++++++++++++++---------------- packages/hurl/src/cli/mod.rs | 4 ++-- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/packages/hurl/src/cli/logger.rs b/packages/hurl/src/cli/logger.rs index 351a998bbf4..1111b6fb4f4 100644 --- a/packages/hurl/src/cli/logger.rs +++ b/packages/hurl/src/cli/logger.rs @@ -90,11 +90,16 @@ fn log_error( error_type.red().to_string() }; - let error_message = error_string(lines, filename, error); + let filename = if let Some(filename) = filename { + filename + } else { + "".to_string() + }; + let error_message = error_string(&lines, filename, error); eprintln!("{}: {}\n", error_type, error_message); } -pub fn error_string(lines: Vec, filename: Option, error: &dyn Error) -> String { +pub fn error_string(lines: &[String], filename: String, error: &dyn Error) -> String { let line_number_size = if lines.len() < 100 { 2 } else if lines.len() < 1000 { @@ -103,17 +108,13 @@ pub fn error_string(lines: Vec, filename: Option, error: &dyn Er 4 }; - let file_info = if let Some(filename) = filename { - format!( - "{}--> {}:{}:{}", - " ".repeat(line_number_size).as_str(), - filename, - error.source_info().start.line, - error.source_info().start.column, - ) - } else { - "".to_string() - }; + let file_info = format!( + "{}--> {}:{}:{}", + " ".repeat(line_number_size).as_str(), + filename, + error.source_info().start.line, + error.source_info().start.column, + ); let line = lines.get(error.source_info().start.line - 1).unwrap(); let line = str::replace(line, "\t", " "); // replace all your tabs with 4 characters @@ -197,7 +198,7 @@ pub mod tests { "HTTP/1.0 200".to_string(), "".to_string(), ]; - let filename = Some("test.hurl".to_string()); + let filename = "test.hurl".to_string(); let error = runner::Error { source_info: SourceInfo::init(2, 10, 2, 13), inner: runner::RunnerError::AssertStatus { @@ -206,7 +207,7 @@ pub mod tests { assert: true, }; assert_eq!( - error_string(lines, filename, &error), + error_string(&lines, filename, &error), r#"Assert Status --> test.hurl:2:10 | @@ -224,14 +225,14 @@ pub mod tests { "[Asserts]".to_string(), r#"xpath "strong(//head/title)" equals "Hello""#.to_string(), ]; - let filename = Some("test.hurl".to_string()); + let filename = "test.hurl".to_string(); let error = runner::Error { source_info: SourceInfo::init(4, 7, 4, 29), inner: runner::RunnerError::QueryInvalidXpathEval {}, assert: true, }; assert_eq!( - error_string(lines, filename, &error), + error_string(&lines, filename, &error), r#"Invalid xpath expression --> test.hurl:4:7 | @@ -249,7 +250,7 @@ pub mod tests { "[Asserts]".to_string(), r#"jsonpath "$.count" >= 5"#.to_string(), ]; - let filename = Some("test.hurl".to_string()); + let filename = "test.hurl".to_string(); let error = runner::Error { source_info: SourceInfo::init(4, 0, 4, 0), inner: runner::RunnerError::AssertFailure { @@ -260,7 +261,7 @@ pub mod tests { assert: true, }; assert_eq!( - error_string(lines, filename, &error), + error_string(&lines, filename, &error), r#"Assert Failure --> test.hurl:4:0 | diff --git a/packages/hurl/src/cli/mod.rs b/packages/hurl/src/cli/mod.rs index 6db8cd386f0..d5dfd3de7ae 100644 --- a/packages/hurl/src/cli/mod.rs +++ b/packages/hurl/src/cli/mod.rs @@ -18,8 +18,8 @@ pub use self::fs::read_to_string; pub use self::logger::{ - log_info, make_logger_error_message, make_logger_parser_error, make_logger_runner_error, - make_logger_verbose, + error_string, log_info, make_logger_error_message, make_logger_parser_error, + make_logger_runner_error, make_logger_verbose, }; pub use self::options::app; pub use self::options::output_color;