@@ -178,13 +178,16 @@ impl Fe32 {
178
178
179
179
/// Creates a field element from a single bech32 character.
180
180
#[ inline]
181
- pub fn from_char ( c : char ) -> Result < Fe32 , Error > {
181
+ pub fn from_char ( c : char ) -> Result < Fe32 , FromCharError > {
182
+ use FromCharError :: * ;
183
+
182
184
// i8::try_from gets a value in the range 0..=127 since char is unsigned.
183
- let byte = i8:: try_from ( u32:: from ( c) ) . map_err ( |_| Error :: InvalidChar ( c) ) ?;
185
+ let byte = i8:: try_from ( u32:: from ( c) ) . map_err ( |_| NotAscii ( c) ) ?;
184
186
// Now we have a valid ASCII value cast is safe.
185
187
let ascii = byte as usize ;
186
188
// We use -1 for any array element that is an invalid char to trigger error from u8::try_from
187
- let u5 = u8:: try_from ( CHARS_INV [ ascii] ) . map_err ( |_| Error :: InvalidChar ( c) ) ?;
189
+ let u5 = u8:: try_from ( CHARS_INV [ ascii] ) . map_err ( |_| Invalid ( c) ) ?;
190
+
188
191
Ok ( Fe32 ( u5) )
189
192
}
190
193
@@ -245,7 +248,7 @@ macro_rules! impl_try_from {
245
248
( $( $ty: ident) +) => {
246
249
$(
247
250
impl TryFrom <$ty> for Fe32 {
248
- type Error = Error ;
251
+ type Error = TryFromError ;
249
252
250
253
/// Tries to create an [`Fe32`] type from a signed source number type.
251
254
///
@@ -256,7 +259,7 @@ macro_rules! impl_try_from {
256
259
fn try_from( value: $ty) -> Result <Self , Self :: Error > {
257
260
let byte = u8 :: try_from( value) ?;
258
261
if byte > 31 {
259
- Err ( Error :: InvalidByte ( byte) ) ?;
262
+ Err ( TryFromError :: InvalidByte ( byte) ) ?;
260
263
}
261
264
Ok ( Fe32 ( byte) )
262
265
}
@@ -324,48 +327,77 @@ impl ops::DivAssign for Fe32 {
324
327
fn div_assign ( & mut self , other : Fe32 ) { * self = * self / other; }
325
328
}
326
329
327
- /// A galois field related error.
330
+ /// A galois field error when converting from a character .
328
331
#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
329
332
#[ non_exhaustive]
330
- pub enum Error {
333
+ pub enum FromCharError {
334
+ /// Tried to interpret a character as a GF32 element but it is not an ASCII character.
335
+ NotAscii ( char ) ,
336
+ /// Tried to interpret a character as a GF32 element but it is not part of the bech32 character set.
337
+ Invalid ( char ) ,
338
+ }
339
+
340
+ impl fmt:: Display for FromCharError {
341
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
342
+ use FromCharError :: * ;
343
+
344
+ match * self {
345
+ NotAscii ( c) => write ! ( f, "non-ascii char in field element: {}" , c) ,
346
+ Invalid ( c) => write ! ( f, "invalid char in field element: {}" , c) ,
347
+ }
348
+ }
349
+ }
350
+
351
+ #[ cfg( feature = "std" ) ]
352
+ impl std:: error:: Error for FromCharError {
353
+ fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > {
354
+ use FromCharError :: * ;
355
+
356
+ match * self {
357
+ NotAscii ( _) | Invalid ( _) => None ,
358
+ }
359
+ }
360
+ }
361
+
362
+ /// A galois field error when converting from an integer.
363
+ #[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
364
+ #[ non_exhaustive]
365
+ pub enum TryFromError {
331
366
/// Tried to interpret an integer as a GF32 element but it could not be converted to an u8.
332
367
NotAByte ( num:: TryFromIntError ) ,
333
368
/// Tried to interpret a byte as a GF32 element but its numeric value was outside of [0, 32).
334
369
InvalidByte ( u8 ) ,
335
- /// Tried to interpret a character as a GF32 element but it is not part of the bech32 character set.
336
- InvalidChar ( char ) ,
337
370
}
338
371
339
- impl fmt:: Display for Error {
372
+ impl fmt:: Display for TryFromError {
340
373
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
341
- use Error :: * ;
374
+ use TryFromError :: * ;
342
375
343
376
match * self {
344
377
NotAByte ( ref e) => write_err ! ( f, "invalid field element" ; e) ,
345
378
InvalidByte ( ref b) => write ! ( f, "invalid byte in field element: {:#04x}" , b) ,
346
- InvalidChar ( ref c) => write ! ( f, "invalid char in field element: {}" , c) ,
347
379
}
348
380
}
349
381
}
350
382
351
383
#[ cfg( feature = "std" ) ]
352
- impl std:: error:: Error for Error {
384
+ impl std:: error:: Error for TryFromError {
353
385
fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > {
354
- use Error :: * ;
386
+ use TryFromError :: * ;
355
387
356
388
match * self {
357
389
NotAByte ( ref e) => Some ( e) ,
358
- InvalidByte ( _) | InvalidChar ( _ ) => None ,
390
+ InvalidByte ( _) => None ,
359
391
}
360
392
}
361
393
}
362
394
363
- impl From < num:: TryFromIntError > for Error {
395
+ impl From < num:: TryFromIntError > for TryFromError {
364
396
#[ inline]
365
- fn from ( e : num:: TryFromIntError ) -> Self { Error :: NotAByte ( e) }
397
+ fn from ( e : num:: TryFromIntError ) -> Self { Self :: NotAByte ( e) }
366
398
}
367
399
368
- impl From < Infallible > for Error {
400
+ impl From < Infallible > for TryFromError {
369
401
#[ inline]
370
402
fn from ( i : Infallible ) -> Self { match i { } }
371
403
}
0 commit comments