From af825556362c477a75b25c5b8f2224b37e29a96c Mon Sep 17 00:00:00 2001 From: jcamiel Date: Fri, 17 Feb 2023 14:16:10 +0100 Subject: [PATCH] Clean ouput module interfaces. --- packages/hurl/src/cli/mod.rs | 8 +++++++- packages/hurl/src/output/json.rs | 9 ++++----- packages/hurl/src/output/mod.rs | 27 +++++++++++++++++++++------ packages/hurl/src/output/raw.rs | 11 +++++------ packages/hurl/src/output/stdout.rs | 10 +++++----- packages/hurl/src/report/mod.rs | 11 ++++++++++- 6 files changed, 52 insertions(+), 24 deletions(-) diff --git a/packages/hurl/src/cli/mod.rs b/packages/hurl/src/cli/mod.rs index 91834213b..576ab326d 100644 --- a/packages/hurl/src/cli/mod.rs +++ b/packages/hurl/src/cli/mod.rs @@ -16,7 +16,7 @@ * */ -use crate::report; +use crate::{output, report}; use std::error::Error; use std::fmt; @@ -67,6 +67,12 @@ impl From for CliError { } } +impl From for CliError { + fn from(e: output::Error) -> Self { + Self { message: e.message } + } +} + impl fmt::Display for CliError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.message) diff --git a/packages/hurl/src/output/json.rs b/packages/hurl/src/output/json.rs index bd8034b81..4bf2473e6 100644 --- a/packages/hurl/src/output/json.rs +++ b/packages/hurl/src/output/json.rs @@ -15,9 +15,8 @@ * limitations under the License. * */ - -use crate::cli::CliError; -use crate::output::write_output; +use crate::output; +use crate::output::Error; use crate::runner::HurlResult; /// Writes the `hurl_result` JSON representation to the file `filename`. @@ -29,9 +28,9 @@ pub fn write_json( hurl_result: &HurlResult, content: &str, filename: &Option, -) -> Result<(), CliError> { +) -> Result<(), Error> { let json_result = hurl_result.to_json(content); let serialized = serde_json::to_string(&json_result).unwrap(); let s = format!("{serialized}\n"); - write_output(&s.into_bytes(), filename) + output::write_output(&s.into_bytes(), filename) } diff --git a/packages/hurl/src/output/mod.rs b/packages/hurl/src/output/mod.rs index a71709cac..99fbe6bca 100644 --- a/packages/hurl/src/output/mod.rs +++ b/packages/hurl/src/output/mod.rs @@ -15,27 +15,42 @@ * limitations under the License. * */ - +//! Serialize a Hurl run result to a file. +//! +//! Therea are two supported serialisation: +//! - JSON: the whole run is serialized to JSON (like the [HAR](https://en.wikipedia.org/wiki/HAR_(file_format)) format) +//! - raw: the last response of a run is serialized to a file. The body can be automatically uncompress +//! or written as it. mod json; mod raw; mod stdout; pub use self::json::write_json; pub use self::raw::write_body; -use crate::cli::CliError; -use crate::output::stdout::write_stdout; +use std::fmt; use std::io::Write; use std::path::Path; +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Error { + pub message: String, +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.message) + } +} + /// Writes `bytes` to the file `filename` or stdout by default. -fn write_output(bytes: &Vec, filename: &Option) -> Result<(), CliError> { +fn write_output(bytes: &Vec, filename: &Option) -> Result<(), Error> { match filename { - None => write_stdout(bytes.as_slice()), + None => stdout::write_stdout(bytes.as_slice()), Some(filename) => { let path = Path::new(filename.as_str()); let mut file = match std::fs::File::create(path) { Err(why) => { - return Err(CliError { + return Err(Error { message: format!("Issue writing to {}: {:?}", path.display(), why), }); } diff --git a/packages/hurl/src/output/raw.rs b/packages/hurl/src/output/raw.rs index 52e499b74..370eb78f2 100644 --- a/packages/hurl/src/output/raw.rs +++ b/packages/hurl/src/output/raw.rs @@ -15,9 +15,8 @@ * limitations under the License. * */ -use crate::cli::CliError; use crate::http::Response; -use crate::output::write_output; +use crate::output; use crate::runner; use crate::runner::{HurlResult, RunnerError}; use crate::util::logger::Logger; @@ -35,7 +34,7 @@ pub fn write_body( color: bool, filename: &Option, logger: &Logger, -) -> Result<(), CliError> { +) -> Result<(), output::Error> { // By default, we output the body response bytes of the last entry if let Some(entry_result) = hurl_result.entries.last() { if let Some(call) = entry_result.calls.last() { @@ -61,16 +60,16 @@ pub fn write_body( assert: false, }; let message = error.fixme(); - return Err(CliError { message }); + return Err(output::Error { message }); } } } else { response.body.clone() }; output.append(&mut body); - let result = write_output(&output, filename); + let result = output::write_output(&output, filename); if result.is_err() { - return Err(CliError { + return Err(output::Error { message: "Undefined error".to_string(), }); } diff --git a/packages/hurl/src/output/stdout.rs b/packages/hurl/src/output/stdout.rs index b38d46e28..afed0205a 100644 --- a/packages/hurl/src/output/stdout.rs +++ b/packages/hurl/src/output/stdout.rs @@ -15,30 +15,30 @@ * limitations under the License. * */ -use crate::cli::CliError; +use crate::output::Error; #[cfg(target_family = "windows")] use atty::Stream; use std::io; use std::io::Write; #[cfg(target_family = "unix")] -pub(crate) fn write_stdout(buf: &[u8]) -> Result<(), CliError> { +pub(crate) fn write_stdout(buf: &[u8]) -> Result<(), Error> { let stdout = io::stdout(); let mut handle = stdout.lock(); - handle.write_all(buf).map_err(|_| CliError { + handle.write_all(buf).map_err(|_| Error { message: "Error writing output".to_string(), }) } #[cfg(target_family = "windows")] -pub(crate) fn write_stdout(buf: &[u8]) -> Result<(), CliError> { +pub(crate) fn write_stdout(buf: &[u8]) -> Result<(), Error> { if atty::is(Stream::Stdout) { println!("{}", String::from_utf8_lossy(buf)); Ok(()) } else { let stdout = io::stdout(); let mut handle = stdout.lock(); - handle.write_all(buf).map_err(|_| CliError { + handle.write_all(buf).map_err(|_| Error { message: "Error writing output".to_string(), }) } diff --git a/packages/hurl/src/report/mod.rs b/packages/hurl/src/report/mod.rs index 59e53d237..59d8a81b6 100644 --- a/packages/hurl/src/report/mod.rs +++ b/packages/hurl/src/report/mod.rs @@ -16,7 +16,10 @@ * */ -//! Various reports for Hurl runs (JUnit, HTML etc...) +//! Various reports for Hurl runs (JUnit, HTML etc...) A report aggregates multiple runs into +//! a single unit. + +use std::fmt; pub mod html; pub mod junit; @@ -25,3 +28,9 @@ pub mod junit; pub struct Error { pub message: String, } + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.message) + } +}