Skip to content

Commit 8eb7954

Browse files
committed
Fix checksum error type
We have a couple of checksum error problems; - Unused checksum related variants in `CharError` - Unuseful error message (duplicated string "invalid checksum") Fix both at the same time by removing the unused variants from `CharError` and by renaming the `InvalidChecksum` variant to `InvalidResidue` and improving the `Display` impl appropriately.
1 parent 7207926 commit 8eb7954

File tree

3 files changed

+19
-30
lines changed

3 files changed

+19
-30
lines changed

src/primitives/decode.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl<'s> UncheckedHrpstring<'s> {
174174
}
175175

176176
if self.data.len() < Ck::CHECKSUM_LENGTH {
177-
return Err(InvalidChecksumLength);
177+
return Err(InvalidLength);
178178
}
179179

180180
let mut checksum_eng = checksum::Engine::<Ck>::new();
@@ -186,7 +186,7 @@ impl<'s> UncheckedHrpstring<'s> {
186186
}
187187

188188
if checksum_eng.residue() != &Ck::TARGET_RESIDUE {
189-
return Err(InvalidChecksum);
189+
return Err(InvalidResidue);
190190
}
191191

192192
Ok(())
@@ -705,10 +705,6 @@ pub enum CharError {
705705
MissingSeparator,
706706
/// No characters after the separator.
707707
NothingAfterSeparator,
708-
/// The checksum does not match the rest of the data.
709-
InvalidChecksum,
710-
/// The checksum is not a valid length.
711-
InvalidChecksumLength,
712708
/// Some part of the string contains an invalid character.
713709
InvalidChar(char),
714710
/// The whole string must be of one case.
@@ -722,8 +718,6 @@ impl fmt::Display for CharError {
722718
match *self {
723719
MissingSeparator => write!(f, "missing human-readable separator, \"{}\"", SEP),
724720
NothingAfterSeparator => write!(f, "invalid data - no characters after the separator"),
725-
InvalidChecksum => write!(f, "invalid checksum"),
726-
InvalidChecksumLength => write!(f, "the checksum is not a valid length"),
727721
InvalidChar(n) => write!(f, "invalid character (code={})", n),
728722
MixedCase => write!(f, "mixed-case strings not allowed"),
729723
}
@@ -736,12 +730,7 @@ impl std::error::Error for CharError {
736730
use CharError::*;
737731

738732
match *self {
739-
MissingSeparator
740-
| NothingAfterSeparator
741-
| InvalidChecksum
742-
| InvalidChecksumLength
743-
| InvalidChar(_)
744-
| MixedCase => None,
733+
MissingSeparator | NothingAfterSeparator | InvalidChar(_) | MixedCase => None,
745734
}
746735
}
747736
}
@@ -750,19 +739,19 @@ impl std::error::Error for CharError {
750739
#[derive(Debug, Clone, PartialEq, Eq)]
751740
#[non_exhaustive]
752741
pub enum ChecksumError {
753-
/// The checksum does not match the rest of the data.
754-
InvalidChecksum,
742+
/// The checksum residue is not valid for the data.
743+
InvalidResidue,
755744
/// The checksum is not a valid length.
756-
InvalidChecksumLength,
745+
InvalidLength,
757746
}
758747

759748
impl fmt::Display for ChecksumError {
760749
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
761750
use ChecksumError::*;
762751

763752
match *self {
764-
InvalidChecksum => write!(f, "invalid checksum"),
765-
InvalidChecksumLength => write!(f, "the checksum is not a valid length"),
753+
InvalidResidue => write!(f, "the checksum residue is not valid for the data"),
754+
InvalidLength => write!(f, "the checksum is not a valid length"),
766755
}
767756
}
768757
}
@@ -773,7 +762,7 @@ impl std::error::Error for ChecksumError {
773762
use ChecksumError::*;
774763

775764
match *self {
776-
InvalidChecksum | InvalidChecksumLength => None,
765+
InvalidResidue | InvalidLength => None,
777766
}
778767
}
779768
}
@@ -858,13 +847,13 @@ mod tests {
858847
.expect("string parses correctly")
859848
.validate_checksum::<Bech32>()
860849
.unwrap_err();
861-
assert_eq!(err, InvalidChecksumLength);
850+
assert_eq!(err, InvalidLength);
862851

863852
let err = UncheckedHrpstring::new("A1G7SGD8")
864853
.expect("string parses correctly")
865854
.validate_checksum::<Bech32>()
866855
.unwrap_err();
867-
assert_eq!(err, InvalidChecksum);
856+
assert_eq!(err, InvalidResidue);
868857
}
869858

