@@ -9,6 +9,23 @@ pub struct Sequence {
9
9
b : bool ,
10
10
}
11
11
12
+ #[ derive( AsnType , Decode , Debug , Encode , PartialEq ) ]
13
+ pub struct SequenceWithDefault {
14
+ #[ rasn( tag( explicit( application, 1 ) ) , default = "default_bool" ) ]
15
+ b : bool ,
16
+ }
17
+
18
+ #[ derive( AsnType , Decode , Debug , Encode , PartialEq ) ]
19
+ pub struct SequenceWithMultiDefault {
20
+ #[ rasn( tag( explicit( application, 1 ) ) , default = "default_bool" ) ]
21
+ b : bool ,
22
+ #[ rasn( tag( explicit( application, 2 ) ) , default = "default_bool" ) ]
23
+ b2 : bool ,
24
+ }
25
+ pub fn default_bool ( ) -> bool {
26
+ true
27
+ }
28
+
12
29
#[ derive( AsnType , Decode , Debug , Encode , PartialEq ) ]
13
30
#[ rasn( tag( explicit( application, 1 ) ) ) ]
14
31
pub struct InlineSequence {
@@ -67,6 +84,18 @@ const _: () = assert!(Tag::const_eq(DelegateSequence::TAG, &InlineSet::TAG,));
67
84
#[ test]
68
85
fn works ( ) {
69
86
const EXPECTED : & [ u8 ] = & [ 0x61 , 0x5 , 0x30 , 0x3 , 0x01 , 0x1 , 0xFF ] ;
87
+ // Note that the explicitly tagged field is dropped.
88
+ // This makes it a sequence with 0 elements
89
+ const EXPECTED_DEFAULT : & [ u8 ] = & [ 0x30 , 0x00 ] ;
90
+ const EXPECTED_NOT_DEFAULT : & [ u8 ] = & [ 0x30 , 0x05 , 0x61 , 0x03 , 0x01 , 0x01 , 0x00 ] ;
91
+ // NOTE: The explicit tag number is just different
92
+ const EXPECTED_MULTI_DEFAULT : & [ u8 ] = & [ 0x30 , 0x05 , 0x62 , 0x03 , 0x01 , 0x01 , 0x00 ] ;
93
+ // This version of the output is what would be generated without a default check.
94
+ // Using this for backward compat. verification
95
+ const EXPECTED_MULTI_DEFAULT_FIELD_ENCODED : & [ u8 ] = & [
96
+ 0x30 , 0x0a , 0x61 , 0x03 , 0x01 , 0x01 , 0xff , 0x62 , 0x03 , 0x01 , 0x01 , 0x00 ,
97
+ ] ;
98
+
70
99
let delegate_seq = DelegateSequence ( Sequence { b : true } ) ;
71
100
let inline_seq = InlineSequence { b : true } ;
72
101
let field_seq = SequenceField { b : true } ;
@@ -84,6 +113,26 @@ fn works() {
84
113
let inline_choice_enc = rasn:: der:: encode ( & inline_choice) . unwrap ( ) ;
85
114
let wrapped_choice_enc = rasn:: der:: encode ( & wrapped_choice) . unwrap ( ) ;
86
115
116
+ // Set the field to match the default value to have it dropped
117
+ let sequence_default = SequenceWithDefault { b : true } ;
118
+ let sequence_non_default = SequenceWithDefault { b : false } ;
119
+ let sequence_default_enc = rasn:: der:: encode ( & sequence_default) . unwrap ( ) ;
120
+ let sequence_non_default_enc = rasn:: der:: encode ( & sequence_non_default) . unwrap ( ) ;
121
+ // Verify it correctly includes encoded fields
122
+ let sequence_multi_default = SequenceWithMultiDefault { b : true , b2 : false } ;
123
+ let sequence_multi_default_enc = rasn:: der:: encode ( & sequence_multi_default) . unwrap ( ) ;
124
+
125
+ assert_eq ! ( sequence_non_default_enc, EXPECTED_NOT_DEFAULT ) ;
126
+ assert_eq ! ( sequence_default_enc, EXPECTED_DEFAULT ) ;
127
+ assert_eq ! ( sequence_multi_default_enc, EXPECTED_MULTI_DEFAULT ) ;
128
+ assert_eq ! (
129
+ sequence_multi_default,
130
+ rasn:: der:: decode( & sequence_multi_default_enc) . unwrap( )
131
+ ) ;
132
+ assert_eq ! (
133
+ sequence_multi_default,
134
+ rasn:: der:: decode( EXPECTED_MULTI_DEFAULT_FIELD_ENCODED ) . unwrap( )
135
+ ) ;
87
136
assert_eq ! ( delegate_seq_enc, EXPECTED ) ;
88
137
assert_eq ! ( inline_seq_enc, EXPECTED ) ;
89
138
assert_eq ! ( field_seq_enc, EXPECTED ) ;
0 commit comments