rasn 0.28.12

A safe no_std ASN.1 codec framework.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! Implementation of ASN.1 traits for `serde_json::Value`.
//!
//! This module provides `AsnType`, `Encode`, `Decode`, `Choice`, and `DecodeChoice`
//! implementations for `serde_json::Value`, treating it as an ASN.1 CHOICE type.
//!
//! The mapping is:
//! - `Value::Null` -> NULL (tag 5)
//! - `Value::Bool` -> BOOLEAN (tag 1)
//! - `Value::Number` -> INTEGER (tag 2) for integers, or UTF8String for decimals
//! - `Value::String` -> UTF8String (tag 12)
//! - `Value::Array` -> SEQUENCE OF (context tag 1) containing recursive Values
//! - `Value::Object` -> SEQUENCE OF (context tag 0) key-value pairs

use alloc::string::{String, ToString};
use alloc::vec::Vec;

use crate::jer::enc::ValueMap;
use crate::{
    AsnType, Decode, Encode,
    de::{Decoder, Error as DecodeError},
    enc::Encoder,
    types::{
        Constraints, Identifier, Tag, TagTree,
        constraints::{Bounded, Constraint, Extensible, Value as ValueConstraint},
        fields::{Field, Fields},
    },
};
use serde_json::{Number, Value};

// Context tags for distinguishing variants that would otherwise share tags
const TAG_OBJECT: Tag = Tag::new(crate::types::Class::Context, 0);
const TAG_ARRAY: Tag = Tag::new(crate::types::Class::Context, 1);
const TAG_DECIMAL: Tag = Tag::new(crate::types::Class::Context, 2); // For non-integer numbers
const CHOICE_LIST: [TagTree; 7] = [
    TagTree::Leaf(Tag::NULL),        // Null
    TagTree::Leaf(Tag::BOOL),        // Bool
    TagTree::Leaf(Tag::INTEGER),     // Number (integer)
    TagTree::Leaf(TAG_DECIMAL),      // Number (decimal/float)
    TagTree::Leaf(Tag::UTF8_STRING), // String
    TagTree::Leaf(TAG_ARRAY),        // Array
    TagTree::Leaf(TAG_OBJECT),       // Object
];
/// Number of variants in the Value CHOICE type (Null, Bool, Integer, Decimal, String, Array, Object)
const VARIANT_COUNT: usize = 7;

impl AsnType for Value {
    const TAG: Tag = Tag::EOC; // CHOICE types use EOC
    const TAG_TREE: TagTree = TagTree::Choice(&CHOICE_LIST);
    const IS_CHOICE: bool = true;
}

impl crate::types::Choice for Value {
    const VARIANTS: &'static [TagTree] = &CHOICE_LIST;

    const VARIANCE_CONSTRAINT: Constraints =
        Constraints::new(&[Constraint::Value(Extensible::new(ValueConstraint::new(
            Bounded::const_new(0, (VARIANT_COUNT - 1) as i128),
        )))]);

    const IDENTIFIERS: &'static [&'static str] = &[
        "null", "bool", "integer", "decimal", "string", "array", "object",
    ];
}

