parent
							
								
									b711eff13d
								
							
						
					
					
						commit
						57ae564fd7
					
				
				 6 changed files with 97 additions and 20 deletions
			
			
		@ -0,0 +1,55 @@ | 
				
			|||||||
 | 
					use std::process::{ | 
				
			||||||
 | 
					    Command, | 
				
			||||||
 | 
					    Output | 
				
			||||||
 | 
					}; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub type JobResult<'res> = Result<&'res [u8], &'res [u8]>; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub trait Job { | 
				
			||||||
 | 
					    async fn run(&mut self); | 
				
			||||||
 | 
					    fn result(&self) -> JobResult; | 
				
			||||||
 | 
					} | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub struct ShellJob<'cmd> { | 
				
			||||||
 | 
					    pub result: JobResult<'cmd>, | 
				
			||||||
 | 
					    pub cmd: &'cmd str, | 
				
			||||||
 | 
					    pub args: Vec<&'cmd str> | 
				
			||||||
 | 
					} | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl ShellJob { | 
				
			||||||
 | 
					    pub fn from_raw(raw_cmd: &str) -> Self { | 
				
			||||||
 | 
					        let cmd_parts = raw_cmd.split(" "); | 
				
			||||||
 | 
					        Self { | 
				
			||||||
 | 
					            cmd: cmd_parts[0], | 
				
			||||||
 | 
					            args: cmd_parts[1..], | 
				
			||||||
 | 
					            result: Err("Did not run".as_bytes()) | 
				
			||||||
 | 
					        } | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					} | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Job for ShellJob { | 
				
			||||||
 | 
					    async fn run(&mut self) { | 
				
			||||||
 | 
					        let result = Command::new(self.cmd) | 
				
			||||||
 | 
					            .args(&self.args) | 
				
			||||||
 | 
					            .output(); | 
				
			||||||
 | 
					        self.result = match result { | 
				
			||||||
 | 
					            Ok(output) => { | 
				
			||||||
 | 
					                if output.status != 0 { | 
				
			||||||
 | 
					                    Err(&output.stderr) | 
				
			||||||
 | 
					                } else { | 
				
			||||||
 | 
					                    Ok(&output.stdout) | 
				
			||||||
 | 
					                } | 
				
			||||||
 | 
					            } | 
				
			||||||
 | 
					            Err(e) => e.to_string().as_bytes() | 
				
			||||||
 | 
					        } | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn result(&self) -> JobResult { | 
				
			||||||
 | 
					        self.result | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					} | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* | 
				
			||||||
 | 
					pub struct PyJob { | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}*/ | 
				
			||||||
					Loading…
					
					
				
		Reference in new issue