-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathlib.rs
More file actions
209 lines (182 loc) · 5.56 KB
/
lib.rs
File metadata and controls
209 lines (182 loc) · 5.56 KB
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
use std::collections::HashMap;
use std::fmt::Debug;
use anyhow::Error;
use protobuf::Message;
use protobuf::UnknownValueRef;
use protobuf::descriptor::DescriptorProto;
use protobuf::descriptor::FieldDescriptorProto;
use protobuf::descriptor::OneofDescriptorProto;
use protobuf::descriptor::field_descriptor_proto::Label;
use protobuf::descriptor::field_descriptor_proto::Type;
use std::convert::From;
use std::path::Path;
const REQUIRED_ID: u32 = 77001;
#[derive(Debug, Clone)]
pub struct Field {
pub name: String,
pub type_name: String,
pub required: bool,
pub is_enum: bool,
pub is_array: bool,
pub fields: Vec<Field>,
}
#[derive(Debug, Clone)]
pub struct PType {
pub name: String,
pub fields: Vec<Field>,
pub descriptor: DescriptorProto,
}
impl PType {
pub fn fields(&self) -> Option<String> {
let mut v = Vec::new();
if let Some(vv) = self.req_fields_as_string() {
v.push(vv);
}
if let Some(vv) = self.enum_fields_as_string() {
v.push(vv);
}
if v.is_empty() {
None
} else {
Some(v.join(","))
}
}
pub fn has_req_fields(&self) -> bool {
self.fields.iter().any(|f| f.required)
}
pub fn req_fields_as_string(&self) -> Option<String> {
if self.has_req_fields() {
Some(format!(
"__required__{{{}}}",
self.fields
.iter()
.filter(|f| f.required)
.map(|f| format!("{}: {}", f.name, f.type_name))
.collect::<Vec<String>>()
.join(",")
))
} else {
None
}
}
pub fn has_enum(&self) -> bool {
self.fields.iter().any(|f| f.is_enum)
}
pub fn enum_fields_as_string(&self) -> Option<String> {
if !self.has_enum() {
return None;
}
Some(
self.fields
.iter()
.filter(|f| f.is_enum)
.map(|f| {
let pairs = f
.fields
.iter()
.map(|f| format!("{}: {}", f.name, f.type_name))
.collect::<Vec<String>>()
.join(",");
format!("{}{{{}}}", f.name, pairs)
})
.collect::<Vec<String>>()
.join(","),
)
}
}
impl From<&FieldDescriptorProto> for Field {
fn from(fd: &FieldDescriptorProto) -> Self {
let options = fd.options.unknown_fields();
let type_name = if let Some(type_name) = fd.type_name.as_ref() {
type_name.clone()
} else if let Type::TYPE_BYTES = fd.type_() {
"Vec<u8>".to_owned()
} else {
use heck::ToUpperCamelCase;
fd.name().to_string().to_upper_camel_case()
};
Field {
name: fd.name().to_owned(),
type_name: type_name.rsplit('.').next().unwrap().to_owned(),
required: options
.iter()
//(firehose.required) = true, UnknownValueRef::Varint(0) => false, UnknownValueRef::Varint(1) => true
.any(|f| f.0 == REQUIRED_ID && UnknownValueRef::Varint(1) == f.1),
is_enum: false,
is_array: Label::LABEL_REPEATED == fd.label(),
fields: vec![],
}
}
}
impl From<&OneofDescriptorProto> for Field {
fn from(fd: &OneofDescriptorProto) -> Self {
Field {
name: fd.name().to_owned(),
type_name: "".to_owned(),
required: false,
is_enum: true,
is_array: false,
fields: vec![],
}
}
}
impl From<&DescriptorProto> for PType {
fn from(dp: &DescriptorProto) -> Self {
let mut fields = dp
.oneof_decl
.iter()
.enumerate()
.map(|(index, fd)| {
let mut fld = Field::from(fd);
fld.fields = dp
.field
.iter()
.filter(|fd| fd.oneof_index.is_some())
.filter(|fd| *fd.oneof_index.as_ref().unwrap() as usize == index)
.map(Field::from)
.collect::<Vec<Field>>();
fld
})
.collect::<Vec<Field>>();
fields.extend(
dp.field
.iter()
.filter(|fd| fd.oneof_index.is_none())
.map(Field::from)
.collect::<Vec<Field>>(),
);
PType {
name: dp.name().to_owned(),
fields,
descriptor: dp.clone(),
}
}
}
pub fn parse_proto_file<'a, P>(file_path: P) -> Result<HashMap<String, PType>, Error>
where
P: 'a + AsRef<Path> + Debug,
{
let dir = if let Some(p) = file_path.as_ref().parent() {
p
} else {
return Err(anyhow::anyhow!(
"Unable to derive parent path for {:?}",
file_path
));
};
let fd = protobuf_parse::Parser::new()
.include(dir)
.input(&file_path)
.file_descriptor_set()?;
assert!(fd.file.len() == 1);
assert!(fd.file[0].has_name());
let file_name = file_path.as_ref().file_name().unwrap().to_str().unwrap();
assert!(fd.file[0].name() == file_name);
let ret_val = fd
.file
.iter() //should be just 1 file
.flat_map(|f| f.message_type.iter())
.map(|dp| (dp.name().to_owned(), PType::from(dp)))
.collect::<HashMap<String, PType>>();
Ok(ret_val)
}