Skip to content

Commit

Permalink
Bump byte-unit from 4.0.19 to 5.0.3 (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
dependabot[bot] authored Nov 28, 2023
1 parent 607efb6 commit b1d9fd7
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 36 deletions.
97 changes: 90 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ path = "src/main.rs"
[dependencies]
abcrypt = { version = "0.2.6", path = "../abcrypt", features = ["serde"] }
anyhow.workspace = true
byte-unit = { version = "4.0.19", default-features = false, features = ["std"] }
byte-unit = "5.0.3"
clap = { workspace = true, features = ["wrap_help"] }
clap_complete = "4.4.4"
clap_complete_nushell = "4.4.2"
Expand Down
43 changes: 20 additions & 23 deletions crates/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{

use abcrypt::argon2::Params;
use anyhow::anyhow;
use byte_unit::{Byte, ByteUnit, KIBIBYTE};
use byte_unit::{Byte, Unit};
use clap::{
builder::{TypedValueParser, ValueParserFactory},
value_parser, ArgGroup, Args, CommandFactory, Parser, Subcommand, ValueEnum, ValueHint,
Expand Down Expand Up @@ -300,10 +300,9 @@ impl Deref for MemorySize {

impl fmt::Display for MemorySize {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Byte::from_bytes(u64::from(self.0) * KIBIBYTE)
.get_adjusted_unit(ByteUnit::KiB)
.format(0)
.fmt(f)
let byte =
Byte::from(u64::from(self.0) * Byte::KIBIBYTE.as_u64()).get_adjusted_unit(Unit::KiB);
write!(f, "{byte:.0}")
}
}

Expand All @@ -312,17 +311,15 @@ impl FromStr for MemorySize {

fn from_str(byte: &str) -> anyhow::Result<Self> {
let byte = Byte::from_str(byte)
.map(|b| b.get_bytes())
.map(u64::from)
.map_err(anyhow::Error::from)?;
match u32::try_from(byte / KIBIBYTE) {
match u32::try_from(byte / Byte::KIBIBYTE.as_u64()) {
Ok(kibibyte) if (Params::MIN_M_COST..=Params::MAX_M_COST).contains(&kibibyte) => {
Ok(Self(kibibyte))
}
_ => Err(anyhow!(
"{} is not in {}..={}",
Byte::from_bytes(byte)
.get_adjusted_unit(ByteUnit::KiB)
.format(0),
"{:.0} is not in {}..={}",
Byte::from(byte).get_adjusted_unit(Unit::KiB),
Self::MIN,
Self::MAX
)),
Expand Down Expand Up @@ -505,48 +502,48 @@ mod tests {

#[test]
fn from_str_memory_size_with_invalid_unit() {
use byte_unit::ByteError;
use byte_unit::ParseError;

assert!(matches!(
MemorySize::from_str("19922944 A")
.unwrap_err()
.downcast_ref::<ByteError>()
.downcast_ref::<ParseError>()
.unwrap(),
ByteError::UnitIncorrect(_)
ParseError::Unit(_)
));
assert!(matches!(
MemorySize::from_str("19.00LiB")
.unwrap_err()
.downcast_ref::<ByteError>()
.downcast_ref::<ParseError>()
.unwrap(),
ByteError::UnitIncorrect(_)
ParseError::Unit(_)
));
}

#[test]
fn from_str_memory_size_with_nan() {
use byte_unit::ByteError;
use byte_unit::ParseError;

assert!(matches!(
MemorySize::from_str("n B")
.unwrap_err()
.downcast_ref::<ByteError>()
.downcast_ref::<ParseError>()
.unwrap(),
ByteError::ValueIncorrect(_)
ParseError::Value(_)
));
assert!(matches!(
MemorySize::from_str("n")
.unwrap_err()
.downcast_ref::<ByteError>()
.downcast_ref::<ParseError>()
.unwrap(),
ByteError::ValueIncorrect(_)
ParseError::Value(_)
));
assert!(matches!(
MemorySize::from_str("nKiB")
.unwrap_err()
.downcast_ref::<ByteError>()
.downcast_ref::<ParseError>()
.unwrap(),
ByteError::ValueIncorrect(_)
ParseError::Value(_)
));
}

Expand Down
10 changes: 5 additions & 5 deletions crates/cli/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn validate_m_parameter_with_invalid_unit_for_encrypt_command() {
.assert()
.failure()
.code(2)
.stderr(predicate::str::contains("The character 'A' is incorrect. 'B', 'K', 'M', 'G', 'T', 'P' or no character is expected"));
.stderr(predicate::str::contains("the character 'A' is incorrect ('B', 'K', 'M', 'G', 'T', 'P', 'E' or no character or no character is expected)"));
command()
.arg("encrypt")
.arg("-m")
Expand All @@ -185,7 +185,7 @@ fn validate_m_parameter_with_invalid_unit_for_encrypt_command() {
.assert()
.failure()
.code(2)
.stderr(predicate::str::contains("The character 'L' is incorrect. 'B', 'K', 'M', 'G', 'T', 'P' or no character is expected"));
.stderr(predicate::str::contains("the character 'L' is incorrect ('B', 'K', 'M', 'G', 'T', 'P', 'E' or no character or no character is expected)"));
}

#[test]
Expand All @@ -202,7 +202,7 @@ fn validate_m_parameter_with_nan_for_encrypt_command() {
.failure()
.code(2)
.stderr(predicate::str::contains(
"The character 'n' is not a number",
"the character 'n' is not a number",
));
command()
.arg("encrypt")
Expand All @@ -216,7 +216,7 @@ fn validate_m_parameter_with_nan_for_encrypt_command() {
.failure()
.code(2)
.stderr(predicate::str::contains(
"The character 'n' is not a number",
"the character 'n' is not a number",
));
command()
.arg("encrypt")
Expand All @@ -230,7 +230,7 @@ fn validate_m_parameter_with_nan_for_encrypt_command() {
.failure()
.code(2)
.stderr(predicate::str::contains(
"The character 'n' is not a number",
"the character 'n' is not a number",
));
}

Expand Down

0 comments on commit b1d9fd7

Please sign in to comment.