Skip to content

Commit ee120fc

Browse files
committed
Remove BBLength from SizedAlgoKind::from_unsized
1 parent acb7de4 commit ee120fc

3 files changed

Lines changed: 12 additions & 23 deletions

File tree

src/uu/checksum_common/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ pub fn checksum_main(
205205
// Set the default algorithm to CRC when not '--check'ing.
206206
let algo_kind = algo.unwrap_or(AlgoKind::Crc);
207207

208-
let algo = SizedAlgoKind::from_unsized(algo_kind, length)?;
208+
let algo = SizedAlgoKind::from_unsized(algo_kind, length.map(BBLength::as_bits))?;
209209
let line_ending = LineEnding::from_zero_flag(matches.get_flag(options::ZERO));
210210

211211
let opts = ChecksumComputeOptions {

src/uucore/src/lib/features/checksum/mod.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -284,19 +284,12 @@ pub enum BBLength {
284284
}
285285

286286
impl BBLength {
287-
pub(crate) fn as_bits(self) -> usize {
287+
pub fn as_bits(self) -> usize {
288288
match self {
289289
BBLength::Byte(n) => n * 8,
290290
BBLength::Bit(n) => n,
291291
}
292292
}
293-
294-
pub(crate) fn as_bytes(self) -> usize {
295-
match self {
296-
BBLength::Byte(n) => n,
297-
BBLength::Bit(n) => n.div_ceil(8),
298-
}
299-
}
300293
}
301294

302295
/// Represents an actual determined algorithm.
@@ -320,9 +313,9 @@ pub enum SizedAlgoKind {
320313
}
321314

322315
impl SizedAlgoKind {
323-
pub fn from_unsized(kind: AlgoKind, output_length: Option<BBLength>) -> UResult<Self> {
316+
pub fn from_unsized(kind: AlgoKind, output_bitlen: Option<usize>) -> UResult<Self> {
324317
use AlgoKind as ak;
325-
match (kind, output_length) {
318+
match (kind, output_bitlen) {
326319
(
327320
ak::Sysv
328321
| ak::Bsd
@@ -347,19 +340,15 @@ impl SizedAlgoKind {
347340
(ak::Sha1, _) => Ok(Self::Sha1),
348341

349342
(ak::Blake2b, l) => Ok(Self::Blake2b(XOFLength::from_bits(
350-
l.map_or(Blake2b::DEFAULT_BIT_SIZE, BBLength::as_bits),
343+
l.unwrap_or(Blake2b::DEFAULT_BIT_SIZE),
351344
))),
352345
(ak::Blake3, l) => Ok(Self::Blake3(XOFLength::from_bits(
353-
l.map_or(Blake3::DEFAULT_BIT_SIZE, BBLength::as_bits),
346+
l.unwrap_or(Blake3::DEFAULT_BIT_SIZE),
354347
))),
355-
(ak::Shake128, l) => Ok(Self::Shake128(
356-
l.map(BBLength::as_bits).map(XOFLength::from_bits),
357-
)),
358-
(ak::Shake256, l) => Ok(Self::Shake256(
359-
l.map(BBLength::as_bits).map(XOFLength::from_bits),
360-
)),
361-
(ak::Sha2, Some(l)) => Ok(Self::Sha2(ShaLength::try_from(l.as_bits())?)),
362-
(ak::Sha3, Some(l)) => Ok(Self::Sha3(ShaLength::try_from(l.as_bits())?)),
348+
(ak::Shake128, l) => Ok(Self::Shake128(l.map(XOFLength::from_bits))),
349+
(ak::Shake256, l) => Ok(Self::Shake256(l.map(XOFLength::from_bits))),
350+
(ak::Sha2, Some(l)) => Ok(Self::Sha2(ShaLength::try_from(l)?)),
351+
(ak::Sha3, Some(l)) => Ok(Self::Sha3(ShaLength::try_from(l)?)),
363352
(algo @ (ak::Sha2 | ak::Sha3), None) => {
364353
Err(ChecksumError::LengthRequiredForSha(algo.to_lowercase().into()).into())
365354
}

src/uucore/src/lib/features/checksum/validate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ fn process_algo_based_line(
761761
let expected_checksum = get_raw_expected_digest(&line_info.checksum, digest_bit_length_hint)
762762
.ok_or(LineCheckError::ImproperlyFormatted)?;
763763

764-
let algo = SizedAlgoKind::from_unsized(algo_kind, algo_len)
764+
let algo = SizedAlgoKind::from_unsized(algo_kind, algo_len.map(BBLength::as_bits))
765765
.map_err(|_| LineCheckError::ImproperlyFormatted)?;
766766

767767
compute_and_check_digest_from_file(filename_to_check, &expected_checksum, algo, opts)
@@ -805,7 +805,7 @@ fn process_non_algo_based_line(
805805
_ => cli_algo_length,
806806
};
807807

808-
let algo = SizedAlgoKind::from_unsized(cli_algo_kind, algo_len)?;
808+
let algo = SizedAlgoKind::from_unsized(cli_algo_kind, algo_len.map(BBLength::as_bits))?;
809809

810810
compute_and_check_digest_from_file(filename_to_check, &expected_checksum, algo, opts)
811811
}

0 commit comments

Comments
 (0)