Skip to content

Commit 36823d7

Browse files
committed
Move deserialization to node_types module; propagate errors to caller
1 parent e018f3f commit 36823d7

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

generator/src/main.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,6 @@ use std::fs::File;
88
use std::io::LineWriter;
99
use std::path::PathBuf;
1010

11-
fn read_node_types(language: &Language) -> Option<Vec<NodeInfo>> {
12-
let json_data = match std::fs::read_to_string(&language.node_types_path) {
13-
Ok(s) => s,
14-
Err(_) => return None,
15-
};
16-
let nodes: Vec<NodeInfo> = match serde_json::from_str(&json_data) {
17-
Ok(n) => n,
18-
Err(_) => return None,
19-
};
20-
21-
Some(nodes)
22-
}
23-
2411
/// Given a tree-sitter node type's (kind, named) pair, returns a single string
2512
/// representing the (unescaped) name we'll use to refer to corresponding QL
2613
/// type.
@@ -301,12 +288,19 @@ fn main() {
301288
node_types_path: PathBuf::from("tree-sitter-ruby/src/node-types.json"),
302289
dbscheme_path: PathBuf::from("ruby.dbscheme"),
303290
};
304-
match read_node_types(&ruby) {
305-
None => {
306-
println!("Failed to read node types");
291+
match node_types::read(&ruby.node_types_path) {
292+
Err(e) => {
293+
println!(
294+
"Failed to read '{}': {}",
295+
match ruby.node_types_path.to_str() {
296+
None => "<undisplayable>",
297+
Some(p) => p,
298+
},
299+
e
300+
);
307301
std::process::exit(1);
308302
}
309-
Some(nodes) => {
303+
Ok(nodes) => {
310304
let mut dbscheme_entries = convert_nodes(&nodes);
311305
dbscheme_entries.push(create_location_entry());
312306
dbscheme_entries.push(create_source_location_prefix_entry());

generator/src/node_types.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use serde::Deserialize;
22
use std::collections::BTreeMap;
3+
use std::fmt;
4+
use std::path::Path;
35

46
#[derive(Deserialize)]
57
pub struct NodeInfo {
@@ -37,3 +39,36 @@ impl Default for FieldInfo {
3739
}
3840
}
3941
}
42+
43+
pub enum Error {
44+
IOError(std::io::Error),
45+
JsonError(serde_json::error::Error),
46+
}
47+
48+
impl From<std::io::Error> for Error {
49+
fn from(error: std::io::Error) -> Self {
50+
Error::IOError(error)
51+
}
52+
}
53+
54+
impl From<serde_json::Error> for Error {
55+
fn from(error: serde_json::Error) -> Self {
56+
Error::JsonError(error)
57+
}
58+
}
59+
60+
impl fmt::Display for Error {
61+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62+
match self {
63+
Error::IOError(e) => write!(f, "{}", e),
64+
Error::JsonError(e) => write!(f, "{}", e),
65+
}
66+
}
67+
}
68+
69+
/// Deserializes the node types from the JSON at the given `path`.
70+
pub fn read(path: &Path) -> Result<Vec<NodeInfo>, Error> {
71+
let json_data = std::fs::read_to_string(path)?;
72+
let node_types: Vec<NodeInfo> = serde_json::from_str(&json_data)?;
73+
Ok(node_types)
74+
}

0 commit comments

Comments
 (0)