use std::path::{Path, PathBuf}; use crate::JAVA_BIN_COMPILER; use crate::Result; #[derive(Debug, Default, Clone)] pub struct CompilerBuilder { class_path: Option, destination: Option, } impl CompilerBuilder { pub fn new() -> Self { Self::default() } pub fn class_path>(&mut self, class_path: S) -> &mut Self { self.class_path = Some(class_path.as_ref().to_path_buf()); self } pub fn destination>(&mut self, destination: S) -> &mut Self { self.destination = Some(destination.as_ref().to_path_buf()); self } pub fn build(&self) -> Compiler { let mut flags = vec![]; if let Option::Some(path) = self.destination.to_owned() { flags.push(CompilerFlag::Destination { path }); } if let Option::Some(path) = self.class_path.to_owned() { flags.push(CompilerFlag::Classpath { path }); } Compiler { flags } } } #[derive(Debug, Clone)] pub struct Compiler { flags: Vec, } impl Compiler { pub fn compile>(self, path: P) -> Result<(Option, Option)> { let mut cmd: Vec = vec![JAVA_BIN_COMPILER.to_string()]; cmd.extend( self.flags .clone() .into_iter() .flat_map(|f| Into::>::into(f)), ); cmd.extend( fs::expand_files(path)? .into_iter() .filter_map(|f| Some(f.to_str()?.to_string())), ); Ok(io::run_process(cmd.as_slice())?) } } #[derive(Debug, Clone)] pub enum CompilerFlag { Classpath { path: PathBuf }, Destination { path: PathBuf }, } impl Into> for CompilerFlag { fn into(self) -> Vec { match self { Self::Classpath { path } => vec!["-classpath".to_string(), path.display().to_string()], Self::Destination { path } => vec!["-d".to_string(), path.display().to_string()], } } }