|
6 | 6 | //! equation to identify the error values, in a BCH-encoded string.
|
7 | 7 | //!
|
8 | 8 |
|
| 9 | +use crate::primitives::decode::{ |
| 10 | + CheckedHrpstringError, ChecksumError, InvalidResidueError, SegwitHrpstringError, |
| 11 | +}; |
| 12 | +#[cfg(feature = "alloc")] |
| 13 | +use crate::DecodeError; |
| 14 | + |
9 | 15 | /// **One more than** the maximum length (in characters) of a checksum which
|
10 | 16 | /// can be error-corrected without an allocator.
|
11 | 17 | ///
|
|
33 | 39 | // this to 16, or maybe even higher. But we will wait for implementors who
|
34 | 40 | // complain.
|
35 | 41 | 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