diff --git a/src/lib.rs b/src/lib.rs index e8531a7..d218c8d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,8 @@ mod config; +mod logger; use config::Manifest; +use logger::Logger; use std::env; use std::error::Error; use std::fmt; @@ -14,6 +16,7 @@ pub type Result = std::result::Result; pub type BoxError = Box; pub fn build() -> Result<()> { + let mut logger = Logger::new(); let root_dir = root_dir()?; let manifest = Manifest::parse_from_file(root_dir.join("Freight.toml"))?; @@ -23,8 +26,8 @@ pub fn build() -> Result<()> { let target_debug = target.join("debug"); fs::create_dir_all(&target_debug)?; - let lib_compile = || -> Result<()> { - println!("Compiling lib.rs"); + let lib_compile = |logger: &mut Logger| -> Result<()> { + logger.compiling_crate(&manifest.crate_name); Rustc::builder() .edition(manifest.edition) .crate_type(CrateType::Lib) @@ -33,12 +36,12 @@ pub fn build() -> Result<()> { .lib_dir(target_debug.clone()) .done() .run(lib_rs.to_str().unwrap())?; - println!("Compiling lib.rs -- Done"); + logger.done_compiling(); Ok(()) }; - let bin_compile = |externs: Vec<&str>| -> Result<()> { - println!("Compiling main.rs"); + let bin_compile = |logger: &mut Logger, externs: Vec<&str>| -> Result<()> { + logger.compiling_bin(&manifest.crate_name); let mut builder = Rustc::builder() .edition(manifest.edition) .crate_type(CrateType::Bin) @@ -51,20 +54,20 @@ pub fn build() -> Result<()> { } builder.done().run(main_rs.to_str().unwrap())?; - println!("Compiling main.rs -- Done"); + logger.done_compiling(); Ok(()) }; match (lib_rs.exists(), main_rs.exists()) { (true, true) => { - lib_compile()?; - bin_compile(vec![&manifest.crate_name])?; + lib_compile(&mut logger)?; + bin_compile(&mut logger, vec![&manifest.crate_name])?; } (true, false) => { - lib_compile()?; + lib_compile(&mut logger)?; } (false, true) => { - bin_compile(vec![])?; + bin_compile(&mut logger, vec![])?; } (false, false) => return Err("There is nothing to compile".into()), } diff --git a/src/logger.rs b/src/logger.rs new file mode 100644 index 0000000..0ea2cd5 --- /dev/null +++ b/src/logger.rs @@ -0,0 +1,27 @@ +use std::io; +use std::io::Write; + +pub struct Logger { + out: io::StdoutLock<'static>, +} + +impl Logger { + pub fn new() -> Self { + Self { + out: io::stdout().lock(), + } + } + pub fn compiling_crate(&mut self, crate_name: &str) { + let _ = self + .out + .write_all(format!("Compiling crate {crate_name}...").as_bytes()); + } + pub fn compiling_bin(&mut self, crate_name: &str) { + let _ = self + .out + .write_all(format!("Compiling bin {crate_name}...").as_bytes()); + } + pub fn done_compiling(&mut self) { + let _ = self.out.write_all(b"Done\n"); + } +}