Skip to content

Commit b1d9fd7

Browse files
Bump byte-unit from 4.0.19 to 5.0.3 (#141)
1 parent 607efb6 commit b1d9fd7

File tree

4 files changed

+116
-36
lines changed

4 files changed

+116
-36
lines changed

Cargo.lock

Lines changed: 90 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ path = "src/main.rs"
2626
[dependencies]
2727
abcrypt = { version = "0.2.6", path = "../abcrypt", features = ["serde"] }
2828
anyhow.workspace = true
29-
byte-unit = { version = "4.0.19", default-features = false, features = ["std"] }
29+
byte-unit = "5.0.3"
3030
clap = { workspace = true, features = ["wrap_help"] }
3131
clap_complete = "4.4.4"
3232
clap_complete_nushell = "4.4.2"

crates/cli/src/cli.rs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::{
1313

1414
use abcrypt::argon2::Params;
1515
use anyhow::anyhow;
16-
use byte_unit::{Byte, ByteUnit, KIBIBYTE};
16+
use byte_unit::{Byte, Unit};
1717
use clap::{
1818
builder::{TypedValueParser, ValueParserFactory},
1919
value_parser, ArgGroup, Args, CommandFactory, Parser, Subcommand, ValueEnum, ValueHint,
@@ -300,10 +300,9 @@ impl Deref for MemorySize {
300300

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

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

313312
fn from_str(byte: &str) -> anyhow::Result<Self> {
314313
let byte = Byte::from_str(byte)
315-
.map(|b| b.get_bytes())
314+
.map(u64::from)
316315
.map_err(anyhow::Error::from)?;
317-
match u32::try_from(byte / KIBIBYTE) {
316+
match u32::try_from(byte / Byte::KIBIBYTE.as_u64()) {
318317
Ok(kibibyte) if (Params::MIN_M_COST..=Params::MAX_M_COST).contains(&kibibyte) => {
319318
Ok(Self(kibibyte))
320319
}
321320
_ => Err(anyhow!(
322-
"{} is not in {}..={}",
323-
Byte::from_bytes(byte)
324-
.get_adjusted_unit(ByteUnit::KiB)
325-
.format(0),
321+
"{:.0} is not in {}..={}",
322+
Byte::from(byte).get_adjusted_unit(Unit::KiB),
326323
Self::MIN,
327324
Self::MAX
328325
)),
@@ -505,48 +502,48 @@ mod tests {
505502

506503
#[test]
507504
fn from_str_memory_size_with_invalid_unit() {
508-
use byte_unit::ByteError;
505+
use byte_unit::ParseError;
509506

510507
assert!(matches!(
511508
MemorySize::from_str("19922944 A")
512509
.unwrap_err()
513-
.downcast_ref::<ByteError>()
510+
.downcast_ref::<ParseError>()
514511
.unwrap(),
515-
ByteError::UnitIncorrect(_)
512+
ParseError::Unit(_)
516513
));
517514
assert!(matches!(
518515
MemorySize::from_str("19.00LiB")
519516
.unwrap_err()
520-
.downcast_ref::<ByteError>()
517+
.downcast_ref::<ParseError>()
521518
.unwrap(),
522-
ByteError::UnitIncorrect(_)
519+
ParseError::Unit(_)
523520
));
524521
}
525522

526523
#[test]
527524
fn from_str_memory_size_with_nan() {
528-
use byte_unit::ByteError;
525+
use byte_unit::ParseError;
529526

530527
assert!(matches!(
531528
MemorySize::from_str("n B")
532529
.unwrap_err()
533-
.downcast_ref::<ByteError>()
530+
.downcast_ref::<ParseError>()
534531
.unwrap(),
535-
ByteError::ValueIncorrect(_)
532+
ParseError::Value(_)
536533
));
537534
assert!(matches!(
538535
MemorySize::from_str("n")
539536
.unwrap_err()
540-
.downcast_ref::<ByteError>()
537+
.downcast_ref::<ParseError>()
541538
.unwrap(),
542-
ByteError::ValueIncorrect(_)
539+
ParseError::Value(_)
543540
));
544541
assert!(matches!(
545542
MemorySize::from_str("nKiB")
546543
.unwrap_err()
547-
.downcast_ref::<ByteError>()
544+
.downcast_ref::<ParseError>()
548545
.unwrap(),
549-
ByteError::ValueIncorrect(_)
546+
ParseError::Value(_)
550547
));
551548
}
552549

crates/cli/tests/integration.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ fn validate_m_parameter_with_invalid_unit_for_encrypt_command() {
173173
.assert()
174174
.failure()
175175
.code(2)
176-
.stderr(predicate::str::contains("The character 'A' is incorrect. 'B', 'K', 'M', 'G', 'T', 'P' or no character is expected"));
176+
.stderr(predicate::str::contains("the character 'A' is incorrect ('B', 'K', 'M', 'G', 'T', 'P', 'E' or no character or no character is expected)"));
177177
command()
178178
.arg("encrypt")
179179
.arg("-m")
@@ -185,7 +185,7 @@ fn validate_m_parameter_with_invalid_unit_for_encrypt_command() {
185185
.assert()
186186
.failure()
187187
.code(2)
188-
.stderr(predicate::str::contains("The character 'L' is incorrect. 'B', 'K', 'M', 'G', 'T', 'P' or no character is expected"));
188+
.stderr(predicate::str::contains("the character 'L' is incorrect ('B', 'K', 'M', 'G', 'T', 'P', 'E' or no character or no character is expected)"));
189189
}
190190

191191
#[test]
@@ -202,7 +202,7 @@ fn validate_m_parameter_with_nan_for_encrypt_command() {
202202
.failure()
203203
.code(2)
204204
.stderr(predicate::str::contains(
205-
"The character 'n' is not a number",
205+
"the character 'n' is not a number",
206206
));
207207
command()
208208
.arg("encrypt")
@@ -216,7 +216,7 @@ fn validate_m_parameter_with_nan_for_encrypt_command() {
216216
.failure()
217217
.code(2)
218218
.stderr(predicate::str::contains(
219-
"The character 'n' is not a number",
219+
"the character 'n' is not a number",
220220
));
221221
command()
222222
.arg("encrypt")
@@ -230,7 +230,7 @@ fn validate_m_parameter_with_nan_for_encrypt_command() {
230230
.failure()
231231
.code(2)
232232
.stderr(predicate::str::contains(
233-
"The character 'n' is not a number",
233+
"the character 'n' is not a number",
234234
));
235235
}
236236

0 commit comments

Comments
 (0)