Skip to content

Commit ff35f9f

Browse files
committed
Shared: Clean up NodeInfo in shared extractor
I was perusing the shared extractor the other day, when I came across the `NodeInfo` struct. I noticed that the `fields` and `subtypes` fields on this struct had two seemingly identical ways of expressing the same thing: `None` and `Some(empty)` (where `empty` is respectively the empty map and the empty vector). As far as I can tell, there's no semantic difference in either case, so we can just elide the option type entirely and use the empty value directly. This has the nice side-effect of cleaning up some of the other code.
1 parent 13d3e97 commit ff35f9f

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

shared/tree-sitter-extractor/src/node_types.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,7 @@ pub fn convert_nodes(prefix: &str, nodes: &[NodeInfo]) -> NodeTypeMap {
114114

115115
// First, find all the token kinds
116116
for node in nodes {
117-
if node.subtypes.is_none()
118-
&& node.fields.as_ref().map_or(0, |x| x.len()) == 0
119-
&& node.children.is_none()
120-
{
117+
if node.subtypes.is_empty() && node.fields.is_empty() && node.children.is_none() {
121118
let type_name = TypeName {
122119
kind: node.kind.clone(),
123120
named: node.named,
@@ -131,7 +128,8 @@ pub fn convert_nodes(prefix: &str, nodes: &[NodeInfo]) -> NodeTypeMap {
131128
let dbscheme_name = escape_name(flattened_name);
132129
let ql_class_name = dbscheme_name_to_class_name(&dbscheme_name);
133130
let dbscheme_name = format!("{}_{}", prefix, &dbscheme_name);
134-
if let Some(subtypes) = &node.subtypes {
131+
let subtypes = &node.subtypes;
132+
if !subtypes.is_empty() {
135133
// It's a tree-sitter supertype node, for which we create a union
136134
// type.
137135
entries.insert(
@@ -147,7 +145,7 @@ pub fn convert_nodes(prefix: &str, nodes: &[NodeInfo]) -> NodeTypeMap {
147145
},
148146
},
149147
);
150-
} else if node.fields.as_ref().map_or(0, |x| x.len()) == 0 && node.children.is_none() {
148+
} else if node.fields.is_empty() && node.children.is_none() {
151149
// Token kind, handled above.
152150
} else {
153151
// It's a product type, defined by a table.
@@ -162,17 +160,15 @@ pub fn convert_nodes(prefix: &str, nodes: &[NodeInfo]) -> NodeTypeMap {
162160

163161
// If the type also has fields or children, then we create either
164162
// auxiliary tables or columns in the defining table for them.
165-
if let Some(node_fields) = &node.fields {
166-
for (field_name, field_info) in node_fields {
167-
add_field(
168-
prefix,
169-
&type_name,
170-
Some(field_name.to_string()),
171-
field_info,
172-
&mut fields,
173-
&token_kinds,
174-
);
175-
}
163+
for (field_name, field_info) in &node.fields {
164+
add_field(
165+
prefix,
166+
&type_name,
167+
Some(field_name.to_string()),
168+
field_info,
169+
&mut fields,
170+
&token_kinds,
171+
);
176172
}
177173
if let Some(children) = &node.children {
178174
// Treat children as if they were a field called 'child'.
@@ -301,12 +297,12 @@ pub struct NodeInfo {
301297
#[serde(rename = "type")]
302298
pub kind: String,
303299
pub named: bool,
304-
#[serde(skip_serializing_if = "Option::is_none")]
305-
pub fields: Option<BTreeMap<String, FieldInfo>>,
300+
#[serde(skip_serializing_if = "BTreeMap::is_empty", default)]
301+
pub fields: BTreeMap<String, FieldInfo>,
306302
#[serde(skip_serializing_if = "Option::is_none")]
307303
pub children: Option<FieldInfo>,
308-
#[serde(skip_serializing_if = "Option::is_none")]
309-
pub subtypes: Option<Vec<NodeType>>,
304+
#[serde(skip_serializing_if = "Vec::is_empty", default)]
305+
pub subtypes: Vec<NodeType>,
310306
}
311307

312308
#[derive(Deserialize)]

0 commit comments

Comments
 (0)