@@ -12,38 +12,20 @@ macro_rules! const_panic {
12
12
} ;
13
13
}
14
14
15
- /// An error happened while decoding bytes to make a principal.
16
- #[ derive( Error , Clone , Debug , Eq , PartialEq ) ]
17
- pub enum PrincipalBufferError {
18
- #[ error( "Buffer is too long." ) ]
19
- BufferTooLong ( ) ,
20
- }
21
-
22
15
/// An error happened while encoding, decoding or serializing a principal.
23
16
#[ derive( Error , Clone , Debug , Eq , PartialEq ) ]
24
17
pub enum PrincipalError {
25
18
#[ error( "Buffer is too long." ) ]
26
19
BufferTooLong ( ) ,
27
20
28
21
#[ error( r#"Invalid textual format: expected "{0}""# ) ]
29
- AbnormalTextualFormat ( String ) ,
22
+ AbnormalTextualFormat ( Principal ) ,
30
23
31
24
#[ error( "Text must be a base 32 string." ) ]
32
25
InvalidTextualFormatNotBase32 ( ) ,
33
26
34
27
#[ error( "Text cannot be converted to a Principal; too small." ) ]
35
28
TextTooSmall ( ) ,
36
-
37
- #[ error( "A custom tool returned an error instead of a Principal: {0}" ) ]
38
- ExternalError ( String ) ,
39
- }
40
-
41
- impl From < PrincipalBufferError > for PrincipalError {
42
- fn from ( v : PrincipalBufferError ) -> Self {
43
- match v {
44
- PrincipalBufferError :: BufferTooLong ( ) => Self :: BufferTooLong ( ) ,
45
- }
46
- }
47
29
}
48
30
49
31
/// A class of principal. Because this should not be exposed it
@@ -204,13 +186,13 @@ impl Principal {
204
186
}
205
187
206
188
/// Attempt to decode a slice into a Principal.
207
- pub const fn try_from_slice ( bytes : & [ u8 ] ) -> Result < Self , PrincipalBufferError > {
189
+ pub const fn try_from_slice ( bytes : & [ u8 ] ) -> Result < Self , PrincipalError > {
208
190
match bytes {
209
191
[ ] => Ok ( Principal :: management_canister ( ) ) ,
210
192
[ 4 ] => Ok ( Principal :: anonymous ( ) ) ,
211
- [ .., 4 ] => Err ( PrincipalBufferError :: BufferTooLong ( ) ) ,
193
+ [ .., 4 ] => Err ( PrincipalError :: BufferTooLong ( ) ) ,
212
194
bytes @ [ ..] => match PrincipalInner :: try_from_slice ( bytes) {
213
- None => Err ( PrincipalBufferError :: BufferTooLong ( ) ) ,
195
+ None => Err ( PrincipalError :: BufferTooLong ( ) ) ,
214
196
Some ( v) => Ok ( Principal ( v) ) ,
215
197
} ,
216
198
}
@@ -235,7 +217,7 @@ impl Principal {
235
217
let expected = format ! ( "{}" , result) ;
236
218
237
219
if text. as_ref ( ) != expected {
238
- return Err ( PrincipalError :: AbnormalTextualFormat ( expected ) ) ;
220
+ return Err ( PrincipalError :: AbnormalTextualFormat ( result ) ) ;
239
221
}
240
222
Ok ( result)
241
223
}
@@ -302,15 +284,15 @@ impl TryFrom<&str> for Principal {
302
284
303
285
/// Vector TryFrom. The slice and array version of this trait are defined below.
304
286
impl TryFrom < Vec < u8 > > for Principal {
305
- type Error = PrincipalBufferError ;
287
+ type Error = PrincipalError ;
306
288
307
289
fn try_from ( bytes : Vec < u8 > ) -> Result < Self , Self :: Error > {
308
290
Self :: try_from ( bytes. as_slice ( ) )
309
291
}
310
292
}
311
293
312
294
impl TryFrom < & Vec < u8 > > for Principal {
313
- type Error = PrincipalBufferError ;
295
+ type Error = PrincipalError ;
314
296
315
297
fn try_from ( bytes : & Vec < u8 > ) -> Result < Self , Self :: Error > {
316
298
Self :: try_from ( bytes. as_slice ( ) )
@@ -319,7 +301,7 @@ impl TryFrom<&Vec<u8>> for Principal {
319
301
320
302
/// Implement try_from for a generic sized slice.
321
303
impl TryFrom < & [ u8 ] > for Principal {
322
- type Error = PrincipalBufferError ;
304
+ type Error = PrincipalError ;
323
305
324
306
fn try_from ( bytes : & [ u8 ] ) -> Result < Self , Self :: Error > {
325
307
Self :: try_from_slice ( bytes)
@@ -539,6 +521,14 @@ mod tests {
539
521
) ;
540
522
}
541
523
524
+ #[ test]
525
+ fn parse_text_bad_format ( ) {
526
+ assert_eq ! (
527
+ Principal :: from_str( "aaaaa-aA" ) . unwrap_err( ) . to_string( ) ,
528
+ r#"Invalid textual format: expected "aaaaa-aa""# ,
529
+ ) ;
530
+ }
531
+
542
532
#[ test]
543
533
fn parse_management_canister_to_text_ok ( ) {
544
534
assert_eq ! ( Principal :: from_str( "aaaaa-aa" ) . unwrap( ) . as_slice( ) , & [ ] ) ;
0 commit comments