870859
#[test]
@@ -918,14 +907,14 @@ mod tests {
918907
for s in invalid {
919908
let err =
920909
UncheckedHrpstring::new(s).unwrap().validate_checksum::<Bech32m>().unwrap_err();
921-
assert_eq!(err, InvalidChecksumLength);
910+
assert_eq!(err, InvalidLength);
922911
}
923912

924913
let err = UncheckedHrpstring::new("M1VUXWEZ")
925914
.unwrap()
926915
.validate_checksum::<Bech32m>()
927916
.unwrap_err();
928-
assert_eq!(err, InvalidChecksum);
917+
assert_eq!(err, InvalidResidue);
929918
}
930919

931920
#[test]

tests/bip_173_test_vectors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ fn bip_173_checksum_calculated_with_uppercase_form() {
1818

1919
assert_eq!(
2020
CheckedHrpstring::new::<Bech32>(s).unwrap_err(),
21-
CheckedHrpstringError::Checksum(ChecksumError::InvalidChecksum)
21+
CheckedHrpstringError::Checksum(ChecksumError::InvalidResidue)
2222
);
2323

2424
assert_eq!(
2525
SegwitHrpstring::new(s).unwrap_err(),
26-
SegwitHrpstringError::Checksum(ChecksumError::InvalidChecksum)
26+
SegwitHrpstringError::Checksum(ChecksumError::InvalidResidue)
2727
);
2828
}
2929

@@ -35,7 +35,7 @@ macro_rules! check_valid_bech32 {
3535
let p = UncheckedHrpstring::new($valid_bech32).unwrap();
3636
p.validate_checksum::<Bech32>().expect("valid bech32");
3737
// Valid bech32 strings are by definition invalid bech32m.
38-
assert_eq!(p.validate_checksum::<Bech32m>().unwrap_err(), ChecksumError::InvalidChecksum);
38+
assert_eq!(p.validate_checksum::<Bech32m>().unwrap_err(), ChecksumError::InvalidResidue);
3939
}
4040
)*
4141
}

tests/bip_350_test_vectors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ fn bip_350_checksum_calculated_with_uppercase_form() {
1717

1818
assert_eq!(
1919
CheckedHrpstring::new::<Bech32m>(s).unwrap_err(),
20-
CheckedHrpstringError::Checksum(ChecksumError::InvalidChecksum)
20+
CheckedHrpstringError::Checksum(ChecksumError::InvalidResidue)
2121
);
2222

2323
assert_eq!(
2424
SegwitHrpstring::new(s).unwrap_err(),
25-
SegwitHrpstringError::Checksum(ChecksumError::InvalidChecksum)
25+
SegwitHrpstringError::Checksum(ChecksumError::InvalidResidue)
2626
);
2727
}
2828

@@ -34,7 +34,7 @@ macro_rules! check_valid_bech32m {
3434
let p = UncheckedHrpstring::new($valid_bech32m).unwrap();
3535
p.validate_checksum::<Bech32m>().expect("valid bech32m");
3636
// Valid bech32m strings are by definition invalid bech32.
37-
assert_eq!(p.validate_checksum::<Bech32>().unwrap_err(), ChecksumError::InvalidChecksum);
37+
assert_eq!(p.validate_checksum::<Bech32>().unwrap_err(), ChecksumError::InvalidResidue);
3838
}
3939
)*
4040
}

0 commit comments

Comments
 (0)