Change CliError from struct to enum (and specify variant).

This commit is contained in:
jcamiel 2024-03-06 17:42:12 +01:00
parent 27ce455005
commit 9b2601032a
No known key found for this signature in database
GPG Key ID: 07FF11CFD55356CC
4 changed files with 39 additions and 77 deletions

View File

@ -1,5 +1,5 @@
# This Hurl file begins with a BOM marker.
# You can check it with 'hexdump -c bom.hurl'
# You can check it with 'hexdump -C bom.hurl'
GET http://localhost:8000/utf8_bom
HTTP 200

View File

@ -15,54 +15,42 @@
* limitations under the License.
*
*/
use std::error::Error;
use std::fmt;
use hurl::{output, report};
use hurl::report;
use std::string::FromUtf8Error;
use std::{fmt, io};
#[allow(unused)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CliError {
pub message: String,
pub enum CliError {
IO(String),
Parsing,
Runtime(String),
}
impl From<Box<dyn Error>> for CliError {
fn from(e: Box<dyn Error>) -> Self {
Self {
message: format!("{e:?}"),
}
impl From<io::Error> for CliError {
fn from(error: io::Error) -> Self {
CliError::IO(error.to_string())
}
}
impl From<&str> for CliError {
fn from(e: &str) -> Self {
Self {
message: e.to_string(),
}
}
}
impl From<String> for CliError {
fn from(e: String) -> Self {
Self { message: e }
impl From<FromUtf8Error> for CliError {
fn from(error: FromUtf8Error) -> Self {
CliError::IO(error.to_string())
}
}
impl From<report::Error> for CliError {
fn from(e: report::Error) -> Self {
Self { message: e.message }
}
}
impl From<output::Error> for CliError {
fn from(e: output::Error) -> Self {
Self {
message: e.to_string(),
}
fn from(error: report::Error) -> Self {
CliError::IO(error.message)
}
}
impl fmt::Display for CliError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.message)
match self {
CliError::IO(message) => write!(f, "{}", message),
CliError::Parsing => Ok(()),
CliError::Runtime(message) => write!(f, "{}", message),
}
}
}

View File

@ -15,12 +15,11 @@
* limitations under the License.
*
*/
use crate::cli::CliError;
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use crate::cli::CliError;
/// Remove BOM from the input bytes
fn strip_bom(bytes: &mut Vec<u8>) {
if bytes.starts_with(&[0xefu8, 0xbb, 0xbf]) {
@ -34,42 +33,21 @@ fn strip_bom(bytes: &mut Vec<u8>) {
pub fn read_to_string(filename: &str) -> Result<String, CliError> {
if filename == "-" {
let mut contents = String::new();
return if let Err(e) = std::io::stdin().read_to_string(&mut contents) {
Err(CliError {
message: format!("Input stream can not be read - {e}"),
})
} else {
return Ok(contents);
};
std::io::stdin().read_to_string(&mut contents)?;
return Ok(contents);
}
let mut f = match File::open(filename) {
Ok(f) => f,
Err(e) => {
return Err(CliError {
message: e.to_string(),
})
}
};
let metadata = fs::metadata(filename).expect("unable to read metadata");
let mut f = File::open(filename)?;
let metadata = fs::metadata(filename).unwrap();
let mut buffer = vec![0; metadata.len() as usize];
if let Err(e) = f.read(&mut buffer) {
return Err(CliError {
message: e.to_string(),
});
}
f.read_exact(&mut buffer)?;
string_from_utf8(buffer)
}
pub fn string_from_utf8(buffer: Vec<u8>) -> Result<String, CliError> {
fn string_from_utf8(buffer: Vec<u8>) -> Result<String, CliError> {
let mut buffer = buffer;
strip_bom(&mut buffer);
match String::from_utf8(buffer) {
Ok(s) => Ok(s),
Err(e) => Err(CliError {
message: e.to_string(),
}),
}
let s = String::from_utf8(buffer)?;
Ok(s)
}
#[cfg(test)]
@ -106,9 +84,7 @@ pub mod tests {
);
assert_eq!(
string_from_utf8(vec![0xef]).err().unwrap(),
CliError {
message: "incomplete utf-8 byte sequence from index 0".to_string()
}
CliError::IO("incomplete utf-8 byte sequence from index 0".to_string()),
);
}
}

View File

@ -264,9 +264,9 @@ fn exit_code(runs: &[HurlRun]) -> i32 {
fn create_cookies_file(runs: &[HurlRun], filename: &str) -> Result<(), cli::CliError> {
let mut file = match std::fs::File::create(filename) {
Err(why) => {
return Err(cli::CliError {
message: format!("Issue writing to {filename}: {why:?}"),
});
return Err(cli::CliError::IO(format!(
"Issue writing to {filename}: {why:?}"
)));
}
Ok(file) => file,
};
@ -277,9 +277,7 @@ fn create_cookies_file(runs: &[HurlRun], filename: &str) -> Result<(), cli::CliE
.to_string();
match runs.first() {
None => {
return Err(cli::CliError {
message: "Issue fetching results".to_string(),
});
return Err(cli::CliError::IO("Issue fetching results".to_string()));
}
Some(run) => {
for cookie in run.hurl_result.cookies.iter() {
@ -290,9 +288,9 @@ fn create_cookies_file(runs: &[HurlRun], filename: &str) -> Result<(), cli::CliE
}
if let Err(why) = file.write_all(s.as_bytes()) {
return Err(cli::CliError {
message: format!("Issue writing to {filename}: {why:?}"),
});
return Err(cli::CliError::IO(format!(
"Issue writing to {filename}: {why:?}"
)));
}
Ok(())
}