@@ -109,8 +109,8 @@ pub mod v1 {
109
109
where
110
110
I : AsRef < [ u8 ] > + Into < Vec < u8 > > ,
111
111
{
112
- let input = input. as_ref ( ) ;
113
- let input_length = input . len ( ) ;
112
+ let input_ref = input. as_ref ( ) ;
113
+ let input_length = input_ref . len ( ) ;
114
114
if !( MINIMUM_ASSET_ID_LENGTH ..=MAXIMUM_ASSET_ID_LENGTH ) . contains ( & input_length) {
115
115
log:: trace!(
116
116
"Length of provided input {} is not included in the inclusive range [{},{}]" ,
@@ -122,12 +122,12 @@ pub mod v1 {
122
122
}
123
123
124
124
let AssetComponents {
125
- namespace,
126
- reference,
127
- identifier,
128
- } = split_components ( input ) ;
125
+ namespace : encoded_namespace ,
126
+ reference : encoded_reference ,
127
+ identifier : encoded_identifier ,
128
+ } = split_components ( input_ref ) ;
129
129
130
- match ( namespace , reference , identifier ) {
130
+ match ( encoded_namespace , encoded_reference , encoded_identifier ) {
131
131
// "slip44:" assets -> https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-20.md
132
132
( Some ( SLIP44_NAMESPACE ) , _, Some ( _) ) => {
133
133
log:: trace!( "Slip44 namespace does not accept an asset identifier." ) ;
@@ -145,27 +145,27 @@ pub mod v1 {
145
145
EvmSmartContractFungibleReference :: from_utf8_encoded ( erc20_reference) . map ( Self :: Erc20 )
146
146
}
147
147
// "erc721:" assets -> https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-22.md
148
- ( Some ( ERC721_NAMESPACE ) , Some ( erc721_reference) , identifier ) => {
148
+ ( Some ( ERC721_NAMESPACE ) , Some ( erc721_reference) , erc721_identifier ) => {
149
149
let reference = EvmSmartContractFungibleReference :: from_utf8_encoded ( erc721_reference) ?;
150
- let identifier = identifier . map_or ( Ok ( None ) , |id| {
150
+ let identifier = erc721_identifier . map_or ( Ok ( None ) , |id| {
151
151
EvmSmartContractNonFungibleIdentifier :: from_utf8_encoded ( id) . map ( Some )
152
152
} ) ?;
153
153
Ok ( Self :: Erc721 ( EvmSmartContractNonFungibleReference (
154
154
reference, identifier,
155
155
) ) )
156
156
}
157
157
// "erc1155:" assets-> https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-29.md
158
- ( Some ( ERC1155_NAMESPACE ) , Some ( erc1155_reference) , identifier ) => {
158
+ ( Some ( ERC1155_NAMESPACE ) , Some ( erc1155_reference) , erc1155_identifier ) => {
159
159
let reference = EvmSmartContractFungibleReference :: from_utf8_encoded ( erc1155_reference) ?;
160
- let identifier = identifier . map_or ( Ok ( None ) , |id| {
160
+ let identifier = erc1155_identifier . map_or ( Ok ( None ) , |id| {
161
161
EvmSmartContractNonFungibleIdentifier :: from_utf8_encoded ( id) . map ( Some )
162
162
} ) ?;
163
163
Ok ( Self :: Erc1155 ( EvmSmartContractNonFungibleReference (
164
164
reference, identifier,
165
165
) ) )
166
166
}
167
167
// Generic yet valid asset IDs
168
- _ => GenericAssetId :: from_utf8_encoded ( input ) . map ( Self :: Generic ) ,
168
+ _ => GenericAssetId :: from_utf8_encoded ( input_ref ) . map ( Self :: Generic ) ,
169
169
}
170
170
}
171
171
}
@@ -276,23 +276,20 @@ pub mod v1 {
276
276
/// Split the given input into its components, i.e., namespace, reference,
277
277
/// and identifier, if the proper separators are found.
278
278
fn split_components ( input : & [ u8 ] ) -> AssetComponents {
279
- let mut split = input. splitn ( 2 , |c| * c == ASSET_NAMESPACE_REFERENCE_SEPARATOR ) ;
280
- let ( namespace , reference) = ( split . next ( ) , split . next ( ) ) ;
279
+ let mut namespace_reference_split = input. splitn ( 2 , |c| * c == ASSET_NAMESPACE_REFERENCE_SEPARATOR ) ;
280
+ let ( parsed_namespace , reference) = ( namespace_reference_split . next ( ) , namespace_reference_split . next ( ) ) ;
281
281
282
282
// Split the remaining reference to extract the identifier, if present
283
- let ( reference , identifier ) = if let Some ( r ) = reference {
284
- let mut split = r. splitn ( 2 , |c| * c == ASSET_REFERENCE_IDENTIFIER_SEPARATOR ) ;
283
+ let ( parsed_reference , parsed_identifier ) = reference . map_or ( ( reference , None ) , |r| {
284
+ let mut reference_identifier_split = r. splitn ( 2 , |c| * c == ASSET_REFERENCE_IDENTIFIER_SEPARATOR ) ;
285
285
// Split the reference further, if present
286
- ( split. next ( ) , split. next ( ) )
287
- } else {
288
- // Return the old reference, which is None if we are at this point
289
- ( reference, None )
290
- } ;
286
+ ( reference_identifier_split. next ( ) , reference_identifier_split. next ( ) )
287
+ } ) ;
291
288
292
289
AssetComponents {
293
- namespace,
294
- reference,
295
- identifier,
290
+ namespace : parsed_namespace ,
291
+ reference : parsed_reference ,
292
+ identifier : parsed_identifier ,
296
293
}
297
294
}
298
295
@@ -315,10 +312,10 @@ pub mod v1 {
315
312
where
316
313
I : AsRef < [ u8 ] > + Into < Vec < u8 > > ,
317
314
{
318
- let input = input. as_ref ( ) ;
319
- check_reference_length_bounds ( input ) ?;
315
+ let input_ref = input. as_ref ( ) ;
316
+ check_reference_length_bounds ( input_ref ) ?;
320
317
321
- let decoded = str:: from_utf8 ( input ) . map_err ( |_| {
318
+ let decoded = str:: from_utf8 ( input_ref ) . map_err ( |_| {
322
319
log:: trace!( "Provided input is not a valid UTF8 string as expected by a Slip44 reference." ) ;
323
320
ReferenceError :: InvalidFormat
324
321
} ) ?;
@@ -359,7 +356,7 @@ pub mod v1 {
359
356
360
357
// Getters
361
358
impl Slip44Reference {
362
- pub fn inner ( & self ) -> & U256 {
359
+ pub const fn inner ( & self ) -> & U256 {
363
360
& self . 0
364
361
}
365
362
}
@@ -383,9 +380,9 @@ pub mod v1 {
383
380
where
384
381
I : AsRef < [ u8 ] > + Into < Vec < u8 > > ,
385
382
{
386
- let input = input. as_ref ( ) ;
383
+ let input_ref = input. as_ref ( ) ;
387
384
// If the prefix is "0x" => parse the address
388
- if let [ b'0' , b'x' , contract_address @ ..] = input {
385
+ if let [ b'0' , b'x' , contract_address @ ..] = input_ref {
389
386
check_reference_length_bounds ( contract_address) ?;
390
387
391
388
let decoded = hex:: decode ( contract_address) . map_err ( |_| {
@@ -407,7 +404,7 @@ pub mod v1 {
407
404
408
405
// Getters
409
406
impl EvmSmartContractFungibleReference {
410
- pub fn inner ( & self ) -> & [ u8 ] {
407
+ pub const fn inner ( & self ) -> & [ u8 ] {
411
408
& self . 0
412
409
}
413
410
}
@@ -431,11 +428,11 @@ pub mod v1 {
431
428
432
429
// Getters
433
430
impl EvmSmartContractNonFungibleReference {
434
- pub fn smart_contract ( & self ) -> & EvmSmartContractFungibleReference {
431
+ pub const fn smart_contract ( & self ) -> & EvmSmartContractFungibleReference {
435
432
& self . 0
436
433
}
437
434
438
- pub fn identifier ( & self ) -> & Option < EvmSmartContractNonFungibleIdentifier > {
435
+ pub const fn identifier ( & self ) -> & Option < EvmSmartContractNonFungibleIdentifier > {
439
436
& self . 1
440
437
}
441
438
}
@@ -457,10 +454,10 @@ pub mod v1 {
457
454
where
458
455
I : AsRef < [ u8 ] > + Into < Vec < u8 > > ,
459
456
{
460
- let input = input. as_ref ( ) ;
461
- check_identifier_length_bounds ( input ) ?;
457
+ let input_ref = input. as_ref ( ) ;
458
+ check_identifier_length_bounds ( input_ref ) ?;
462
459
463
- input . iter ( ) . try_for_each ( |c| {
460
+ input_ref . iter ( ) . try_for_each ( |c| {
464
461
if !c. is_ascii_digit ( ) {
465
462
log:: trace!( "Provided input has some invalid values as expected by a smart contract-based asset identifier." ) ;
466
463
Err ( IdentifierError :: InvalidFormat )
@@ -470,7 +467,7 @@ pub mod v1 {
470
467
} ) ?;
471
468
472
469
Ok ( Self (
473
- Vec :: < u8 > :: from ( input )
470
+ Vec :: < u8 > :: from ( input_ref )
474
471
. try_into ( )
475
472
. map_err ( |_| IdentifierError :: InvalidFormat ) ?,
476
473
) )
@@ -519,12 +516,13 @@ pub mod v1 {
519
516
} = split_components ( input. as_ref ( ) ) ;
520
517
521
518
match ( namespace, reference, identifier) {
522
- ( Some ( namespace ) , Some ( reference ) , identifier ) => Ok ( Self {
523
- namespace : GenericAssetNamespace :: from_utf8_encoded ( namespace ) ?,
524
- reference : GenericAssetReference :: from_utf8_encoded ( reference ) ?,
519
+ ( Some ( encoded_namespace ) , Some ( encoded_reference ) , encoded_identifier ) => Ok ( Self {
520
+ namespace : GenericAssetNamespace :: from_utf8_encoded ( encoded_namespace ) ?,
521
+ reference : GenericAssetReference :: from_utf8_encoded ( encoded_reference ) ?,
525
522
// Transform Option<Result> to Result<Option> and bubble Err case up, keeping Ok(Option) for
526
523
// successful cases.
527
- id : identifier. map_or ( Ok ( None ) , |id| GenericAssetIdentifier :: from_utf8_encoded ( id) . map ( Some ) ) ?,
524
+ id : encoded_identifier
525
+ . map_or ( Ok ( None ) , |id| GenericAssetIdentifier :: from_utf8_encoded ( id) . map ( Some ) ) ?,
528
526
} ) ,
529
527
_ => Err ( Error :: InvalidFormat ) ,
530
528
}
@@ -533,13 +531,13 @@ pub mod v1 {
533
531
534
532
// Getters
535
533
impl GenericAssetId {
536
- pub fn namespace ( & self ) -> & GenericAssetNamespace {
534
+ pub const fn namespace ( & self ) -> & GenericAssetNamespace {
537
535
& self . namespace
538
536
}
539
- pub fn reference ( & self ) -> & GenericAssetReference {
537
+ pub const fn reference ( & self ) -> & GenericAssetReference {
540
538
& self . reference
541
539
}
542
- pub fn id ( & self ) -> & Option < GenericAssetIdentifier > {
540
+ pub const fn id ( & self ) -> & Option < GenericAssetIdentifier > {
543
541
& self . id
544
542
}
545
543
}
@@ -557,19 +555,19 @@ pub mod v1 {
557
555
where
558
556
I : AsRef < [ u8 ] > + Into < Vec < u8 > > ,
559
557
{
560
- let input = input. as_ref ( ) ;
561
- check_namespace_length_bounds ( input ) ?;
558
+ let input_ref = input. as_ref ( ) ;
559
+ check_namespace_length_bounds ( input_ref ) ?;
562
560
563
- input . iter ( ) . try_for_each ( |c| {
564
- if !matches ! ( c, b'-' | b'a' ..=b'z' | b'0' ..=b'9' ) {
561
+ input_ref . iter ( ) . try_for_each ( |c| {
562
+ if !matches ! ( * c, b'-' | b'a' ..=b'z' | b'0' ..=b'9' ) {
565
563
log:: trace!( "Provided input has some invalid values as expected by a generic asset namespace." ) ;
566
564
Err ( NamespaceError :: InvalidFormat )
567
565
} else {
568
566
Ok ( ( ) )
569
567
}
570
568
} ) ?;
571
569
Ok ( Self (
572
- Vec :: < u8 > :: from ( input )
570
+ Vec :: < u8 > :: from ( input_ref )
573
571
. try_into ( )
574
572
. map_err ( |_| NamespaceError :: InvalidFormat ) ?,
575
573
) )
@@ -607,19 +605,19 @@ pub mod v1 {
607
605
where
608
606
I : AsRef < [ u8 ] > + Into < Vec < u8 > > ,
609
607
{
610
- let input = input. as_ref ( ) ;
611
- check_reference_length_bounds ( input ) ?;
608
+ let input_ref = input. as_ref ( ) ;
609
+ check_reference_length_bounds ( input_ref ) ?;
612
610
613
- input . iter ( ) . try_for_each ( |c| {
614
- if !matches ! ( c, b'-' | b'.' | b'%' | b'a' ..=b'z' | b'A' ..=b'Z' | b'0' ..=b'9' ) {
611
+ input_ref . iter ( ) . try_for_each ( |c| {
612
+ if !matches ! ( * c, b'-' | b'.' | b'%' | b'a' ..=b'z' | b'A' ..=b'Z' | b'0' ..=b'9' ) {
615
613
log:: trace!( "Provided input has some invalid values as expected by a generic asset reference." ) ;
616
614
Err ( ReferenceError :: InvalidFormat )
617
615
} else {
618
616
Ok ( ( ) )
619
617
}
620
618
} ) ?;
621
619
Ok ( Self (
622
- Vec :: < u8 > :: from ( input )
620
+ Vec :: < u8 > :: from ( input_ref )
623
621
. try_into ( )
624
622
. map_err ( |_| ReferenceError :: InvalidFormat ) ?,
625
623
) )
@@ -657,19 +655,19 @@ pub mod v1 {
657
655
where
658
656
I : AsRef < [ u8 ] > + Into < Vec < u8 > > ,
659
657
{
660
- let input = input. as_ref ( ) ;
661
- check_identifier_length_bounds ( input ) ?;
658
+ let input_ref = input. as_ref ( ) ;
659
+ check_identifier_length_bounds ( input_ref ) ?;
662
660
663
- input . iter ( ) . try_for_each ( |c| {
664
- if !matches ! ( c, b'-' | b'.' | b'%' | b'a' ..=b'z' | b'A' ..=b'Z' | b'0' ..=b'9' ) {
661
+ input_ref . iter ( ) . try_for_each ( |c| {
662
+ if !matches ! ( * c, b'-' | b'.' | b'%' | b'a' ..=b'z' | b'A' ..=b'Z' | b'0' ..=b'9' ) {
665
663
log:: trace!( "Provided input has some invalid values as expected by a generic asset identifier." ) ;
666
664
Err ( IdentifierError :: InvalidFormat )
667
665
} else {
668
666
Ok ( ( ) )
669
667
}
670
668
} ) ?;
671
669
Ok ( Self (
672
- Vec :: < u8 > :: from ( input )
670
+ Vec :: < u8 > :: from ( input_ref )
673
671
. try_into ( )
674
672
. map_err ( |_| IdentifierError :: InvalidFormat ) ?,
675
673
) )
0 commit comments