forked from kellerkindt/asn1rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
showcase.rs
92 lines (77 loc) · 2.05 KB
/
showcase.rs
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
use asn1rs::prelude::*;
asn_to_rust!(
r#"BasicSchema DEFINITIONS AUTOMATIC TAGS ::=
BEGIN
Pizza ::= SEQUENCE {
price INTEGER,
size INTEGER(1..4),
note UTF8String OPTIONAL
}
Topping ::= ENUMERATED {
not-pineapple,
even-less-pineapple,
no-pineapple-at-all
}
Custom ::= UTF8String
WhatToEat ::= CHOICE {
pizza Pizza,
custom Custom
}
END"#
);
// This module contains the same content which is also generated by the macro call above
mod what_is_being_generated {
use asn1rs::prelude::*;
#[asn(sequence)]
#[derive(Default, Debug, Clone, PartialEq, Hash)]
pub struct Pizza {
#[asn(integer(min..max))]
pub price: u64,
#[asn(integer(1..4))]
pub size: u8,
#[asn(optional(utf8string))]
pub note: Option<String>,
}
#[asn(enumerated)]
#[derive(Debug, Clone, PartialEq, Hash, Copy, PartialOrd, Eq)]
pub enum Topping {
NotPineapple,
EvenLessPineapple,
NoPineappleAtAll,
}
#[asn(transparent)]
#[derive(Default, Debug, Clone, PartialEq, Hash)]
pub struct Custom(#[asn(utf8string)] pub String);
#[asn(choice)]
#[derive(Debug, Clone, PartialEq, Hash)]
pub enum WhatToEat {
#[asn(complex(Pizza, tag(UNIVERSAL(16))))]
Pizza(Pizza),
#[asn(complex(Custom, tag(UNIVERSAL(16))))]
Custom(Custom),
}
}
#[test]
fn uper_proof() {
use asn1rs::syn::io::UperWriter;
let mut writer = UperWriter::default();
writer
.write(&WhatToEat::Pizza(Pizza {
price: 2,
size: 3,
note: Some(String::from("Extra crusty!")),
}))
.unwrap();
// read into the plain type to prove they behave the same
use what_is_being_generated as g;
let mut reader = writer.as_reader();
let read = reader.read::<g::WhatToEat>().expect("Failed to read");
assert_eq!(
read,
g::WhatToEat::Pizza(g::Pizza {
price: 2,
size: 3,
note: Some(String::from("Extra crusty!"))
})
);
}