Skip to content

Commit aaf742d

Browse files
authored
Merge pull request #8645 from cakebaker/uucore_checksum_simplify_create_sha3
uucore/checksum: improve API of `create_sha3()`
2 parents f4b7638 + 68d3e5f commit aaf742d

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

src/uu/hashsum/src/hashsum.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,10 @@ fn create_algorithm_from_flags(matches: &ArgMatches) -> UResult<HashAlgorithm> {
9797
set_or_err(detect_algo("b3sum", None)?)?;
9898
}
9999
if matches.get_flag("sha3") {
100-
let bits = matches.get_one::<usize>("bits").copied();
101-
set_or_err(create_sha3(bits)?)?;
100+
match matches.get_one::<usize>("bits") {
101+
Some(bits) => set_or_err(create_sha3(*bits)?)?,
102+
None => return Err(ChecksumError::BitsRequiredForSha3.into()),
103+
}
102104
}
103105
if matches.get_flag("sha3-224") {
104106
set_or_err(HashAlgorithm {

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,34 +249,32 @@ impl UError for ChecksumError {
249249
///
250250
/// # Returns
251251
///
252-
/// Returns a UResult of a tuple containing the algorithm name, the hasher instance, and
253-
/// the output length in bits or an Err if an unsupported output size is provided, or if
254-
/// the `--bits` flag is missing.
255-
pub fn create_sha3(bits: Option<usize>) -> UResult<HashAlgorithm> {
252+
/// Returns a `UResult` with an `HashAlgorithm` or an `Err` if an unsupported
253+
/// output size is provided.
254+
pub fn create_sha3(bits: usize) -> UResult<HashAlgorithm> {
256255
match bits {
257-
Some(224) => Ok(HashAlgorithm {
256+
224 => Ok(HashAlgorithm {
258257
name: "SHA3_224",
259258
create_fn: Box::new(|| Box::new(Sha3_224::new())),
260259
bits: 224,
261260
}),
262-
Some(256) => Ok(HashAlgorithm {
261+
256 => Ok(HashAlgorithm {
263262
name: "SHA3_256",
264263
create_fn: Box::new(|| Box::new(Sha3_256::new())),
265264
bits: 256,
266265
}),
267-
Some(384) => Ok(HashAlgorithm {
266+
384 => Ok(HashAlgorithm {
268267
name: "SHA3_384",
269268
create_fn: Box::new(|| Box::new(Sha3_384::new())),
270269
bits: 384,
271270
}),
272-
Some(512) => Ok(HashAlgorithm {
271+
512 => Ok(HashAlgorithm {
273272
name: "SHA3_512",
274273
create_fn: Box::new(|| Box::new(Sha3_512::new())),
275274
bits: 512,
276275
}),
277276

278-
Some(_) => Err(ChecksumError::InvalidOutputSizeForSha3.into()),
279-
None => Err(ChecksumError::BitsRequiredForSha3.into()),
277+
_ => Err(ChecksumError::InvalidOutputSizeForSha3.into()),
280278
}
281279
}
282280

@@ -459,8 +457,10 @@ pub fn detect_algo(algo: &str, length: Option<usize>) -> UResult<HashAlgorithm>
459457
bits,
460458
})
461459
}
462-
//ALGORITHM_OPTIONS_SHA3 | "sha3" => (
463-
_ if algo.starts_with("sha3") => create_sha3(length),
460+
_ if algo.starts_with("sha3") => {
461+
let bits = length.ok_or(ChecksumError::BitsRequiredForSha3)?;
462+
create_sha3(bits)
463+
}
464464

465465
_ => Err(ChecksumError::UnknownAlgorithm.into()),
466466
}

0 commit comments

Comments
 (0)