impl crate::types::DecodeChoice for Value {
    fn from_tag<D: Decoder>(decoder: &mut D, tag: Tag) -> Result<Self, D::Error> {
        match tag {
            Tag::NULL => {
                decoder.decode_null(Tag::NULL)?;
                Ok(Value::Null)
            }
            Tag::BOOL => {
                let b = decoder.decode_bool(Tag::BOOL)?;
                Ok(Value::Bool(b))
            }
            Tag::INTEGER => {
                let i: i64 = decoder.decode_integer(Tag::INTEGER, Constraints::default())?;
                Ok(Value::Number(Number::from(i)))
            }
            t if t == TAG_DECIMAL => {
                // Decimal numbers are encoded as UTF8String with context tag
                let s = decoder.decode_utf8_string(TAG_DECIMAL, Constraints::default())?;

                if let Some(num) = s.parse::<f64>().ok().and_then(Number::from_f64) {
                    return Ok(Value::Number(num));
                }
                // Fallback: return as-is if parsing fails (shouldn't happen)
                Err(DecodeError::custom(
                    alloc::format!("Failed to parse decimal number: {s}"),
                    decoder.codec(),
                ))
            }
            Tag::UTF8_STRING => {
                let s = decoder.decode_utf8_string(Tag::UTF8_STRING, Constraints::default())?;
                Ok(Value::String(s))
            }
            t if t == TAG_ARRAY => {
                let arr: Vec<Value> =
                    decoder.decode_sequence_of(TAG_ARRAY, Constraints::default())?;
                Ok(Value::Array(arr))
            }
            t if t == TAG_OBJECT => {
                let map: ValueMap = ValueMap::decode_with_tag_and_constraints(
                    decoder,
                    TAG_OBJECT,
                    Constraints::default(),
                )?;
                Ok(Value::Object(map))
            }
            _ => Err(DecodeError::no_valid_choice(
                "serde_json::Value",
                decoder.codec(),
            )),
        }
    }
}

impl Encode for Value {
    fn encode<'b, E: Encoder<'b>>(&self, encoder: &mut E) -> Result<(), E::Error> {
        // For CHOICE types, we use encode_choice
        let tag = value_to_tag(self);
        encoder.encode_choice::<Self>(
            Self::CONSTRAINTS,
            tag,
            |enc| {
                match self {
                    Value::Null => {
                        enc.encode_null(Tag::NULL, Identifier::EMPTY)?;
                    }
                    Value::Bool(b) => {
                        enc.encode_bool(Tag::BOOL, *b, Identifier::EMPTY)?;
                    }
                    Value::Number(n) => {
                        // Try to encode as integer if possible
                        if let Some(i) = n.as_i64() {
                            enc.encode_integer(
                                Tag::INTEGER,
                                Constraints::default(),
                                &i,
                                Identifier::EMPTY,
                            )?;
                        } else {
                            // For floats and large numbers, encode as UTF8String with TAG_DECIMAL
                            let s = n.to_string();
                            enc.encode_utf8_string(
                                TAG_DECIMAL,
                                Constraints::default(),
                                &s,
                                Identifier::EMPTY,
                            )?;
                        }
                    }
                    Value::String(s) => {
                        enc.encode_utf8_string(
                            Tag::UTF8_STRING,
                            Constraints::default(),
                            s,
                            Identifier::EMPTY,
                        )?;
                    }
                    Value::Array(arr) => {
                        enc.encode_sequence_of(
                            TAG_ARRAY,
                            arr,
                            Constraints::default(),
                            Identifier::EMPTY,
                        )?;
                    }
                    Value::Object(map) => {
                        map.encode_with_tag_and_constraints(
                            enc,
                            TAG_OBJECT,
                            Constraints::default(),
                            Identifier::EMPTY,
                        )?;
                    }
                }
                Ok(tag)
            },
            Self::IDENTIFIER,
        )?;
        Ok(())
    }

    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
        &self,
        encoder: &mut E,
        _tag: Tag,
        _constraints: Constraints,
        _identifier: Identifier,
    ) -> Result<(), E::Error> {
        // CHOICE types ignore the outer tag - they use their variant's tag
        self.encode(encoder)
    }
}

impl Decode for Value {
    fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, D::Error> {
        decoder.decode_choice::<Self>(Constraints::default())
    }

    fn decode_with_tag_and_constraints<D: Decoder>(
        decoder: &mut D,
        _tag: Tag,
        _constraints: Constraints,
    ) -> Result<Self, D::Error> {
        // CHOICE types ignore the outer tag
        Self::decode(decoder)
    }
}

/// Helper function to determine the tag for a Value variant
fn value_to_tag(value: &Value) -> Tag {
    match value {
        Value::Null => Tag::NULL,
        Value::Bool(_) => Tag::BOOL,
        Value::Number(n) => {
            if n.as_i64().is_some() {
                Tag::INTEGER
            } else {
                TAG_DECIMAL
            }
        }
        Value::String(_) => Tag::UTF8_STRING,
        Value::Array(_) => TAG_ARRAY,
        Value::Object(_) => TAG_OBJECT,
    }
}

