forked from kellerkindt/asn1rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc_macro_reparse.rs
214 lines (177 loc) · 5.8 KB
/
proc_macro_reparse.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
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
//! The Tests in this module ensure that the generated rust code and its asn attributes carry all
//! the data back into the rust model. It does not cover whether all attributes from the parsed
//! ASN-Model is reflected into the Rust-Model.
//!
//! Summed up, the tests check whether the RustCodeGen path serializes all data and whether
//! the proc-macro path is the proper inverse of it - whether it deserializes all data.
//!
//! ASN-Definition ----> ASN-Model
//! |
//! V
//! Rust-Model ---> RustCodeGen ---+
//! A V
//! | eq! Rust-Code and ASN-Attributes
//! V |
//! Rust-Model <--- proc-macro <---+
use asn1rs::model::{Definition, Model, Rust};
use asn1rs::parser::Tokenizer;
use asn1rs_model::gen::RustCodeGenerator;
use codegen::Scope;
use proc_macro2::TokenStream;
#[test]
fn test_standard_enum() {
parse_asn_map_to_rust_map_to_stringify_with_proc_macro_annotation_re_parse_check_equal(
r#"BasicSchema DEFINITIONS AUTOMATIC TAGS ::= BEGIN
MyType ::= [UNIVERSAL 5] ENUMERATED {
implicit,
number(7),
wow
}
END"#,
)
}
#[test]
fn test_extensible_enum() {
parse_asn_map_to_rust_map_to_stringify_with_proc_macro_annotation_re_parse_check_equal(
r#"BasicSchema DEFINITIONS AUTOMATIC TAGS ::= BEGIN
MyType ::= [UNIVERSAL 5] ENUMERATED {
implicit,
number(7),
...,
wow
}
END"#,
)
}
#[test]
fn test_standard_choice() {
parse_asn_map_to_rust_map_to_stringify_with_proc_macro_annotation_re_parse_check_equal(
r#"BasicSchema DEFINITIONS AUTOMATIC TAGS ::= BEGIN
MyType ::= [PRIVATE 1] CHOICE {
abc UTF8String,
def [APPLICATION 7] INTEGER,
ghi UTF8String
}
END"#,
)
}
#[test]
fn test_extensible_choice() {
parse_asn_map_to_rust_map_to_stringify_with_proc_macro_annotation_re_parse_check_equal(
r#"BasicSchema DEFINITIONS AUTOMATIC TAGS ::= BEGIN
MyType ::= [PRIVATE 1] CHOICE {
abc UTF8String,
def [APPLICATION 7] INTEGER,
...,
ghi UTF8String
}
END"#,
)
}
#[test]
fn test_standard_sequence() {
parse_asn_map_to_rust_map_to_stringify_with_proc_macro_annotation_re_parse_check_equal(
r#"BasicSchema DEFINITIONS AUTOMATIC TAGS ::= BEGIN
MyType ::= [5] SEQUENCE {
abc UTF8String,
def [APPLICATION 7] INTEGER,
ghi UTF8String
}
END"#,
)
}
#[test]
fn test_extensible_sequence() {
parse_asn_map_to_rust_map_to_stringify_with_proc_macro_annotation_re_parse_check_equal(
r#"BasicSchema DEFINITIONS AUTOMATIC TAGS ::= BEGIN
MyType ::= [5] SEQUENCE {
abc UTF8String,
...,
def [APPLICATION 7] INTEGER,
ghi UTF8String
}
END"#,
)
}
#[test]
fn test_standard_sequence_of() {
parse_asn_map_to_rust_map_to_stringify_with_proc_macro_annotation_re_parse_check_equal(
r#"BasicSchema DEFINITIONS AUTOMATIC TAGS ::= BEGIN
MyType ::= [1023] SEQUENCE OF INTEGER
END"#,
)
}
#[test]
fn test_integer_constants() {
parse_asn_map_to_rust_map_to_stringify_with_proc_macro_annotation_re_parse_check_equal(
r#"BasicSchema DEFINITIONS AUTOMATIC TAGS ::= BEGIN
MyType ::= [5] SEQUENCE {
great INTEGER { abc(3), def(68) } (0..255),
wower INTEGER { ghi(3), jkl(99) },
def [APPLICATION 7] INTEGER,
ghi UTF8String
}
OtherType ::= [APPLICATION 99] INTEGER {
my-type(5),
other-type(7)
}
END"#,
)
}
#[test]
fn test_extensible_integer() {
parse_asn_map_to_rust_map_to_stringify_with_proc_macro_annotation_re_parse_check_equal(
r#"BasicSchema DEFINITIONS AUTOMATIC TAGS ::= BEGIN
RangedAndExtensible ::= Integer (0..255,...)
END"#,
)
}
fn parse_asn_map_to_rust_map_to_stringify_with_proc_macro_annotation_re_parse_check_equal(
asn: &str,
) {
let tokens = Tokenizer::default().parse(asn);
let asn_model = Model::try_from(tokens).unwrap().try_resolve().unwrap();
let rust_model = asn_model.to_rust();
for definition in rust_model.definitions {
let stringified = generate_rust_code_with_proc_macro_attributes(&definition);
let mut lines = stringified.lines().map(str::trim).filter(|s| !s.is_empty());
let attribute = extract_attribute(lines.next().unwrap());
let body = lines
.map(|l| l.to_string())
.collect::<Vec<_>>()
.join("\n")
.parse::<TokenStream>()
.unwrap();
println!("---");
println!("ATTRIBUTE: {}", attribute.to_string());
println!("BODY: {}", body.to_string());
println!("---");
let re_parsed = asn1rs::ast::parse_asn_definition(attribute, body)
.map(|(d, _item)| d)
.unwrap()
.unwrap();
let re_parsed_model = Model {
name: rust_model.name.clone(),
imports: rust_model.imports.clone(),
definitions: vec![re_parsed],
..Default::default()
};
assert_eq!(vec![definition], re_parsed_model.to_rust().definitions);
println!("{:?}", re_parsed_model.to_rust().definitions);
}
}
fn generate_rust_code_with_proc_macro_attributes(definition: &Definition<Rust>) -> String {
let mut scope = Scope::new();
RustCodeGenerator::default().add_definition(&mut scope, &definition);
scope.to_string()
}
fn extract_attribute(attr: &str) -> TokenStream {
const PREFIX: &str = "#[asn(";
const SUFFIX: &str = ")]";
assert!(attr.starts_with(PREFIX));
assert!(attr.ends_with(SUFFIX));
let substr = attr.split_at(PREFIX.len()).1;
let substr = substr.split_at(substr.len() - SUFFIX.len()).0;
let attr: TokenStream = substr.parse().unwrap();
attr
}