(lines: &[String])
| 141 | type VectorFieldFunction = Box<dyn Fn(f64, f64) -> (f64, f64)>; |
| 142 | |
| 143 | fn build_function_from_lines(lines: &[String]) -> eyre::Result<VectorFieldFunction> { |
| 144 | let context = rune::Context::with_default_modules()?; |
| 145 | let runtime = rune::sync::Arc::try_new(context.runtime()?)?; |
| 146 | let mut script = build_script_from_lines(lines)?; |
| 147 | let mut diagnostics = rune::Diagnostics::new(); |
| 148 | let maybe_unit = rune::prepare(&mut script) |
| 149 | .with_context(&context) |
| 150 | .with_diagnostics(&mut diagnostics) |
| 151 | .build(); |
| 152 | if !diagnostics.is_empty() { |
| 153 | let mut writer = |
| 154 | rune::termcolor::StandardStream::stderr(rune::termcolor::ColorChoice::Always); |
| 155 | diagnostics.emit(&mut writer, &script)?; |
| 156 | } |
| 157 | let unit = rune::sync::Arc::try_new(maybe_unit?)?; |
| 158 | let vm = rune::Vm::new(runtime, unit); |
| 159 | let eval_field = vm.lookup_function(["eval_field"])?; |
| 160 | let eval_expr = move |x: f64, y: f64| -> (f64, f64) { |
| 161 | eval_field |
| 162 | .call((x, y)) |
| 163 | .expect("Failed to call eval_field()") |
| 164 | }; |
| 165 | Ok(Box::new(eval_expr)) |
| 166 | } |
| 167 | |
| 168 | fn build_script_from_lines(lines: &[String]) -> eyre::Result<rune::Sources> { |
| 169 | let mut sources = rune::Sources::new(); |
no test coverage detected