// ASN.1 trait implementations for ValueMap (Map<String, Value>)

impl AsnType for ValueMap {
    const TAG: Tag = TAG_OBJECT;
}

/// Helper type for encoding a single JSON object key-value entry.
/// Used internally by `ValueMap` encoding.
#[derive(Clone, Debug, PartialEq)]
struct ValueEntry<'a> {
    key: &'a str,
    value: &'a Value,
}

impl AsnType for ValueEntry<'_> {
    const TAG: Tag = Tag::SEQUENCE;
}

/// Owned version of ValueEntry for decoding.
#[derive(Clone, Debug, PartialEq)]
struct OwnedValueEntry {
    key: String,
    value: Value,
}

impl AsnType for OwnedValueEntry {
    const TAG: Tag = Tag::SEQUENCE;
}

impl crate::types::Constructed<2, 0> for OwnedValueEntry {
    const FIELDS: Fields<2> = Fields::from_static([
        Field::new_required(0, Tag::UTF8_STRING, TagTree::Leaf(Tag::UTF8_STRING), "key"),
        Field::new_required(1, Tag::EOC, Value::TAG_TREE, "value"),
    ]);
}

impl Encode for ValueEntry<'_> {
    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
        &self,
        encoder: &mut E,
        tag: Tag,
        _constraints: Constraints,
        identifier: Identifier,
    ) -> Result<(), E::Error> {
        encoder.encode_sequence::<2, 0, OwnedValueEntry, _>(
            tag,
            |enc| {
                enc.encode_utf8_string(
                    Tag::UTF8_STRING,
                    Constraints::default(),
                    self.key,
                    Identifier::EMPTY,
                )?;
                self.value.encode(enc)?;
                Ok(())
            },
            identifier,
        )?;
        Ok(())
    }
}

impl Decode for OwnedValueEntry {
    fn decode_with_tag_and_constraints<D: Decoder>(
        decoder: &mut D,
        tag: Tag,
        _constraints: Constraints,
    ) -> Result<Self, D::Error> {
        decoder.decode_sequence::<2, 0, Self, _, _>(tag, None::<fn() -> Self>, |dec| {
            let key = dec.decode_utf8_string(Tag::UTF8_STRING, Constraints::default())?;
            let value = Value::decode(dec)?;
            Ok(OwnedValueEntry { key, value })
        })
    }
}

impl Encode for ValueMap {
    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
        &self,
        encoder: &mut E,
        tag: Tag,
        constraints: Constraints,
        identifier: Identifier,
    ) -> Result<(), E::Error> {
        let entries: Vec<ValueEntry<'_>> = self
            .iter()
            .map(|(k, v)| ValueEntry { key: k, value: v })
            .collect();
        encoder.encode_sequence_of(tag, &entries, constraints, identifier)?;
        Ok(())
    }
}

