@@ -15,33 +15,36 @@ use paths::Utf8PathBuf;
1515use process_wrap:: std:: { StdChildWrapper , StdCommandWrap } ;
1616use stdx:: process:: streaming_output;
1717
18- /// Cargo output is structured as one JSON per line. This trait abstracts parsing one line of
19- /// cargo output into a Rust data type
20- pub ( crate ) trait CargoParser < T > : Send + ' static {
18+ /// This trait abstracts parsing one line of JSON output into a Rust
19+ /// data type.
20+ ///
21+ /// This is useful for `cargo check` output, `cargo test` output, as
22+ /// well as custom discover commands.
23+ pub ( crate ) trait JsonLinesParser < T > : Send + ' static {
2124 fn from_line ( & self , line : & str , error : & mut String ) -> Option < T > ;
2225 fn from_eof ( & self ) -> Option < T > ;
2326}
2427
25- struct CargoActor < T > {
26- parser : Box < dyn CargoParser < T > > ,
28+ struct CommandActor < T > {
29+ parser : Box < dyn JsonLinesParser < T > > ,
2730 sender : Sender < T > ,
2831 stdout : ChildStdout ,
2932 stderr : ChildStderr ,
3033}
3134
32- impl < T : Sized + Send + ' static > CargoActor < T > {
35+ impl < T : Sized + Send + ' static > CommandActor < T > {
3336 fn new (
34- parser : impl CargoParser < T > ,
37+ parser : impl JsonLinesParser < T > ,
3538 sender : Sender < T > ,
3639 stdout : ChildStdout ,
3740 stderr : ChildStderr ,
3841 ) -> Self {
3942 let parser = Box :: new ( parser) ;
40- CargoActor { parser, sender, stdout, stderr }
43+ CommandActor { parser, sender, stdout, stderr }
4144 }
4245}
4346
44- impl < T : Sized + Send + ' static > CargoActor < T > {
47+ impl < T : Sized + Send + ' static > CommandActor < T > {
4548 fn run ( self , outfile : Option < Utf8PathBuf > ) -> io:: Result < ( bool , String ) > {
4649 // We manually read a line at a time, instead of using serde's
4750 // stream deserializers, because the deserializer cannot recover
@@ -113,6 +116,9 @@ impl<T: Sized + Send + 'static> CargoActor<T> {
113116 }
114117}
115118
119+ /// 'Join On Drop' wrapper for a child process.
120+ ///
121+ /// This wrapper kills the process when the wrapper is dropped.
116122struct JodGroupChild ( Box < dyn StdChildWrapper > ) ;
117123
118124impl Drop for JodGroupChild {
@@ -122,9 +128,9 @@ impl Drop for JodGroupChild {
122128 }
123129}
124130
125- /// A handle to a cargo process used for fly-checking .
131+ /// A handle to a shell command, such as cargo for diagnostics (flycheck) .
126132pub ( crate ) struct CommandHandle < T > {
127- /// The handle to the actual cargo process. As we cannot cancel directly from with
133+ /// The handle to the actual child process. As we cannot cancel directly from with
128134 /// a read syscall dropping and therefore terminating the process is our best option.
129135 child : JodGroupChild ,
130136 thread : stdx:: thread:: JoinHandle < io:: Result < ( bool , String ) > > ,
@@ -147,7 +153,7 @@ impl<T> fmt::Debug for CommandHandle<T> {
147153impl < T : Sized + Send + ' static > CommandHandle < T > {
148154 pub ( crate ) fn spawn (
149155 mut command : Command ,
150- parser : impl CargoParser < T > ,
156+ parser : impl JsonLinesParser < T > ,
151157 sender : Sender < T > ,
152158 out_file : Option < Utf8PathBuf > ,
153159 ) -> std:: io:: Result < Self > {
@@ -167,7 +173,7 @@ impl<T: Sized + Send + 'static> CommandHandle<T> {
167173 let stdout = child. 0 . stdout ( ) . take ( ) . unwrap ( ) ;
168174 let stderr = child. 0 . stderr ( ) . take ( ) . unwrap ( ) ;
169175
170- let actor = CargoActor :: < T > :: new ( parser, sender, stdout, stderr) ;
176+ let actor = CommandActor :: < T > :: new ( parser, sender, stdout, stderr) ;
171177 let thread =
172178 stdx:: thread:: Builder :: new ( stdx:: thread:: ThreadIntent :: Worker , "CommandHandle" )
173179 . spawn ( move || actor. run ( out_file) )
0 commit comments