1- use std:: { collections:: HashMap , path:: PathBuf , str :: FromStr } ;
1+ use std:: { collections:: HashMap , path:: PathBuf } ;
22use std:: rc:: Rc ;
33use std:: cell:: RefCell ;
44use std:: fs:: OpenOptions ;
@@ -8,11 +8,11 @@ use serde_json::Value;
88
99use petgraph:: { dot, graph:: NodeIndex } ;
1010
11- use anyhow:: { Result , anyhow } ;
11+ use anyhow:: { Result , format_err } ;
1212
1313use std:: fs;
1414
15- use crate :: { graph:: CachedStableGraph , merge_views} ;
15+ use crate :: { graph:: CachedStableGraph , merge_views, url_norm :: FromJSON } ;
1616use crate :: dfs;
1717
1818pub struct CustomCommandProvider {
@@ -28,24 +28,24 @@ impl CustomCommandProvider {
2828 }
2929 }
3030
31- pub fn execute ( & self , command : & str , args : Vec < Value > , root_path : & PathBuf ) -> Result < Value , String > {
31+ pub fn execute ( & self , command : & str , args : Vec < Value > , root_path : & PathBuf ) -> Result < Value > {
3232 if self . commands . contains_key ( command) {
3333 return self . commands . get ( command) . unwrap ( ) . run_command ( root_path, args) ;
3434 }
35- Err ( "command doesn't exist" . into ( ) )
35+ Err ( format_err ! ( "command doesn't exist" ) )
3636 }
3737}
3838
3939pub trait Invokeable {
40- fn run_command ( & self , root : & PathBuf , arguments : Vec < Value > ) -> Result < Value , String > ;
40+ fn run_command ( & self , root : & PathBuf , arguments : Vec < Value > ) -> Result < Value > ;
4141}
4242
4343pub struct GraphDotCommand {
4444 pub graph : Rc < RefCell < CachedStableGraph > >
4545}
4646
4747impl Invokeable for GraphDotCommand {
48- fn run_command ( & self , root : & PathBuf , _: Vec < Value > ) -> Result < Value , String > {
48+ fn run_command ( & self , root : & PathBuf , _: Vec < Value > ) -> Result < Value > {
4949 let filepath = root. join ( "graph.dot" ) ;
5050 eprintln ! ( "generating dot file at {:?}" , filepath) ;
5151 let mut file = OpenOptions :: new ( )
@@ -66,7 +66,7 @@ impl Invokeable for GraphDotCommand {
6666 } ;
6767
6868 match write_data_closure ( ) {
69- Err ( err) => Err ( format ! ( "Error generating graphviz data: {}" , err) ) ,
69+ Err ( err) => Err ( format_err ! ( "Error generating graphviz data: {}" , err) ) ,
7070 _ => Ok ( Value :: Null )
7171 }
7272 }
@@ -81,7 +81,7 @@ impl VirtualMergedDocument {
8181 fn get_file_toplevel_ancestors ( & self , uri : & PathBuf ) -> Result < Option < Vec < petgraph:: stable_graph:: NodeIndex > > > {
8282 let curr_node = match self . graph . borrow_mut ( ) . find_node ( uri) {
8383 Some ( n) => n,
84- None => return Err ( anyhow ! ( "node not found {:?}" , uri) ) ,
84+ None => return Err ( format_err ! ( "node not found {:?}" , uri) ) ,
8585 } ;
8686 let roots = self . graph . borrow ( ) . collect_root_ancestors ( curr_node) ;
8787 if roots. is_empty ( ) {
@@ -111,7 +111,7 @@ impl VirtualMergedDocument {
111111
112112 let source = match fs:: read_to_string ( & path) {
113113 Ok ( s) => s,
114- Err ( e) => return Err ( anyhow ! ( "error reading {:?}: {}" , path, e) )
114+ Err ( e) => return Err ( format_err ! ( "error reading {:?}: {}" , path, e) )
115115 } ;
116116 sources. insert ( path. clone ( ) , source) ;
117117 }
@@ -121,19 +121,15 @@ impl VirtualMergedDocument {
121121}
122122
123123impl Invokeable for VirtualMergedDocument {
124- fn run_command ( & self , root : & PathBuf , arguments : Vec < Value > ) -> Result < Value , String > {
125- let path = arguments. get ( 0 ) . unwrap ( ) . to_string ( ) ;
126- let path = percent_encoding:: percent_decode_str (
127- path. trim_start_matches ( '"' ) . trim_end_matches ( '"' ) . strip_prefix ( "/" ) . unwrap ( )
128- ) . decode_utf8 ( ) . unwrap ( ) ;
129- let path = PathBuf :: from_str ( & path) . unwrap ( ) ;
124+ fn run_command ( & self , root : & PathBuf , arguments : Vec < Value > ) -> Result < Value > {
125+ let path = PathBuf :: from_json ( arguments. get ( 0 ) . unwrap ( ) ) ?;
130126
131127 let file_ancestors = match self . get_file_toplevel_ancestors ( & path) {
132128 Ok ( opt) => match opt {
133129 Some ( ancestors) => ancestors,
134130 None => vec ! [ ] ,
135131 } ,
136- Err ( e) => return Err ( e. to_string ( ) ) ,
132+ Err ( e) => return Err ( e) ,
137133 } ;
138134
139135 //eprintln!("ancestors for {}:\n\t{:?}", path, file_ancestors.iter().map(|e| self.graph.borrow().graph.node_weight(*e).unwrap().clone()).collect::<Vec<String>>());
@@ -147,19 +143,19 @@ impl Invokeable for VirtualMergedDocument {
147143 let root = self . graph . borrow_mut ( ) . find_node ( & path) . unwrap ( ) ;
148144 let tree = match self . get_dfs_for_node ( root) {
149145 Ok ( tree) => tree,
150- Err ( e) => return Err ( e. to_string ( ) ) ,
146+ Err ( e) => return Err ( e. into ( ) ) ,
151147 } ;
152148
153149 let sources = match self . load_sources ( & tree) {
154150 Ok ( s) => s,
155- Err ( e) => return Err ( e. to_string ( ) )
151+ Err ( e) => return Err ( e)
156152 } ;
157153 all_sources. extend ( sources) ;
158154
159155 let graph = self . graph . borrow ( ) ;
160156 let view = merge_views:: generate_merge_list ( & tree, & all_sources, & graph) ;
161157 return Ok ( serde_json:: value:: Value :: String ( view) ) ;
162158 }
163- return Err ( format ! ( "{:?} is not a top-level file aka has ancestors" , path. strip_prefix( root) . unwrap( ) ) )
159+ return Err ( format_err ! ( "{:?} is not a top-level file aka has ancestors" , path. strip_prefix( root) . unwrap( ) ) )
164160 }
165161}
0 commit comments