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
mod idl_analyzer {
static IDL_FIRST: &str = r#"
library IdlTest;
enum Enumeration {
First,
Second,
Third,
}
enum TypeEnumeration {
First: string,
Second: int[],
Third: RecordType,
}
record RecordType {
name: string,
fields: StructType[],
data: byte[],
}
struct StructType {
x: float,
y: float,
}
const Base10Constant: int = 0x4042;
const IntegerConstant: int = 1123581321345589144233377610987159725844181;
const FloatingPointConstant: float = 3.141592653589793238462643383279;
const StringConstant: string = "Frohe und dankbare Gefühle nach dem Sturm.";
interface InterfaceService {
hello: (name: string) -> string,
sum: (first: int, second: int) -> int,
sendMessage: (message: string) -> stream string,
}
"#;
use idl::{idl::analyzer, idl::nodes, idl::parser};
fn to_hex_string(bytes: &[u8]) -> String {
let strs: Vec<String> = bytes.iter().map(|b| format!("{:02x}", b)).collect();
strs.join("")
}
#[test]
fn try_this() {
match parser::Parser::parse(IDL_FIRST) {
Ok(parser) => match analyzer::Analyzer::resolve(&parser) {
Ok(analyzer) => {
let hex_value = analyzer.library_hash();
println!(
"library `{}` disgest: {}",
analyzer.library_name(),
to_hex_string(&hex_value)
);
assert_eq!(
"19bd6b08b8592cf3e6fdf269a4763592",
to_hex_string(&hex_value),
"Invalid hash for library."
);
for node in &analyzer.nodes {
match node {
nodes::IdlNode::TypeStruct(value) => {
println!(
"`{}` disgest: {}",
value.ident,
to_hex_string(&value.hash)
);
for field_node in &value.fields {
if let nodes::StructNode::StructField(field) = field_node {
println!(
" `{}` disgest: {}",
field.ident,
to_hex_string(&field.hash)
);
}
}
}
nodes::IdlNode::TypeEnum(value) => {
println!(
"`{}` disgest: {}",
value.ident,
to_hex_string(&value.hash)
);
for field_node in &value.fields {
if let nodes::EnumNode::EnumField(field) = field_node {
println!(
" `{}` disgest: {}",
field.ident,
to_hex_string(&field.hash)
);
}
}
}
nodes::IdlNode::TypeList(value) => {
println!(
"`{}` disgest: {}",
value.ident,
to_hex_string(&value.hash)
);
for field_node in &value.fields {
if let nodes::TypeListNode::TypeListField(field) = field_node {
println!(
" `{}` disgest: {}",
field.ident,
to_hex_string(&field.hash)
);
}
}
}
nodes::IdlNode::TypeConst(value) => {
println!(
"`{}` disgest: {}",
value.ident,
to_hex_string(&value.hash)
);
}
nodes::IdlNode::TypeInterface(value) => {
println!(
"`{}` disgest: {}",
value.ident,
to_hex_string(&value.hash)
);
for field_node in &value.fields {
if let nodes::InterfaceNode::InterfaceField(field) = field_node
{
println!(
" `{}` disgest: {}",
field.ident,
to_hex_string(&field.hash)
);
}
}
}
_ => {}
}
}
}
Err(err) => panic!("Error: {}", err),
},
Err(err) => {
panic!("{:?} at {:?}", err.1, err.1.get_range());
}
}
}
}