Skip to content

Commit

Permalink
feat(runtime): add runtime builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann committed Oct 6, 2019
1 parent 816dcdc commit b3040d4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
26 changes: 15 additions & 11 deletions crates/mun/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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();
Expand All @@ -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))
}

Expand All @@ -107,11 +107,15 @@ fn compiler_options(matches: &ArgMatches) -> Result<mun_compiler::CompilerOption
})
}

fn runtime_options(matches: &ArgMatches) -> Result<mun_runtime::RuntimeOptions, failure::Error> {
let delay: u64 = matches.value_of("delay").unwrap().parse()?;
fn runtime(matches: &ArgMatches) -> Result<MunRuntime, failure::Error> {
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()
}
43 changes: 30 additions & 13 deletions crates/mun_runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: Into<PathBuf>>(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, Error> {
MunRuntime::new(self.options)
}
}

/// A runtime for the Mun scripting language.
pub struct MunRuntime {
assemblies: HashMap<PathBuf, Assembly>,
Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand Down

0 comments on commit b3040d4

Please sign in to comment.