Skip to content

Commit e4f16cf

Browse files
committed
Implement error traits for gf32::Error
Remove the `todo!` and implement the error traits.
1 parent a2209d5 commit e4f16cf

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/primitives/gf32.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use core::{fmt, num, ops};
1616
#[cfg(all(test, mutate))]
1717
use mutagen::mutate;
1818

19+
use crate::write_err;
20+
1921
/// Logarithm table of each bech32 element, as a power of alpha = Z.
2022
///
2123
/// Includes Q as 0 but this is false; you need to exclude Q because it has no discrete log. If we
@@ -317,12 +319,27 @@ pub enum Error {
317319
}
318320

319321
impl fmt::Display for Error {
320-
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { todo!() }
322+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
323+
use Error::*;
324+
325+
match *self {
326+
NotAByte(ref e) => write_err!(f, "invalid field element"; e),
327+
InvalidByte(ref b) => write!(f, "invalid byte in field element: {:#04x}", b),
328+
InvalidChar(ref c) => write!(f, "invalid char in field element: {}", c),
329+
}
330+
}
321331
}
322332

323333
#[cfg(feature = "std")]
324334
impl std::error::Error for Error {
325-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { todo!() }
335+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
336+
use Error::*;
337+
338+
match *self {
339+
NotAByte(ref e) => Some(e),
340+
InvalidByte(_) | InvalidChar(_) => None,
341+
}
342+
}
326343
}
327344

328345
impl From<num::TryFromIntError> for Error {

0 commit comments

Comments
 (0)