Skip to content

Commit d08da46

Browse files
committed
correction: introduce CorrectableError trait
This is just a helper trait to describe all the errors in the library from which we can get an "invalid residue". The residues contain all the information needed for error correction.
1 parent 4c6010e commit d08da46

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ use crate::primitives::decode::{ChecksumError, UncheckedHrpstring, UncheckedHrps
166166
#[doc(inline)]
167167
pub use {
168168
crate::primitives::checksum::Checksum,
169+
crate::primitives::correction::CorrectableError,
169170
crate::primitives::gf32::Fe32,
170171
crate::primitives::gf32_ext::{Fe1024, Fe32768},
171172
crate::primitives::hrp::Hrp,

src/primitives/correction.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
//! equation to identify the error values, in a BCH-encoded string.
77
//!
88
9+
use crate::primitives::decode::{
10+
CheckedHrpstringError, ChecksumError, InvalidResidueError, SegwitHrpstringError,
11+
};
12+
#[cfg(feature = "alloc")]
13+
use crate::DecodeError;
14+
915
/// **One more than** the maximum length (in characters) of a checksum which
1016
/// can be error-corrected without an allocator.
1117
///
@@ -33,3 +39,68 @@
3339
// this to 16, or maybe even higher. But we will wait for implementors who
3440
// complain.
3541
pub const NO_ALLOC_MAX_LENGTH: usize = 7;
42+
43+
/// Trait describing an error for which an error correction algorithm is applicable.
44+
///
45+
/// Essentially, this trait represents an error which contains an [`InvalidResidueError`]
46+
/// variant.
47+
pub trait CorrectableError {
48+
/// Given a decoding error, if this is a "checksum failed" error, extract
49+
/// that specific error type.
50+
///
51+
/// There are many ways in which decoding a checksummed string might fail.
52+
/// If the string was well-formed in all respects except that the final
53+
/// checksum characters appear to be wrong, it is possible to run an
54+
/// error correction algorithm to attempt to extract errors.
55+
///
56+
/// In all other cases we do not have enough information to do correction.
57+
///
58+
/// This is the function that implementors should implement.
59+
fn residue_error(&self) -> Option<&InvalidResidueError>;
60+
}
61+
62+
impl CorrectableError for InvalidResidueError {
63+
fn residue_error(&self) -> Option<&InvalidResidueError> { Some(self) }
64+
}
65+
66+
impl CorrectableError for ChecksumError {
67+
fn residue_error(&self) -> Option<&InvalidResidueError> {
68+
match self {
69+
ChecksumError::InvalidResidue(ref e) => Some(e),
70+
_ => None,
71+
}
72+
}
73+
}
74+
75+
impl CorrectableError for SegwitHrpstringError {
76+
fn residue_error(&self) -> Option<&InvalidResidueError> {
77+
match self {
78+
SegwitHrpstringError::Checksum(ref e) => e.residue_error(),
79+
_ => None,
80+
}
81+
}
82+
}
83+
84+
impl CorrectableError for CheckedHrpstringError {
85+
fn residue_error(&self) -> Option<&InvalidResidueError> {
86+
match self {
87+
CheckedHrpstringError::Checksum(ref e) => e.residue_error(),
88+
_ => None,
89+
}
90+
}
91+
}
92+
93+
#[cfg(feature = "alloc")]
94+
impl CorrectableError for crate::segwit::DecodeError {
95+
fn residue_error(&self) -> Option<&InvalidResidueError> { self.0.residue_error() }
96+
}
97+
98+
#[cfg(feature = "alloc")]
99+
impl CorrectableError for DecodeError {
100+
fn residue_error(&self) -> Option<&InvalidResidueError> {
101+
match self {
102+
DecodeError::Checksum(ref e) => e.residue_error(),
103+
_ => None,
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)