Skip to content

Commit

Permalink
chore(remap): update remap-cli crate to use remap-compiler
Browse files Browse the repository at this point in the history
Signed-off-by: Jean Mertz <git@jeanmertz.com>
  • Loading branch information
JeanMertz committed Feb 4, 2021
1 parent 11e86dc commit 277c412
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
18 changes: 9 additions & 9 deletions lib/remap-cli/src/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{repl, Error};
use remap::{state, Formatter, Object, Program, Runtime, Value};
use remap::{diagnostic::Formatter, state, Program, Runtime, Target, Value};
use std::collections::BTreeMap;
use std::fs::File;
use std::io::{self, Read};
Expand Down Expand Up @@ -83,15 +83,15 @@ fn repl(_: Vec<Value>) -> Result<(), Error> {
Err(Error::ReplFeature)
}

fn execute(object: &mut impl Object, source: String) -> Result<Value, Error> {
let state = state::Program::default();
fn execute(object: &mut impl Target, source: String) -> Result<Value, Error> {
let state = state::Runtime::default();
let mut runtime = Runtime::new(state);
let (program, _) = Program::new(source.clone(), &remap_functions::all(), None, true).map_err(
|diagnostics| Error::Parse(Formatter::new(&source, diagnostics).colored().to_string()),
)?;
let program = remap::compile(&source, &remap_functions::all()).map_err(|diagnostics| {
Error::Parse(Formatter::new(&source, diagnostics).colored().to_string())
})?;

runtime
.run(object, &program)
.resolve(object, &program)
.map_err(|err| Error::Runtime(err.to_string()))
}

Expand All @@ -112,7 +112,7 @@ fn read_into_objects(input: Option<&PathBuf>) -> Result<Vec<Value>, Error> {
}?;

match input.as_str() {
"" => Ok(vec![Value::Map(BTreeMap::default())]),
"" => Ok(vec![Value::Object(BTreeMap::default())]),
_ => input
.lines()
.map(|line| Ok(serde_to_remap(serde_json::from_str(&line)?)))
Expand Down Expand Up @@ -150,5 +150,5 @@ fn should_open_repl(opts: &Opts) -> bool {
}

fn default_objects() -> Vec<Value> {
vec![Value::Map(BTreeMap::new())]
vec![Value::Object(BTreeMap::new())]
}
18 changes: 6 additions & 12 deletions lib/remap-cli/src/repl.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::Error;
use prettytable::{format, Cell, Row, Table};
use regex::Regex;
use remap::{state, Formatter, Object, Program, Runtime, Value};
use remap::{diagnostic::Formatter, state, Program, Runtime, Target, Value};
use remap_functions::all as funcs;
use rustyline::completion::Completer;
use rustyline::error::ReadlineError;
Expand All @@ -28,7 +28,7 @@ pub(crate) fn run(mut objects: Vec<Value>) -> Result<(), Error> {
let func_docs_regex = Regex::new(r"^help\sdocs\s(\w{1,})$").unwrap();

let mut compiler_state = state::Compiler::default();
let mut rt = Runtime::new(state::Program::default());
let mut rt = Runtime::new(state::Runtime::default());
let mut rl = Editor::<Repl>::new();
rl.set_helper(Some(Repl::new()));

Expand Down Expand Up @@ -135,7 +135,7 @@ pub(crate) fn run(mut objects: Vec<Value>) -> Result<(), Error> {
}

fn resolve(
object: Option<&mut impl Object>,
object: Option<&mut impl Target>,
runtime: &mut Runtime,
program: &str,
state: &mut state::Compiler,
Expand All @@ -145,18 +145,12 @@ fn resolve(
Some(object) => object,
};

let program = match Program::new_with_state(
program.to_owned(),
&remap_functions::all(),
None,
true,
state,
) {
Ok((program, _)) => program,
let program = match remap::compile_with_state(program, &remap_functions::all(), state) {
Ok(program) => program,
Err(diagnostics) => return Formatter::new(program, diagnostics).colored().to_string(),
};

match runtime.run(object, &program) {
match runtime.resolve(object, &program) {
Ok(value) => value.to_string(),
Err(err) => err.to_string(),
}
Expand Down

0 comments on commit 277c412

Please sign in to comment.