File tree Expand file tree Collapse file tree 2 files changed +46
-17
lines changed
Expand file tree Collapse file tree 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;
88use std:: io:: LineWriter ;
99use 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 ( ) ) ;
Original file line number Diff line number Diff line change 11use serde:: Deserialize ;
22use std:: collections:: BTreeMap ;
3+ use std:: fmt;
4+ use std:: path:: Path ;
35
46#[ derive( Deserialize ) ]
57pub 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+ }
You can’t perform that action at this time.
0 commit comments