File: der_sequence_default.rs

package info (click to toggle)
rust-asn1-rs 0.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 996 kB
  • sloc: makefile: 13; sh: 1
file content (45 lines) | stat: -rw-r--r-- 1,130 bytes parent folder | download
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
use asn1_rs::*;
use hex_literal::hex;

#[derive(Debug, PartialEq, DerSequence)]
// #[debug_derive]
pub struct T0 {
    #[optional]
    #[default(0)]
    a: u16,
}

#[derive(Debug, PartialEq, DerSequence)]
// #[debug_derive]
pub struct T1 {
    #[tag_explicit(0)]
    #[optional]
    #[default(0)]
    a: u16,
}

fn main() {
    // optional value present
    let input1 = &hex!("3003 020103");
    let (rem, t0) = T0::from_der(input1).expect("parsing failed");
    assert!(rem.is_empty());
    assert_eq!(t0, T0 { a: 3 });

    // optional value absent
    let input_empty = &hex!("3000");
    let (rem, t1) = T0::from_der(input_empty).expect("parsing failed");
    assert!(rem.is_empty());
    assert_eq!(t1, T0 { a: 0 });

    // optional value present
    let input1 = &hex!("3005 a003 020103");
    let (rem, t0) = T1::from_der(input1).expect("parsing failed");
    assert!(rem.is_empty());
    assert_eq!(t0, T1 { a: 3 });

    // optional value absent
    let input_empty = &hex!("3000");
    let (rem, t1) = T1::from_der(input_empty).expect("parsing failed");
    assert!(rem.is_empty());
    assert_eq!(t1, T1 { a: 0 });
}