Skip to content

Commit

Permalink
Merge pull request #42 from jgardona/feat/hr-errors
Browse files Browse the repository at this point in the history
More Human Errors
  • Loading branch information
jgardona authored Nov 29, 2023
2 parents 5150d2d + 84d0eb8 commit 268aa39
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 13 deletions.
64 changes: 53 additions & 11 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ mod errors;
mod fs;
mod view;

use std::num::ParseIntError;

use clap::Parser;

use self::{errors::Result, fs::read_data, view::display_data};
use crate::CliError;

use self::{
errors::{Result, ERR_CANT_PARSE_NUMBER, ERR_NOT_AVAILABLE_DATA},
fs::read_data,
view::display_data,
};

#[derive(Debug, Parser)]
#[command(author, version, about, long_about = None)]
Expand Down Expand Up @@ -45,39 +49,53 @@ pub fn execute() -> Result<()> {
let length = parse_unit(&cli.length.unwrap_or("0".into()))?;
let skip = parse_unit(&cli.skip)?;

let data = read_data(skip, length, &cli.filename)?;
let data = read_data(skip, length, &cli.filename)
.map_err(|_| CliError(ERR_NOT_AVAILABLE_DATA.into()))?;
display_data(skip, !cli.squeeze, &data, &mut std::io::stdout().lock())?;

Ok(())
}

fn parse_unit(input: &str) -> std::result::Result<usize, ParseIntError> {
fn parse_unit(input: &str) -> std::result::Result<usize, CliError> {
if let Some(suffix) = input.strip_suffix("kb") {
let mut value = suffix.parse::<usize>()?;
let mut value = suffix
.parse::<usize>()
.map_err(|_| CliError(ERR_CANT_PARSE_NUMBER.into()))?;
value *= 1000;
Ok(value)
} else if let Some(suffix) = input.strip_suffix("mb") {
let mut value = suffix.parse::<usize>()?;
let mut value = suffix
.parse::<usize>()
.map_err(|_| CliError(ERR_CANT_PARSE_NUMBER.into()))?;
value *= 1000 * 1000;
Ok(value)
} else if let Some(suffix) = input.strip_suffix('K') {
let mut value = suffix.parse::<usize>()?;
let mut value = suffix
.parse::<usize>()
.map_err(|_| CliError(ERR_CANT_PARSE_NUMBER.into()))?;
value *= 1024;
Ok(value)
} else if let Some(suffix) = input.strip_suffix('M') {
let mut value = suffix.parse::<usize>()?;
let mut value = suffix
.parse::<usize>()
.map_err(|_| CliError(ERR_CANT_PARSE_NUMBER.into()))?;
value *= 1024 * 1024;
Ok(value)
} else if let Some(prefix) = input.strip_prefix("0x") {
let value = usize::from_str_radix(prefix, 16)?;
let value = usize::from_str_radix(prefix, 16)
.map_err(|_| CliError(ERR_CANT_PARSE_NUMBER.into()))?;
Ok(value)
} else {
input.parse::<usize>()
input
.parse::<usize>()
.map_err(|_| CliError(ERR_CANT_PARSE_NUMBER.into()))
}
}

#[cfg(test)]
mod test_cli {
use crate::{cli::errors::ERR_CANT_PARSE_NUMBER, CliError};

use super::parse_unit;
use anyhow::{Ok, Result};

Expand Down Expand Up @@ -119,6 +137,30 @@ mod test_cli {
let result = parse_unit("0x400")?;
assert_eq!(expected, result);

let expected = CliError(ERR_CANT_PARSE_NUMBER.into());
let result = parse_unit("fffkb").unwrap_err();
assert_eq!(expected.to_string(), result.to_string());

let expected = CliError(ERR_CANT_PARSE_NUMBER.into());
let result = parse_unit("fffmb").unwrap_err();
assert_eq!(expected.to_string(), result.to_string());

let expected = CliError(ERR_CANT_PARSE_NUMBER.into());
let result = parse_unit("fffK").unwrap_err();
assert_eq!(expected.to_string(), result.to_string());

let expected = CliError(ERR_CANT_PARSE_NUMBER.into());
let result = parse_unit("fffM").unwrap_err();
assert_eq!(expected.to_string(), result.to_string());

let expected = CliError(ERR_CANT_PARSE_NUMBER.into());
let result = parse_unit("0xkkk").unwrap_err();
assert_eq!(expected.to_string(), result.to_string());

let expected = CliError(ERR_CANT_PARSE_NUMBER.into());
let result = parse_unit("ff").unwrap_err();
assert_eq!(expected.to_string(), result.to_string());

Ok(())
}
}
3 changes: 3 additions & 0 deletions src/cli/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
pub type Error = Box<dyn std::error::Error>;
pub type Result<T> = std::result::Result<T, Error>;
pub static ERR_CANT_PARSE_NUMBER: &str = "Cant parse input number";
pub static ERR_NOT_AVAILABLE_DATA: &str =
"Cant read data. Check if you are trying to read more than is available, or if the file exists";

#[cfg(test)]
mod errors_test {
Expand Down
13 changes: 13 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
use std::{error::Error, fmt::Display};

use cli::execute;

mod cli;

#[derive(Debug)]
pub struct CliError(String);

impl Error for CliError {}

impl Display for CliError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

fn main() {
execute().unwrap_or_else(|e| {
eprintln!("Something wrong happened: {e}");
Expand Down
4 changes: 2 additions & 2 deletions tests/integrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn test_no_squeeze_parse_1kb_() -> Result<()> {

#[test]
fn test_file_not_found() -> Result<()> {
let expected = "Something wrong happened: No such file or directory (os error 2)\n";
let expected = "Something wrong happened: Cant read data. Check if you are trying to read more than is available, or if the file exists\n";

let mut cmd = assert_cmd::Command::cargo_bin("mhv")?;
cmd.arg("tests/data/dat")
Expand All @@ -80,7 +80,7 @@ fn test_file_not_found() -> Result<()> {

#[test]
fn test_buffer_overflow() -> Result<()> {
let expected = "Something wrong happened: failed to fill whole buffer\n";
let expected = "Something wrong happened: Cant read data. Check if you are trying to read more than is available, or if the file exists\n";

let mut cmd = assert_cmd::Command::cargo_bin("mhv")?;
cmd.arg("-l50")
Expand Down

0 comments on commit 268aa39

Please sign in to comment.