impl Decode for ValueMap {
    fn decode_with_tag_and_constraints<D: Decoder>(
        decoder: &mut D,
        tag: Tag,
        constraints: Constraints,
    ) -> Result<Self, D::Error> {
        let entries: Vec<OwnedValueEntry> = decoder.decode_sequence_of(tag, constraints)?;
        Ok(entries.into_iter().map(|e| (e.key, e.value)).collect())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn round_trip_der(value: &Value) {
        let encoded = crate::der::encode(value).expect("DER encode failed");
        let decoded: Value = crate::der::decode(&encoded).expect("DER decode failed");
        assert_eq!(value, &decoded);
    }

    #[test]
    fn test_integer() {
        round_trip_der(&json!(0));
        round_trip_der(&json!(42));
        round_trip_der(&json!(-17));
        round_trip_der(&json!(i64::MAX));
        round_trip_der(&json!(i64::MIN));
    }

    #[test]
    fn test_string() {
        round_trip_der(&json!(""));
        round_trip_der(&json!("hello"));
        round_trip_der(&json!("with \"quotes\""));
        round_trip_der(&json!("unicode: 你好"));
        round_trip_der(&json!("42"));
    }

    #[test]
    fn test_array() {
        round_trip_der(&json!([]));
        round_trip_der(&json!([1, 2, 3]));
        round_trip_der(&json!([null, true, "hello"]));
        round_trip_der(&json!([[1, 2], [3, 4]]));
        // Large array test
        let arr: Vec<i32> = (0..100).collect();
        let value = json!(arr);
        round_trip_der(&value);
    }

    #[test]
    fn test_object() {
        round_trip_der(&json!({}));
        round_trip_der(&json!({"key": "value"}));
        round_trip_der(&json!({"a": 1, "b": 2}));
        round_trip_der(&json!({"nested": {"inner": true}}));
    }

    #[test]
    fn test_complex_structure() {
        let value = json!({
            "users": [
                {"id": 1, "name": "Alice", "active": true},
                {"id": 2, "name": "Bob", "active": false}
            ],
            "metadata": {
                "version": "1.0",
                "count": 2
            },
            "tags": ["important", "reviewed"],
            "notes": null
        });
        round_trip_der(&value);
    }
    // ============================================================
    // Test cases adapted from serde-json-canonicalizer https://github.com/evik42/serde-json-canonicalizer
    // ============================================================

    // Basic types tests (from basic_types.rs)
    #[test]
    fn test_literals() {
        // null, true, false
        round_trip_der(&json!(null));
        round_trip_der(&json!(true));
        round_trip_der(&json!(false));
    }

    #[test]
    fn test_number() {
        round_trip_der(&json!(42));
        round_trip_der(&json!(0));
        // JavaScript's MAX_SAFE_INTEGER: 2^53 - 1 = 9007199254740991
        round_trip_der(&json!(9007199254740991i64));
        round_trip_der(&json!(-9007199254740991i64));

        // u8 max
        round_trip_der(&json!(255));
        // i8 range
        round_trip_der(&json!(127));
        round_trip_der(&json!(-128));
        // u16 max
        round_trip_der(&json!(65535));
        // i16 range
        round_trip_der(&json!(32767));
        round_trip_der(&json!(-32768));
        // u32 max
        round_trip_der(&json!(4294967295u64));
        // i32 range
        round_trip_der(&json!(2147483647));
        round_trip_der(&json!(-2147483648i64));
    }

    #[test]
    fn test_basic_string_number() {}

    #[test]
    fn test_empty() {
        // empty array
        round_trip_der(&json!([]));
        // empty object
        round_trip_der(&json!({}));
        // empty key
        round_trip_der(&json!({"": "empty"}));
    }

    // Unicode and special character tests (adapted from weird.rs and unicode tests)
    #[test]
    fn test_unicode() {
        round_trip_der(&json!({"": "Euro Sign"}));
        // unicode_control_char
        round_trip_der(&json!({"\u{0080}": "Control\u{007f}"}));
        round_trip_der(&json!({"😂": "Smiley"}));
        round_trip_der(&json!({"ö": "Latin Small Letter O With Diaeresis"}));
        round_trip_der(&json!({"\u{fb33}": "Hebrew Letter Dalet With Dagesh"}));
        // A with combining ring above (unnormalized form of Å)
        round_trip_der(&json!({"Unnormalized Unicode": "A\u{030a}"}));
        // String with various escape sequences
        round_trip_der(&json!({ "string": "€$\u{000f}\nA'B\"\\\\\"/", }));
    }

    #[test]
    fn test_separators() {
        round_trip_der(&json!({"\r": "Carriage Return"}));
        round_trip_der(&json!({"\n": "Newline"}));
        round_trip_der(&json!({"</script>": "Browser Challenge"}));
        round_trip_der(&json!("column1\tcolumn2"));
        round_trip_der(&json!("path\\to\\file"));
        round_trip_der(&json!("before\u{0000}after"));
    }

    // Array tests (from arrays.json)
    #[test]
    fn test_nested() {
        let value = json!([
            56,
            {
                "d": true,
                "10": null,
                "1": []
            }
        ]);
        round_trip_der(&value);
        let value = json!({
            "1": {"f": {"f": "hi", "F": 5}, "\n": 56},
            "10": {},
            "": "empty",
            "a": {},
            "111": [{"e": "yes", "E": "no"}],
            "A": {}
        });
        round_trip_der(&value);

        // deeply nested object
        let value = json!([[[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]]]);
        round_trip_der(&value);
        // deeply nested array
        let value = json!({
            "a": {
                "b": {
                    "c": {
                        "d": {
                            "e": "deep"
                        }
                    }
                }
            }
        });
        round_trip_der(&value);

        // Mixed content tests
        let value = json!({
            "null_val": null,
            "bool_true": true,
            "bool_false": false,
            "int_pos": 42,
            "int_neg": -17,
            "int_zero": 0,
            "string": "hello world",
            "string_empty": "",
            "string_unicode": "こんにちは",
            "array_empty": [],
            "array_mixed": [1, "two", true, null],
            "object_empty": {},
            "object_nested": {"inner": {"value": 123}}
        });
        round_trip_der(&value);
    }

    // Multiple codecs comprehensive test
    #[test]
    fn test_all_codecs() {
        let value = json!({
            "nested": {"key": "value", "array": [1, 2, 3], "a_key": "value"},
            "test": "value",
            "number": 42,
            "array": [1, 2, 3],
        });
        let value2 = json!({
            "array": [1, 2, 3],
            "nested": {"array": [1, 2, 3], "a_key": "value", "key": "value"},
            "number": 42,
            "test": "value",
        });
        let value3 = json!({
            "array": [1, 2, 3],
            "nested": {"a_key": "value", "array": [1, 2, 3], "key": "value"},
            "test": "value",
            "number": 42,
        });

        // DER canonical
        let der_enc = crate::der::encode(&value).unwrap();
        let der_enc2 = crate::der::encode(&value2).unwrap();
        let der_enc3 = crate::der::encode(&value3).unwrap();
        assert_eq!(der_enc, der_enc2);
        assert_eq!(der_enc, der_enc3);
        let der_dec: Value = crate::der::decode(&der_enc).expect("DER decode failed");
        assert_eq!(value, der_dec);

        // BER non canonical
        let ber_enc = crate::ber::encode(&value).unwrap();
        let ber_dec: Value = crate::ber::decode(&ber_enc).expect("BER decode failed");
        assert_eq!(value, ber_dec);

        // OER
        let oer_enc = crate::oer::encode(&value).unwrap();
        let oer_enc2 = crate::oer::encode(&value2).unwrap();
        let oer_enc3 = crate::oer::encode(&value3).unwrap();
        assert_eq!(oer_enc, oer_enc2);
        assert_eq!(oer_enc, oer_enc3);
        let oer_dec: Value = crate::oer::decode(&oer_enc).expect("OER decode failed");
        assert_eq!(value, oer_dec);

        // COER
        let coer_enc = crate::coer::encode(&value).unwrap();
        let coer_enc2 = crate::coer::encode(&value2).unwrap();
        let coer_enc3 = crate::coer::encode(&value3).unwrap();
        assert_eq!(coer_enc, coer_enc2);
        assert_eq!(coer_enc, coer_enc3);
        let coer_dec: Value = crate::coer::decode(&coer_enc).expect("COER decode failed");
        assert_eq!(value, coer_dec);

        // UPER
        let uper_enc = crate::uper::encode(&value).unwrap();
        let uper_enc2 = crate::uper::encode(&value2).unwrap();
        let uper_enc3 = crate::uper::encode(&value3).unwrap();
        assert_eq!(uper_enc, uper_enc2);
        assert_eq!(uper_enc, uper_enc3);
        let uper_dec: Value = crate::uper::decode(&uper_enc).expect("UPER decode failed");
        assert_eq!(value, uper_dec);

        // APER is not designed to be canonical
        let aper_enc = crate::aper::encode(&value).unwrap();
        let aper_dec: Value = crate::aper::decode(&aper_enc).expect("APER decode failed");
        assert_eq!(value, aper_dec);
    }
}