File tree 2 files changed +46
-17
lines changed
2 files changed +46
-17
lines changed Original file line number Diff line number Diff line change @@ -8,19 +8,6 @@ use std::fs::File;
8
8
use std:: io:: LineWriter ;
9
9
use std:: path:: PathBuf ;
10
10
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
-
24
11
/// Given a tree-sitter node type's (kind, named) pair, returns a single string
25
12
/// representing the (unescaped) name we'll use to refer to corresponding QL
26
13
/// type.
@@ -301,12 +288,19 @@ fn main() {
301
288
node_types_path : PathBuf :: from ( "tree-sitter-ruby/src/node-types.json" ) ,
302
289
dbscheme_path : PathBuf :: from ( "ruby.dbscheme" ) ,
303
290
} ;
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
+ ) ;
307
301
std:: process:: exit ( 1 ) ;
308
302
}
309
- Some ( nodes) => {
303
+ Ok ( nodes) => {
310
304
let mut dbscheme_entries = convert_nodes ( & nodes) ;
311
305
dbscheme_entries. push ( create_location_entry ( ) ) ;
312
306
dbscheme_entries. push ( create_source_location_prefix_entry ( ) ) ;
Original file line number Diff line number Diff line change 1
1
use serde:: Deserialize ;
2
2
use std:: collections:: BTreeMap ;
3
+ use std:: fmt;
4
+ use std:: path:: Path ;
3
5
4
6
#[ derive( Deserialize ) ]
5
7
pub struct NodeInfo {
@@ -37,3 +39,36 @@ impl Default for FieldInfo {
37
39
}
38
40
}
39
41
}
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
+ }
You can’t perform that action at this time.
0 commit comments