1- use std:: { collections:: HashMap , path:: { Path , PathBuf } } ;
2- use std:: rc:: Rc ;
31use std:: cell:: RefCell ;
42use std:: fs:: OpenOptions ;
53use std:: io:: prelude:: * ;
4+ use std:: rc:: Rc ;
5+ use std:: {
6+ collections:: HashMap ,
7+ path:: { Path , PathBuf } ,
8+ } ;
69
10+ use petgraph:: dot:: Config ;
711use serde_json:: Value ;
812
913use petgraph:: { dot, graph:: NodeIndex } ;
1014
11- use anyhow:: { Result , format_err} ;
15+ use anyhow:: { format_err, Result } ;
16+ use slog_scope:: info;
1217
1318use std:: fs;
1419
15- use crate :: { graph:: CachedStableGraph , merge_views, url_norm:: FromJson } ;
1620use crate :: dfs;
21+ use crate :: { graph:: CachedStableGraph , merge_views, url_norm:: FromJson } ;
1722
1823pub struct CustomCommandProvider {
19- commands : HashMap < String , Box < dyn Invokeable > >
24+ commands : HashMap < String , Box < dyn Invokeable > > ,
2025}
2126
2227impl CustomCommandProvider {
2328 pub fn new ( commands : Vec < ( & str , Box < dyn Invokeable > ) > ) -> CustomCommandProvider {
24- CustomCommandProvider {
25- commands : commands. into_iter ( ) . map ( |tup| {
26- ( tup. 0 . into ( ) , tup. 1 )
27- } ) . collect ( ) ,
29+ CustomCommandProvider {
30+ commands : commands. into_iter ( ) . map ( |tup| ( tup. 0 . into ( ) , tup. 1 ) ) . collect ( ) ,
2831 }
2932 }
3033
31- pub fn execute ( & self , command : & str , args : Vec < Value > , root_path : & Path ) -> Result < Value > {
34+ pub fn execute ( & self , command : & str , args : & [ Value ] , root_path : & Path ) -> Result < Value > {
3235 if self . commands . contains_key ( command) {
3336 return self . commands . get ( command) . unwrap ( ) . run_command ( root_path, args) ;
3437 }
@@ -37,43 +40,42 @@ impl CustomCommandProvider {
3740}
3841
3942pub trait Invokeable {
40- fn run_command ( & self , root : & Path , arguments : Vec < Value > ) -> Result < Value > ;
43+ fn run_command ( & self , root : & Path , arguments : & [ Value ] ) -> Result < Value > ;
4144}
4245
4346pub struct GraphDotCommand {
44- pub graph : Rc < RefCell < CachedStableGraph > >
47+ pub graph : Rc < RefCell < CachedStableGraph > > ,
4548}
4649
4750impl Invokeable for GraphDotCommand {
48- fn run_command ( & self , root : & Path , _: Vec < Value > ) -> Result < Value > {
51+ fn run_command ( & self , root : & Path , _: & [ Value ] ) -> Result < Value > {
4952 let filepath = root. join ( "graph.dot" ) ;
50- eprintln ! ( "generating dot file at {:?}" , filepath) ;
51- let mut file = OpenOptions :: new ( )
52- . truncate ( true )
53- . write ( true )
54- . create ( true )
55- . open ( filepath)
56- . unwrap ( ) ;
53+
54+ info ! ( "generating dot file" ; "path" => filepath. as_os_str( ) . to_str( ) ) ;
55+
56+ let mut file = OpenOptions :: new ( ) . truncate ( true ) . write ( true ) . create ( true ) . open ( filepath) . unwrap ( ) ;
5757
5858 let mut write_data_closure = || -> Result < ( ) , std:: io:: Error > {
5959 let graph = self . graph . as_ref ( ) ;
6060
6161 file. seek ( std:: io:: SeekFrom :: Start ( 0 ) ) ?;
62- file. write_all ( dot:: Dot :: new ( & graph. borrow ( ) . graph ) . to_string ( ) . as_bytes ( ) ) ?;
62+ file. write_all ( "digraph {\n \t graph [splines=ortho]\n \t node [shape=box]\n " . as_bytes ( ) ) ?;
63+ file. write_all ( dot:: Dot :: with_config ( & graph. borrow ( ) . graph , & [ Config :: GraphContentOnly ] ) . to_string ( ) . as_bytes ( ) ) ?;
64+ file. write_all ( "\n }" . as_bytes ( ) ) ?;
6365 file. flush ( ) ?;
6466 file. seek ( std:: io:: SeekFrom :: Start ( 0 ) ) ?;
6567 Ok ( ( ) )
6668 } ;
6769
6870 match write_data_closure ( ) {
69- Err ( err) => Err ( format_err ! ( "Error generating graphviz data: {}" , err) ) ,
70- _ => Ok ( Value :: Null )
71+ Err ( err) => Err ( format_err ! ( "error generating graphviz data: {}" , err) ) ,
72+ _ => Ok ( Value :: Null ) ,
7173 }
7274 }
7375}
7476
7577pub struct VirtualMergedDocument {
76- pub graph : Rc < RefCell < CachedStableGraph > >
78+ pub graph : Rc < RefCell < CachedStableGraph > > ,
7779}
7880
7981impl VirtualMergedDocument {
@@ -111,7 +113,7 @@ impl VirtualMergedDocument {
111113
112114 let source = match fs:: read_to_string ( & path) {
113115 Ok ( s) => s,
114- Err ( e) => return Err ( format_err ! ( "error reading {:?}: {}" , path, e) )
116+ Err ( e) => return Err ( format_err ! ( "error reading {:?}: {}" , path, e) ) ,
115117 } ;
116118 let source = source. replace ( "\r \n " , "\n " ) ;
117119 sources. insert ( path. clone ( ) , source) ;
@@ -122,7 +124,7 @@ impl VirtualMergedDocument {
122124}
123125
124126impl Invokeable for VirtualMergedDocument {
125- fn run_command ( & self , root : & Path , arguments : Vec < Value > ) -> Result < Value > {
127+ fn run_command ( & self , root : & Path , arguments : & [ Value ] ) -> Result < Value > {
126128 let path = PathBuf :: from_json ( arguments. get ( 0 ) . unwrap ( ) ) ?;
127129
128130 let file_ancestors = match self . get_file_toplevel_ancestors ( & path) {
@@ -132,15 +134,15 @@ impl Invokeable for VirtualMergedDocument {
132134 } ,
133135 Err ( e) => return Err ( e) ,
134136 } ;
135-
136- //eprintln !("ancestors for {}:\n\t{:?}", path, file_ancestors.iter().map(|e| self.graph.borrow().graph.node_weight(*e).unwrap().clone()).collect::<Vec<String>>());
137+
138+ //info !("ancestors for {}:\n\t{:?}", path, file_ancestors.iter().map(|e| self.graph.borrow().graph.node_weight(*e).unwrap().clone()).collect::<Vec<String>>());
137139
138140 // the set of all filepath->content. TODO: change to Url?
139141 let mut all_sources: HashMap < PathBuf , String > = HashMap :: new ( ) ;
140142
141143 // if we are a top-level file (this has to be one of the set defined by Optifine, right?)
142144 if file_ancestors. is_empty ( ) {
143- // gather the list of all descendants
145+ // gather the list of all descendants
144146 let root = self . graph . borrow_mut ( ) . find_node ( & path) . unwrap ( ) ;
145147 let tree = match self . get_dfs_for_node ( root) {
146148 Ok ( tree) => tree,
@@ -149,14 +151,17 @@ impl Invokeable for VirtualMergedDocument {
149151
150152 let sources = match self . load_sources ( & tree) {
151153 Ok ( s) => s,
152- Err ( e) => return Err ( e)
154+ Err ( e) => return Err ( e) ,
153155 } ;
154156 all_sources. extend ( sources) ;
155157
156158 let graph = self . graph . borrow ( ) ;
157159 let view = merge_views:: generate_merge_list ( & tree, & all_sources, & graph) ;
158160 return Ok ( serde_json:: value:: Value :: String ( view) ) ;
159161 }
160- return Err ( format_err ! ( "{:?} is not a top-level file aka has ancestors" , path. strip_prefix( root) . unwrap( ) ) )
162+ return Err ( format_err ! (
163+ "{:?} is not a top-level file aka has ancestors" ,
164+ path. strip_prefix( root) . unwrap( )
165+ ) ) ;
161166 }
162- }
167+ }
0 commit comments