diff --git a/crates/mun/src/main.rs b/crates/mun/src/main.rs index 989d2928c..398797796 100644 --- a/crates/mun/src/main.rs +++ b/crates/mun/src/main.rs @@ -4,7 +4,7 @@ extern crate failure; use std::time::Duration; use clap::{App, AppSettings, Arg, ArgMatches, SubCommand}; -use mun_runtime::invoke_fn; +use mun_runtime::{invoke_fn, MunRuntime, RuntimeBuilder}; fn main() -> Result<(), failure::Error> { let matches = App::new("mun") @@ -56,9 +56,8 @@ fn main() -> Result<(), failure::Error> { .arg( Arg::with_name("delay") .long("delay") - .required(true) .takes_value(true) - .help("how much to delay received filesystem events (in ms). This allows bundling of identical events, e.g. when several writes to the same file are detected. A high delay will make hot reloading less responsive."), + .help("how much to delay received filesystem events (in ms). This allows bundling of identical events, e.g. when several writes to the same file are detected. A high delay will make hot reloading less responsive. (defaults to 10 ms)"), ), ) .get_matches(); @@ -84,10 +83,11 @@ fn build(matches: &ArgMatches) -> Result<(), failure::Error> { /// Starts the runtime with the specified library and invokes function `entry`. fn start(matches: &ArgMatches) -> Result<(), failure::Error> { - let runtime_options = runtime_options(matches)?; - let mut runtime = mun_runtime::MunRuntime::new(runtime_options)?; + let mut runtime = runtime(matches)?; let entry_point = matches.value_of("entry").unwrap_or("main"); + + #[allow(clippy::unit_arg)] Ok(invoke_fn!(runtime, entry_point)) } @@ -107,11 +107,15 @@ fn compiler_options(matches: &ArgMatches) -> Result Result { - let delay: u64 = matches.value_of("delay").unwrap().parse()?; +fn runtime(matches: &ArgMatches) -> Result { + let mut builder = RuntimeBuilder::new( + matches.value_of("LIBRARY").unwrap(), // Safe because its a required arg + ); - Ok(mun_runtime::RuntimeOptions { - library_path: matches.value_of("LIBRARY").unwrap().into(), // Safe because its a required arg - delay: Duration::from_millis(delay), - }) + if let Some(delay) = matches.value_of("delay") { + let delay: u64 = delay.parse()?; + builder.set_delay(Duration::from_millis(delay)); + } + + builder.spawn() } diff --git a/crates/mun_runtime/src/lib.rs b/crates/mun_runtime/src/lib.rs index 906a82904..043863aac 100644 --- a/crates/mun_runtime/src/lib.rs +++ b/crates/mun_runtime/src/lib.rs @@ -22,6 +22,31 @@ pub struct RuntimeOptions { pub delay: Duration, } +/// A builder for the runtime. +pub struct RuntimeBuilder { + options: RuntimeOptions, +} + +impl RuntimeBuilder { + pub fn new>(library_path: P) -> Self { + Self { + options: RuntimeOptions { + library_path: library_path.into(), + delay: Duration::from_millis(10), + }, + } + } + + pub fn set_delay(&mut self, delay: Duration) -> &mut Self { + self.options.delay = delay; + self + } + + pub fn spawn(self) -> Result { + MunRuntime::new(self.options) + } +} + /// A runtime for the Mun scripting language. pub struct MunRuntime { assemblies: HashMap, @@ -135,7 +160,7 @@ invoke_fn_impl! { #[cfg(all(test, windows))] mod tests { - use super::{invoke_fn, MunRuntime, RuntimeOptions}; + use super::{invoke_fn, MunRuntime, RuntimeBuilder}; use std::path::PathBuf; use std::time::Duration; @@ -148,22 +173,14 @@ mod tests { #[test] fn mun_new_runtime() { - let options = RuntimeOptions { - library_path: test_lib_path(), - delay: Duration::from_millis(10), - }; - - let _runtime = MunRuntime::new(options).expect("Failed to initialize Mun runtime."); + let builder = RuntimeBuilder::new(test_lib_path()); + let _runtime = builder.spawn().expect("Failed to initialize Mun runtime."); } #[test] fn mun_invoke_fn() { - let options = RuntimeOptions { - library_path: test_lib_path(), - delay: Duration::from_millis(10), - }; - - let mut runtime = MunRuntime::new(options).expect("Failed to initialize Mun runtime."); + let builder = RuntimeBuilder::new(test_lib_path()); + let mut runtime = builder.spawn().expect("Failed to initialize Mun runtime."); let a: f64 = 4.0; let b: f64 = 2.0;