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